88
Working with the geodatabase effectively using SQL Shannon Shields Shannon Shields Thomas Brown Thomas Brown Kevin Watt Kevin Watt Kevin Watt Kevin Watt

Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Working with the geodatabase effectively using SQL

Shannon Shields Shannon Shields Thomas BrownThomas Brown

Kevin WattKevin WattKevin WattKevin Watt

Page 2: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Today’s schedule

• 75 minute session– 60 – 65 minute lecture– 10 – 15 minutes Q & A following the lecture

• Cell phones and pagersp p g

• Please complete the session survey – we take your feedback very seriously!

Page 3: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Why Use a Spatial Type?

Efficiency y–Spatial data and methods are stored in the database–Spatial operations are encapsulated within the type

Applications access native dbms type no mapping layer–Applications access native dbms type – no mapping layer

Accessed using common API’s and SQL–C, C++, C#, Java, OLEDB–SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in DML and

select statement syntax

Page 4: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

What is a Spatial Type

DBMS datatype and properties– Geometry type, coordinates, dimension, spatial reference

Spatial IndexSpatial Index– Access path for quick retrieval

Operators and FunctionsOperators and Functions– Constructors – Accessor

Relational– Relational – Geometry

Page 5: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Geodatabase geometry storage options

DB2/InformixSQL Server Oracle

SDEBinary

SDELOB

SDEBinary

SQL S 2008ST_GEOMETRY

SDELOB

ST_GEOMETRY

SDO GEOMETRY

SQL Server 2008

GEOMETRY

GEOGRAPHY PostgreSQLSDO_GEOMETRYGEOGRAPHY g

ST_GEOMETRY

PostGIS

GEOMETRYGEOMETRY

Page 6: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

st_geometry Properties

• Geometry Type Name TypeName Type

• Extent (MBR)• Characteristics

– Simple Empty Dimension

-------------------------- --------------------ENTITY NUMBER(38)ENTITY NUMBER(38)NUMPTS NUMBER(38)NUMPTS NUMBER(38)

(6 )(6 )Simple, Empty, Dimension• Spatial Reference• Coordinates

MINX FLOAT(64)MINX FLOAT(64)MINY FLOAT(64)MINY FLOAT(64)MAXX FLOAT(64)MAXX FLOAT(64)MAXY FLOAT(64)MAXY FLOAT(64)MAXY FLOAT(64)MAXY FLOAT(64)AREA FLOAT(64)AREA FLOAT(64)LEN FLOAT(64)LEN FLOAT(64)SRID NUMBER(38)SRID NUMBER(38)SRID NUMBER(38)SRID NUMBER(38)POINTS BLOBPOINTS BLOB

Page 7: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Spatial Index

Rtree or Grid, Grid-Tessellating

Modeled as separate table or DBMS index

A i t d t t t d tAssociated to geometry type and operators

Enabled to the optimizer using statistics

Page 8: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Operators and Functions

Constructors - Builds a new instance of the typeyp– Can be overloaded

ST_POINT (X, Y, SRID)( )

ST_POINT (X, Y, SRID)( )ST_POINT (X, Y, Z, M, SRID)

ST_POINT (“X Y”, SRID)ST_POINT (X, Y, Z, M, SRID)ST_POINT (“X Y”, SRID)

– Derived Subtypes can construct Supertype

CREATE TABLE accidents (shape ST GEOMETRY);CREATE TABLE accidents (shape ST GEOMETRY);CREATE TABLE accidents (shape ST_GEOMETRY);INSERT INTO accidents (ST_POINT(10,10,1));CREATE TABLE accidents (shape ST_GEOMETRY);INSERT INTO accidents (ST_POINT(10,10,1));

Page 9: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Operators and Functions

“A geometry’s internal representation is different from an applications external format to improve performance and storage efficiency”

Accessor functions convert geometry data stored in internal format to an external application format

Page 10: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Operators and Functions

Accessor–Well-Known Text

INSERT INTO di t i t VALUES (ST POLYGON(‘ l ((10INSERT INTO districts VALUES (ST_POLYGON(‘polygon((10 10,50 10,50 50,10 50,10 10))’,1));

SELECT ST ASTEXT(A SHAPE) FROM districts;SELECT ST_ASTEXT(A.SHAPE) FROM districts;ST_ASTEXT(SHAPE)---------------------------------------POLYGON ((10 0 10 0 50 0 10 0 50 0 50 0 10 0POLYGON ((10.0 10.0, 50.0 10.0, 50.0 50.0, 10.0

50.0, 10.0 10.0))

Page 11: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Operators and Functions

Accessor– Well-Known Binary

SELECT ST ASTEXT(ST GEOMFROMWKB(ST ASBINARY(SHAPE),1))_ _ _FROM districts

ST ASTEXT(ST GEOMFROMWKB(ST ASBINARY(SHAPE),0))_ ( _ ( _ ( ), ))---------------------------------------POLYGON ((10.0 10.0, 50.0 10.0, 50.0 50.0, 10.0

50.0, 10.0 10.0)), ))

Page 12: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Relational operators

• Tests the spatial relationship between two geometry p p g yobjects

– Input (GEOMETRY_A, GEOMETRY_B)Returns TRUE / 1 or FALSE / 0–Returns TRUE / 1 or FALSE / 0

• Examples– ST_INTERSECTS– ST_TOUCHES– ST CONTAINS– ST_CONTAINS

Page 13: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Relational operators

• ST TOUCHES (Geometry A, Geometry B) = 0 or 1_ ( y_ , y_ )

Point / LineLine / Polygon

Point / Polygon

Line / Polygon

Point / Polygon

Polygon / PolygonLine / Line

Page 14: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Geometry operators

• Builds a new geometry object from one or more g y jexisting geometry objects

– Input: (GEOMETRY_A, args…)Returns: (GEOMETRY)–Returns: (GEOMETRY)

• ExamplesExamples– ST_BUFFER– ST_UNION

ST DIFFERENCE– ST_DIFFERENCE

Page 15: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Geometry operators

• ST_Difference (geom1 geom2)

• ST_Union (geom1 geom2) (geom1,geom2)(geom1,geom2)

Page 16: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Using SQL with the G d t bGeodatabase

Page 17: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Editing ArcGIS feature classes with SQL

• Edit simple features onlyp y–Points, lines, polygons (single or multipart)–Without geodatabase behavior

• Not part of topology geometric network• Not part of topology, geometric network

• Editing non-versioned feature classesg–Applies directly to business table (no delta tables)

• Editing versioned feature classes requires a defined workflow

Page 18: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Editing non-versioned feature classes

• Requires a unique identifier (objectid) when insertingq q ( j ) g–Obtained from classes sequence or procedure

• Can leverage DBMS functionality–Unique indexes, constraints, referential integrity, default

values, triggers , gg

Page 19: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Obtaining unique row_id values

• DBMS procedures for obtaining unique identifiers

//Oracle

SQL> SELECT registration_id FROM sde.table_registryWHERE owner = ‘TOMB’ AND table_name = ‘PARCELS’;

SQL> SELECT sde.version_user_ddl.next_row_id(‘TOMB’, 114) FROM dual;

//SQL*Server

SELECT registration_id FROM sde.sde_table_registryWHERE owner = ‘TOMB’ AND table_name = ‘PARCELS’

DECLARE @id AS INTEGERDECLARE @num_ids AS INTEGERexec sde.i114_get_ids 2, 1, @id OUTPUT, @num_ids OUTPUT

Page 20: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Editing Versioned feature classes

• Use SQL to access multiversioned views– Ability to modify geometry when stored as a spatial type– Documentation is located within ArcGIS Desktop Help

• Recommended workflow– Create multiversioned views– Create a new versionCreate a new version– Perform edits within the new version– Use ArcMap to reconcile/post to its parent version

Page 21: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Working with multiversioned views

• For non-ESRI applications which require SQL access to versioned tables

– Ability to access any version– View derives a result set based on a version query– Procedures provided with SDE installation

• SDE administration command for creating the viewSDE administration command for creating the view

sdetable –o create_mv_view –T <view_name> -t <table_name>[ i < i >] [ < >] [ D <d t b >][-i <service>] [-s <server_name>] [-D <database>] –u <DB_User_name> [-p <DB_User_password>] [-N] [-q]

sdetable –o create_mv_view –T parcels_mv –t parcels –i 5151l t b N–s alex –u tomb -N

Page 22: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Working with multiversioned views

• DBMS procedure for setting the version for the view to reference

//Oracle

SQL d i til t t i (‘t b PROPOSED SUBDIVISION’)SQL> exec sde.version_util.set_current_version (‘tomb.PROPOSED_SUBDIVISION’);

SQL> SELECT owner, parcel_id FROM parcel_mvWHERE st_envintersects(shape, 5,5,10,10) = 1;

//SQL*Server

exec sde.set_current_version (‘tomb.PROPOSED_SUBDIVISION’)or

exec dbo.set current version (‘tomb.PROPOSED SUBDIVISION’)exec dbo.set_current_version ( tomb.PROPOSED_SUBDIVISION )

//DB2

call setcurrentversion (‘tomb.PROPOSED_SUBDIVISION’)

Page 23: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Working with multiversioned views

• DBMS procedures for editing a versioned geodatabase and multiversioned views

//Oracle

SQL> exec sde.version_user_ddl.edit_version (‘tomb.PROPOSED_SUBDIVISION’, 1);

SQL> UPDATE parcel_mv SET owner = ‘Ethan Thomas’WHERE parcel_id = ‘322-2002-001’ AND st_intersects(shape,st_geom) = 1;

SQL> COMMIT;SQL> COMMIT;

SQL> exec sde.version_user_ddl.edit_version (‘tomb.PROPOSED_SUBDIVISION’, 2);

//SQL*Server//SQL Server

exec sde.edit_version (‘tomb.PROPOSED_SUBDIVISION’, 1)exec dbo.edit_version (‘tomb.PROPOSED_SUBDIVISION’, 2)

Page 24: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Rules when working with multiversioned views

• Do not update the objectid (row_id) value

• Do not edit archive enabled classes in the DEFAULT version– See Knowledge Base article: 35645

• Do not modify geometries for classes participating in topologies or geometric networks

– Will not create dirty areas or be validated– Will not maintain connectivity in the logical network

D t d t tt ib t hi h d fi d t b b h i• Do not update attributes which define geodatabase behavior– Enabled/Disabled attributes– Ancillary attributes

Weight attributes– Weight attributes– Subtypes

Page 25: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Spatial views

• Spatial views are stored as any other view -- in the database

• Views can be created with sdetable or created in the database and registered with ArcGISregistered with ArcGIS

Page 26: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Registration

• Registration informs the geodatabase of tables or views containing spatial attributes created outside ArcGIS

• Registration writes information about these tables and the spatialRegistration writes information about these tables and the spatial attribute into the geodatabase system tables so ArcGIS can access them properly

– Description of the table’s attributesp– Type of features in the spatial attribute– Spatial reference information

Page 27: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Using ArcObjects

• Nothing prevents the application developer from g p pp pconsuming relational operators via ArcObjects

//Oracle, st geometry// , _g y

pQueryDef = pFeatureWorkspace.CreateQueryDef();pQueryDef.Tables = “tb.parcels”, “tb.neighborhoods”;pQueryDef.SubFields = “parcels.objectid";Q D f Wh ClpQueryDef.WhereClause = “sde.st_intersects(parcels.shape, neighborhoods.shape) = 1”;

pCursor = pQueryDef.Evaluate();IRow pRow = pCursor.NextRow();i t C t 0int pCnt = 0;if (pRow != null){

pCnt = pCnt + 1;M h l R l C Obj t( R )Marshal.ReleaseComObject(pRow);

}

Page 28: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Working with PostgreSQLSpatial TypesSpatial Types

ESRI Developer Summit 2008ESRI Developer Summit 2008 2828

Page 29: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

What is PostgreSQL?What is PostgreSQL?

–– Open Source RDBMSOpen Source RDBMS

–– Conforms to SQL 92/99 standardsConforms to SQL 92/99 standards

–– Includes complex database featuresIncludes complex database features(Inheritance, stored procedures, UDT, views, extensible index (Inheritance, stored procedures, UDT, views, extensible index framework, etc.) framework, etc.)

ArcGIS Server Enterprise 9.3 includesArcGIS Server Enterprise 9.3 includes

–– ArcSde for PostgreSQL (installs PostgreSQL rdbms)ArcSde for PostgreSQL (installs PostgreSQL rdbms)

–– Support for SQL Spatial typesSupport for SQL Spatial types

Page 30: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Spatial types in PostgreSQLSpatial types in PostgreSQL

•• Two spatial typesTwo spatial types– ST_GEOMETRY (ESRI)(ESRI)– GEOMETRY (Open source project (Open source project -- PostGISPostGIS))

•• Both are OGC/ISO compliantBoth are OGC/ISO compliant–– support standard constructor, accessor, analytical functionssupport standard constructor, accessor, analytical functions

•• Full geodatabase functionality supported on both spatial typesFull geodatabase functionality supported on both spatial types–– geometric networks, topology, versioning, archiving, replication etc.geometric networks, topology, versioning, archiving, replication etc.

•• Both types provide spatial index functionalityBoth types provide spatial index functionality

Page 31: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

What is different between the two spatial types?What is different between the two spatial types?

GEOMETRYGEOMETRY

Resides under ‘public’ schema

ST_GEOMETRYST_GEOMETRY

R id d ‘ d ’ h • Resides under ‘public’ schema

• Only available in PostgreSQL

• Resides under ‘sde’ schema

• Consistent implementation across databases (Oracle, Informix, DB2, PostgreSQL)

• Not supported• Supports parametric curves, surfaces, and point-id

• Stored as Well Known Binary• Stored as compressed shape

Developer Summit 2008Developer Summit 2008 3131

Page 32: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

st_geometry

• Default geodatabase spatial type– GEOMETRY_STORAGE parameter value defines the storage type– ‘ST_GEOMETRY’ or ‘PG_GEOMETRY’

• Superclass st_geometry implemented as a UDT• Subtype (st_point, st_linestring, st_polygon, st_multipoint…) created as

a domain type

CREATE DOMAIN sde.st_point AS sde.st_geometryCONSTRAINT st_point_check CHECK ((point_constraint(VALUE) = true));

• Spatial Index– Uses GiST indexing framework

Implements Rtree indexing strategy– Implements Rtree indexing strategy

Page 33: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Creating data with st_geometryCreating data with st_geometry

\\ Create table with st_geometry columnCREATE TABLE gis.blocks_st (objectid INTEGER NOT NULL, block VARCHAR(24) shape st geometry);VARCHAR(24), shape st_geometry);

\\ Assign ownershipalter table gis.blocks_st owner to gis;

\\ Create spatial indexCREATE INDEX blockssp_idx ON gis.blocks_st USING gist(shape st geometry ops);st_geometry_ops);

\\ Register with SRIDSELECT sde.st_register_spatial_column(‘mydb','gis','blocks_st', 'shape',1);

\\ Insert Data

Developer Summit 2008Developer Summit 2008 3333

\\ Insert DataINSERT INTO gis.blocks_st VALUES (1,‘block', st_geometry('polygon((52 28,58 28,58 23,52 23,52 28))'1));

Page 34: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Creating data with GeometryCreating data with Geometry

\\ Create tableCREATE TABLE gis.blocks_pg (objectid INTEGER NOT NULL block VARCHAR(24));NULL,block VARCHAR(24));

\\ Add spatial columnSELECT public.Addgeometrycolumn (‘gis’, ’blocks_pg’, ’geom’, -1, ’POLYGON’, 2);

\\ Assign ownershipalter table gis.blocks pg owner to gisalter table gis.blocks_pg owner to gis

\\ Create spatial indexCREATE INDEX blockssp_idx ON blocks_st USING gist(geom i t tgist_geometry_ops

\\ Insert dataINSERT INTO gis.blocks pg (ID, geom, block)

Developer Summit 2008Developer Summit 2008 3434

g _pg ( , g , )VALUES (1,GeomFromText('POLYGON((52 28,58 28,58 23,52 23, 52 28)',2227),'block');

Page 35: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Working with spatial data in PostgreSQLWorking with spatial data in PostgreSQL

•• Register with GeodatabaseRegister with Geodatabase

// Geodatabasesdelayer -o register -l blocks_st,shape -e a -C objectid SDE -t ST GEOMETRY -i esri sde-C objectid,SDE -t ST_GEOMETRY -i esri_sde -D mydb -u gis -p gis

// Geodatabasesdelayer o register l blocks pg geom e asdelayer -o register -l blocks_pg,geom -e a -C objectid,SDE -t PG_GEOMETRY -i esri_sde -D mydb -u gis -p gis

Page 36: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Working with spatial data in PostgreSQLWorking with spatial data in PostgreSQL

• Querying spatial attributes

S C bj id ( h )SELECT st.objectid, st_astext(shape)

FROM map.blocks_st st

WHERE st_astext(sde.st_intersection

(sde.st_geometry('polygon ((52 28,58 28,58 23,

52 23,52 8))’ ,1),st.shape))::text

NOT LIKE '%EMPTY%';

OBJECTID SHAPE_WKT-------- -------------------------------------------1 POLYGON ((2217028.84 399516.70, 2217028.84

399507.82, 2217039.12 399507.82, 2217039.12399516.70, 2217028.84 399516.70))

Developer Summit 2007Developer Summit 2007 3636

Page 37: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Working with SQL Server 2008Spatial TypesSpatial Types

ESRI Developer Summit 2008ESRI Developer Summit 2008 3737

Page 38: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Spatial data in SQL Server 2008

• Microsoft introduces two new Spatial data typesGEOMETRY– XY coordinates on a plane– Supports geographic and projected coordinate systems

OGC compliant– OGC compliantGEOGRAPHY– Latitude, longitude coordinates on an ellipsoid– Supports only geographic coordinate system

Spatial reference must match SQL Server-supported EPSG codes defined in sys spatial reference systemscodes defined in sys.spatial_reference_systems

• Implemented as .NET CLR (UDT) data type

Developer Summit 2008Developer Summit 2008 3838

• Available in all 2008 editions except Compact edition

Page 39: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Comparing Geometry and Geography types

ASPECT GEOMETRY GEOGRAPHY

Line interpolation Cartesian Great Circle

Spatial Domain Defined by coordinate system

Sphere (Earth)system

Units from calculations Defined by coordinate system – same as data

Defined by coordinate system – meters or feety

unitsy

Supported coordinate systems

Any SRID SRID has no inherent

Defined in sys.spatial_reference_

meaning, but is required for comparisons

systems table

Page 40: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

SQL Server spatial indexSQL Server spatial index

• 4-level tesselated grid hierarchy• Adjustable

• Created by ArcGIS using feature class extent and dbtune parameters B_MS_SPINDEX and A_MS_SPINDEX

Developer Summit 2008Developer Summit 2008 4040

Page 41: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Spatial index usage

• SQL Server Query Optimizer determines whetherSQL Server Query Optimizer determines whether spatial index is used

– Improvements to optimizer with regard to spatial indexes announced for SQL Server 2008 SP1

–Behavior of optimizer is still evolving

C tl diffi lt t k d ti ti l i d–Currently difficult to make recommendations on spatial index settings for different mixes of features

• Sp_help_spatial_geometry/geography_index–Describes efficiency of spatial index for a given filter shapey p g p

Page 42: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Creating a SQL Server spatial indexCreating a SQL Server spatial index

CREATE SPATIAL INDEX spidx_geography

ON world rivers (geography col)

GEOGRAPHY TYPE

ON world_rivers (geography_col)

USING GEOGRAPHY_GRID WITH (

GRIDS = (LOW, LOW, MEDIUM, HIGH),

CELLS PER OBJECT = 64);CELLS_PER_OBJECT = 64);

CREATE SPATIAL INDEX spidx geometry

GEOMETRY TYPE

CREATE SPATIAL INDEX spidx_geometry

ON parcels (geometry_col)

USING GEOMETRY_GRID WITH (

BOUNDING BOX = (0 0 500 200)BOUNDING_BOX = (0, 0, 500, 200),

GRIDS = (MEDIUM, MEDIUM, MEDIUM, HIGH),

CELLS_PER_OBJECT = 64);

Page 43: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

SQL Server spatial queriesSQL Server spatial queries

-- Select all rivers that intersect Sonora

SELECT r.name, r.shape.STLength()/1000 length_km FROM gdb.rivers r WHERE(SELECT s.shape FROM gdb.states sWHERE name = 'Sonora').STIntersects(r.Shape) = 1

Page 44: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Registering existing SQL Server tables with spatial Registering existing SQL Server tables with spatial columnscolumnscolumnscolumns

• Registration: storing metadata about a tables so that g gArcGIS knows the characteristics of the spatial attribute• Tables created with ArcGIS are already registered• Tables created with SQL Server or other tools must be

manually registeredy g

sdelayer o register l blocks geog col e an+c t GEOGRAPHYsdelayer –o register –l blocks,geog_col –e an+c –t GEOGRAPHY –C objectid,sde –G 4326 –k GEOGRAPHY –P HIGH …

Developer Summit 2008Developer Summit 2008 4444

Page 45: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Registration considerations

• Registration does not add a spatial index or calculate a g player extent

–Manually create your own index, or move layer in and out of load_only_io mode

–Use sdelayer –o alter to calculate an extent

• Registration does not check for valid geometriesRegistration does not check for valid geometries–Use STIsValid and MakeValid methods

• Registration pre requisites• Registration pre-requisites–Single spatial column–Single entity type (point, line or polygon)

Si l di t f (SRID)–Single coordinate reference (SRID)

Page 46: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Registration and SRID

• What is an SRIDWhat is an SRID–ArcGIS: complete spatial reference (sde_spatial_references)

• offsets, scale, units, cluster tolerance, coordinate system

–Geography: coordinate system (sys.spatial_reference_systems)–Geometry: Arbitrary but required

• Registering a table with existing featuresExisting SQL Server SRID is recorded in–Existing SQL Server SRID is recorded in sde_spatial_references (auth_srid)

Page 47: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Registering empty tables

• ArcGIS sets SQL Server SRID– Geography: epsg code from sys.spatial_reference_systems– Geometry: epsg code from sys.spatial_reference_systems (if any)

OR ArcGIS SRID• Workflow: Geography

– Provide –G with valid coordinate reference– If no –G specified default is 4326 WGS84

• Workflow: Geometry– Using SQL, insert single feature with desired SRID– Provide –G with valid coordinate reference– Delete dummy feature after registration

• Alternatively, use –R option to use existing ArcGIS spatial referencereference

Page 48: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Working with Oraclest_geometry

ESRI Developer Summit 2008ESRI Developer Summit 2008 4848

Page 49: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Knowledge BaseMany articles availableMany articles available

Page 50: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Migration options for moving to a spatial type

• ArcGIS 9.3 default storage with Oracle is st_geometry• Geoprocessing tool for converting non-versioned and versioned

feature classes in-line

Page 51: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Oracle Export/Import with st_geometry and spatial indexesindexes

• ArcGIS 9.3– Spatial index no longer exported, but created during the import

S K l d B ti l 34342See Knowledge Base article: 34342

Page 52: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Oracle partitioning and st_geometry

• Supports range partitioning with global or local spatial indexes

• ArcGIS 9 3 provides further support for creating partitioned spatial• ArcGIS 9.3 provides further support for creating partitioned spatial indexes via the dbtune

– Future development work for providing partitioned statistics

See Knowledge Base article: 34119

Page 53: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Index scans or Full table scans– Clustering– Clustering

• Represents how well the table is clustered based on the indexed attribute

• For the rows where owner = ‘BROWN’For the rows where owner BROWN– Are the rows stored in the same data block – Or are the rows dispersed among many data blocks

• When clustered, less i/o is required to retrieve the row

Page 54: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Example: Clustered table

Key Column Values IndexKey Column Values

149

151

IndexLeaf Block

152

Table Extent

Rows149151152

Data Block

Page 55: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Example: Non-clustered table

Key Column Values IndexKey Column Values

149

151

IndexLeaf Block

152

Row 149Row 151 Row 152

Table Extent

Data Block

Page 56: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Clustering applies to spatial attributes

• Very important when the majority of queries are spatial– Every map display…

• SDE administration command for non-versioned layersy

sdeexport –o create –t <table_name> -f <export_file> [-i <service>] [-s <server_name>] [-D <database>] –u <DB User name> [-p <DB User password>] [-N] [-q]

• Able to use SQL to spatially cluster data

–u <DB_User_name> [-p <DB_User_password>] [-N] [-q]

See Knowledge Base article: 32423See Knowledge Base article: 33341

Page 57: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

st_geometry synonyms

• Best practice when developing SQL applications is to fully qualify all objects

SELECT owner, sde.st astext(shape) AS GEOMETRYSELECT owner, sde.st_astext(shape) AS GEOMETRY FROM tb.parclesWHERE sde.st_envintersects(shape,10,10,20,20) = 1

See Knowledge Base article: 34004SSee Knowledge Base article: 34328

Page 58: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

st_geometry synonyms

Oracle 11g and 10.2.0.4 prohibit the creation of public synonyms named st_* (st_geometry, st_contains, st_relate…)

• ArcGIS 9.3 no longer creates public synonyms for types and g p y y ypoperators for new installations

– Application developers must fully qualify all references to types and operators

• Upgrading from an ArcGIS 9.2 instance will not drop existing public synonyms

– Application developers can continue to leverage existing public synonyms

Page 59: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Power of aggregation

• New functionality with ArcGIS 9.3 for Oracle– st_union_all– st_intersection_all– st_convexhull_all_ _

SELECT owner, sde.st_astext(sde.st_union_all(shape)) AS GEOMETRY

FROM tb.centerlines WHERE street_name = ‘CENTER ST’;

INSERT INTO lightning areas SELECT oid nextvalINSERT INTO lightning_areas SELECT oid.nextval, sde.st_convexhull_all(shape)FROM lightning_strikesWHERE strike date = TO DATE(’03-15-08’,’MM-DD-YYYY'); _ _ ( , );

Page 60: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Indexing st_geometry properties

• Area and length are properties of the st_geometry type can be queried directly

– Verses deriving the property from the geometry directly via an operator

SELECT name, species, a.shape.area AS AREA FROM forrest_stands aWHERE a.shape.area > 100000

// Create the index…CREATE INDEX forrest_area ON forrest_stands (shape.area);

Page 61: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Working with relational toperators

Page 62: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Relational operators

Difference between a.shape and b.shapeIs it… st_contains (a.shape, b.shape)

or st_contains (b.shape, a.shape)

• Review the operator definition

• Check join aliases (a table, b table)

• Impacts the optimizer and access path

• Test and know the results!

Page 63: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

st_distance

• Is NOT a relational operator which leverages the spatial p g pindex

• It is a function for computing distance between two objects

Question:Question:“ Locate all wells within 5 miles from a given location “ Locate all wells within 5 miles from a given location ggand order the results based on the distance… ”and order the results based on the distance… ”

Page 64: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

st_distance

• Inefficient approach…pp

SELECT name, volume, st_distance(shape,st_point(125,350,3)) AS DISTANCE

FROM wellsWHERE st_distance(shape,st_point(125,350,3)) < 5ORDER BY distance

–Results in a full table scan of wells, calculating the distance , gbetween each well and the input geometry to detect if the distance is less than 5 miles

Page 65: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

st_distance

• Efficient approach…pp

SELECT name, volume, st_distance(shape,st point(125 350 3)) AS DISTANCEst_point(125,350,3)) AS DISTANCE

FROM wellsWHERE st_intersects(shape,

st buffer(st point(125 350 3) 5)) = 1st_buffer(st_point(125,350,3),5)) = 1ORDER BY distance

Page 66: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

How st_intersects and st_buffer interacts

• A very common access path for finding relationships y p g pbetween objects is to buffer an input shape and use the output geometry as input to st_intersects

Question:Question:Question:Question:“ I want to discover all single family residential parcels “ I want to discover all single family residential parcels with an appraisal value less than 125K and are within a ½ with an appraisal value less than 125K and are within a ½ mile distance from a bus stop… ”mile distance from a bus stop… ”

Page 67: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

How st_intersects and st_buffer interacts

• Efficient access path (but still expensive…)p ( p )

SELECT a.address, a.pin, st_distance(a.shape,b.shape)FROM parcels a busstops bFROM parcels a, busstops bWHERE a.appraisal < 125000 AND a.zoning = ‘SFR’AND st_intersects(b.shape,

st buffer(a shape 2640)) = 1st_buffer(a.shape,2640)) = 1

– Initial access path is via a composite attribute index on appraisal and zoning returning each parcel’s shape as input to st_buffer, which is than input to st_intersects and busstops’ spatial indexFor each candidate two spatial operations– For each candidate two spatial operations

Page 68: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

How are geometries passed to operators…

• Step 1: Access path via the parcel’s composite index for the predicate filter to obtain the parcel geometryfor the predicate filter to obtain the parcel geometry

WHERE a.appraisal < 125000 AND a.zoning = ‘SFR’

Index Parcel’s table

ROWID APPRAISAL ZONINGAADA… 45000 SFR

AABA… 52000 MPR

AABA… 59000 SFR

ROWID OBJECTID … SHAPEAADA… 136072

AABA… 852018

AACD… 79000 SFR

AABA… 59000 SFR

AACD… 79000 COM

AABC… 94000 SFR

AACD… 854211

AABA… 852018

AABC… 29375

AABC… 105000 COM

AABC… 122000 SFR

AABC… 130000 SFR

AABC… 482190

Page 69: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

How are geometries passed to operators…

• Step 2: Each geometry is passed individually to the st buffer operatorst_buffer operator

st_buffer(a.shape,2640)st_buffer(a.shape,2640)

ROWID OBJECTID … SHAPE

Parcel’s table Buffered Shape

AADA… 136072

AACD 854211

AABA… 852018

AACD… 854211

AABC… 29375

AABC… 482190

Page 70: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

How are geometries passed to operators…

• Step 3: The buffered shape becomes the input to st intersectsst_intersects

AND st_intersects(b.shape,st buffer(a.shape,2640)) = 1

AND st_intersects(b.shape,st buffer(a.shape,2640)) = 1

– Using the buffered shape’s envelope as the primary filter to search

st_buffer(a.shape,2640)) 1st_buffer(a.shape,2640)) 1

g ybusstop’s spatial index

Page 71: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

How are geometries passed to operators…

• Step 4: The envelope is applied to the spatial index to detect all intersecting busstopsdetect all intersecting busstops

97

SP_ID GX GY MINX MINY MAXX MAXYAADA 62 97 26 34 28 38

96

97AADA…AADA…

AADA… 62 97 26 34 28 38

AABA… 60 96 5 22 12 29

AACD… 62 95 28 14 32 18

AABA… 61 96 5 22 12 29

GY

95

96

AABA…AABA…

AACD…AACD…

AACD… 62 95 28 14 32 18

AABC… 61 95 13 4 23 14

AABC… 62 95 13 4 23 14

94

AABC… 61 94 13 4 23 14

AABC… 62 94 13 4 32 14

GX60 61 62 63

AABC…AABC…

Page 72: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

How are geometries passed to operators…

• Step 5: Each envelope intersecting busstops is passed to the st intersects function to return their relationto t e st_ te sects u ct o to etu t e e at o

– (true or false)

Returns true, has relation Returns false, no relation, ,

Page 73: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

How to further improve performance with relational operatorsrelational operators

• Secondary filtering…y gQuestion:Question:“ Which parcels intersect the selected neighborhood?”“ Which parcels intersect the selected neighborhood?”

SELECT COUNT(*)FROM parcels a, neighborhoods bWHERE b.name = ‘WESTWOOD’ AND st_intersects(a.shape,b.shape) = 1

401 522 P l401,522 Parcels

52 Neighborhoods

Page 74: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Primary Filter

• Neighborhoods envelope is used as the primary search g p p yfilter to discover all candidate parcels, via its spatial index

17,584 Candidate Parcels

Page 75: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Secondary Filtering

• Using parcels spatial index grid properties, categorize all exterior, interior and boundary grid cells

EXTERIOR

INTERIORINTERIOR

BOUNDARY

Page 76: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Secondary FilteringExterior conditionsExterior conditions

• All parcels in exterior grid cells are immediately p g yremoved from the candidate list

–Relation equals false (0)

5,989 Exterior Parcels5,989 Exterior Parcels

Page 77: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Secondary FilteringInterior conditionsInterior conditions

• All parcels in interior grid cells are immediately added p g yto the result set

–Relation equals true (1)

8,246 Interior Parcels,

Page 78: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Secondary FilteringBoundary conditionsBoundary conditions

• All parcels intersecting boundary cells require p g y qadditional filtering to further refine the candidate list

4,965 Boundary Parcels

Page 79: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Secondary FilteringBoundary ConditionsBoundary Conditions

• Boundary cells are further tessellated and each ycandidate parcel is evaluated for exterior, interior and boundary intersections

F2 Unknown

CandidatesF

U

Instead of

“7”

TTTU

Page 80: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Summary

• Secondary filtering reduces the number of relational y gcomparisons between the input geometry and each candidate geometry

–And significantly decreases user response time…

–Primary filtering: 16.38 seconds• 17,584 relational comparisons

–Secondary filtering: .58 seconds• 1,237 relational comparisons

10,403 Parcels in the final result set

Page 81: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Comparing Spatial Data Typesp g p yp(its like comparing apples to oranges)

Page 82: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Comparing Spatial Data Types

DB2/InformixSQL Server Oracle

Better/Faster Better/FasterBetter/Faster Better/Faster

Out-of-the-boxBLOB FeaturesSelf-Tuning

Out of the boxRequires Tuning Self-Tuning Varbinary No

Storage

BLOB FeaturesLocator/LOB Requires Storage

9.X LOB Set Blocking

How does one compare Geometry Storage types based on “out-of-the-box” tuning and default storage configurations?

Page 83: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Comparing Spatial Data Types

www.ideageospatial.com

DB2

ArcGIS

ST_GEOMETRY

BufferBufferCacheCache

SQL ServerArcGIS SDEBinary

OracleSDEBinary (LONG RAW)

SDELOB (BLOB)

ST_GEOMETRY (BLOB)

SDO_GEOMETRY (VARRAY)

Page 84: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Comparing Spatial Data Types

CO D

ST_GEOMETRY Oracle

COLD

ST GEOMETRY

HOT

ST_GEOMETRY Oracle

HOT

SAME Cold/Hot Buffer Cache Rate…?

Page 85: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Comparing Spatial Data Types

Reason…BLOB is not CACHE’d

O f O f OC COracle’s Default BLOB configuration is NOCACHE

DBTUNE - STORE AS (CHUNK 8K CACHE ENABLE

STORAGE IN ROW PCTVERSION 1)S O G O C S O )

See Knowledge Base article: 33428

Page 86: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Comparing Spatial Data Types

”…It is interesting to see how poorly Oracle ST Geometry performed throughout the tests. It is possible that ArcSDE has not yet been optimized for this new storage format. That fact that it does not use LOB storage should offer a benefit, though it is a custom data type and Oracle may not yet be optimized to deal with

t d t t h th d l t it lf d f thcustom data types, perhaps the deployment itself may need further optimization…” IdeaGeoSpatial

Performance Difference IS…ROW Storage

DBTUNE - STORE AS (CHUNK 8K CACHE ENABLELOB STORAGE…?

(

STORAGE IN ROW PCTVERSION 1)

Page 87: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

Comparing Spatial Data Types

G S S CArcGIS using App Server or Direct Connect use Compressed Binary format.

Oracle SDEBinary

SDELOB (BLOB)

ST_GEOMETRY

OracleSDO_GEOMETRY

ArcGIS SQL Server SDEBinary

Compressed binaryServer SDEBinary

GEOMETRY

GEOGRAPHYDBMS type

CompressedBinary

DB2/PostgreSQLST_GEOMETRY

PostgreSQL

PostGIS GEOMETRY

Page 88: Working with the geodatabase effectively using SQL · 2009-04-02 · –SQL extensions enable spatial types and methods in DML andSQL extensions enable spatial types and methods in

•Questions•Questions…

W ill b il bl i h T h T lk•We will be available in the Tech Talk area– Outside the presentation room