Introduction to Spatial SQL
description
Transcript of Introduction to Spatial SQL
![Page 1: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/1.jpg)
Introduction to Spatial SQL
Matt FancherGIS Specialist
Public Utilities Commission of Ohio
2012 Ohio GIS ConferenceSeptember 19 - 21, 2012 | Hyatt Regency Hotel | Columbus, Ohio
![Page 2: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/2.jpg)
Structured Query Language
![Page 3: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/3.jpg)
SQL can select data from a database
SQL can insert data in a database
SQL can update data in a database
SQL can delete data from a database
![Page 4: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/4.jpg)
“I want to SELECT some informationFROM a particular source
WHERE certain criteria are met.”
![Page 5: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/5.jpg)
SELECT column1, column2FROM table1
WHERE criteria are met
![Page 6: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/6.jpg)
“I want to SELECT some informationFROM a combination of sourcesWHERE certain criteria are met.”
![Page 7: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/7.jpg)
SELECT table1.column, table2.columnFROM table1 JOIN table2ON table1.id = table2.idWHERE criteria are met
![Page 8: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/8.jpg)
DECLARE @variable AS data type
![Page 9: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/9.jpg)
SET @variable = some value
![Page 10: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/10.jpg)
SET @variable = some object
![Page 11: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/11.jpg)
SELECT column1, column2FROM table1
WHERE column1 = @variable
![Page 12: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/12.jpg)
SpatialSQL
![Page 13: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/13.jpg)
GeographyGeometry
Integer Text
VarcharDate
Time
Blob
Decimal
Long
Char
Double
Short
Numeric
Float Bit
Money
Binary
Datetime
Real
![Page 14: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/14.jpg)
Well Known Text
![Page 15: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/15.jpg)
Point(x y)
Line(x1 y1, x2 y2, … , xn yn)
Polygon(x1 y1, x2 y2, … , xn yn, x1 y1)
![Page 16: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/16.jpg)
STPointFromText(‘Point(x y)’, SRID)
STLineFromText(‘Line(x1 y1, x2 y2, … , xn yn)’, SRID)
STPolygonFromText(‘Polygon(x1 y1, x2 y2, … , xn yn, x1 y1)’, SRID)
![Page 17: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/17.jpg)
Geography::STPointFromText(‘Point(x y)’, SRID)
Geometry::STPointFromText(‘Point(x y)’, SRID)
![Page 18: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/18.jpg)
“ I want to INSERT a new record INTO a database table
using this list of VALUES.”
![Page 19: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/19.jpg)
INSERT INTO LocationTable VALUES(101, geography::STPointFromText(‘Point(x y)’, SRID))
![Page 20: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/20.jpg)
Expose Spatial PropertiesTest Spatial Relationships
Perform Spatial Operations
![Page 21: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/21.jpg)
STX & STYSTLength()
STArea()STCentroid()STEnvelope()
Spatial Properties
![Page 22: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/22.jpg)
SELECT LakeNameFROM Lakes
WHERE Shape.STArea > 1000000
![Page 23: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/23.jpg)
STIntersects()STDisjoint()STDistance()STTouches()STWithin()
Spatial Relationships
![Page 24: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/24.jpg)
SELECT Congress.District, County.CountyFROM CongressJOIN County ON
Congress.Shape.STIntersects(County.Shape) = 1
![Page 25: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/25.jpg)
STIntersection()STUnion()
STDifference()STBuffer()
STConvexHull()
Spatial Operations
![Page 26: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/26.jpg)
DECLARE @Township as Geometry;DECLARE @City as Geometry;DECLARE @Difference as Decimal;
SELECT @Township = Shape FROM TownshipWHERE Name = ‘Orange Township’;
SELECT @City = Shape FROM CityWHERE Name = ‘Columbus’;
SELECT @Difference = @Township.STDifference(@City).STArea;
![Page 27: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/27.jpg)
![Page 28: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/28.jpg)
![Page 29: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/29.jpg)
![Page 30: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/30.jpg)
![Page 31: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/31.jpg)
![Page 32: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/32.jpg)
![Page 33: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/33.jpg)
Address Google MapsGeocode Service
Latitude/LongitudeCoordinates
SQLStatements Result Set Display on Page
Application Process:
![Page 34: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/34.jpg)
SELECT Electric_Company, Company_Type, ChoiceFROM Electric_Company_TableWHERE Shape.STIntersects(geography::STPointFromText('POINT(<longitude> <latitude>)', 4269)) = 1;
SELECT Municipality_NameFROM Municipal_Utility_TableWHERE Shape.STIntersects(geography::STPointFromText('POINT(<longitude> <latitude>)', 4269)) = 1;
SELECT Telephone_Company, Telephone_ExchangeFROM Telephone_Exchange_TableWHERE Shape.STIntersects(geography::STPointFromText('POINT(<longitude> <latitude>)', 4269)) = 1;
SELECT County_NameFROM County_TableWHERE Shape.STIntersects(geography::STPointFromText('POINT(<longitude> <latitude>)', 4269)) = 1;
![Page 35: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/35.jpg)
![Page 36: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/36.jpg)
![Page 37: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/37.jpg)
--First create a point from the user’s input
DECLARE @Point AS geometry;SET @Point = geometry::STPointFromText('POINT(<x_coord> <y_coord>)', 0);
--Then buffer the point by a specified distance
DECLARE @Buffer AS geometry;SET @Buffer = @Point.STBuffer(<linear_distance>);
--Finally execute a select statement to estimate the population in the buffer
SELECTROUND(SUM(PopDen * Shape.STIntersection(@Buffer).STArea() / 27878400),0)AS PopulationEstimateFROM Census_Block_2010WHERE Shape.STIntersects(@Buffer) = 1;
SQL Solution:
![Page 38: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/38.jpg)
![Page 39: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/39.jpg)
![Page 40: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/40.jpg)
![Page 41: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/41.jpg)
The End
![Page 42: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/42.jpg)
![Page 43: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/43.jpg)
![Page 44: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/44.jpg)
![Page 45: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/45.jpg)
![Page 46: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/46.jpg)
Web Server
ColdFusion Script
T-SQLStatements
Web MapBuilt on
Google MapsAPI
MS SQL ServerDatabase
Request
Request Result Set
Response
T-SQL Script Implementation:
![Page 47: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/47.jpg)
Loading Spatial Data
• Shape2sql:
![Page 48: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/48.jpg)
SELECT
P_Id LastName FirstName Address City1 Hansen Ola Timoteivn 10 Sandnes2 Svendson Tove Borgvn 23 Sandnes3 Pettersen Kari Storgt 20 Stavanger
SELECT LastName, FirstName FROM Persons
The "Persons" table:
LastName FirstNameHansen OlaSvendson TovePettersen Kari
The result-set will look like this:
![Page 49: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/49.jpg)
WHERE
P_Id LastName FirstName Address City1 Hansen Ola Timoteivn 10 Sandnes2 Svendson Tove Borgvn 23 Sandnes3 Pettersen Kari Storgt 20 Stavanger
The "Persons" table:
SELECT * FROM Persons WHERE City='Sandnes'
P_Id LastName FirstName Address City1 Hansen Ola Timoteivn 10 Sandnes2 Svendson Tove Borgvn 23 Sandnes
The result-set will look like this:
![Page 50: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/50.jpg)
JOINP_Id LastName FirstName Address City1 Hansen Ola Timoteivn 10 Sandnes2 Svendson Tove Borgvn 23 Sandnes3 Pettersen Kari Storgt 20 Stavanger
O_Id OrderNo P_Id1 77895 32 44678 33 22456 14 24562 15 34764 15
The "Orders" table:
The "Persons" table:
LastName FirstName OrderNoHansen Ola 22456Hansen Ola 24562Pettersen Kari 77895Pettersen Kari 44678
The result-set will look like this:
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNoFROM PersonsINNER JOIN OrdersON Persons.P_Id = Orders.P_IdORDER BY Persons.LastName
![Page 51: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/51.jpg)
Overview
• Agenda:– Native spatial data types in RDBMS– Structure Query Language (SQL) syntax– Examples of spatial SQL use
• Notes:– Microsoft SQL Server 2008 used in examples– Given from perspective of ArcGIS Specialist
![Page 52: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/52.jpg)
Native Spatial Data Types in RDBMS
• Analogous to integer, text, date, etc.• Used to store spatial data objects: points, lines,
and polygons• Intrinsic to the RDBMS; no extension needed• MS SQL Server has to spatial data types:
– Geography (i.e. for geographic coordinates)– Geometry (i.e. for projected coordinates)
• Spatial data implementation compliant with OGC Simple Feature Access standard
![Page 53: Introduction to Spatial SQL](https://reader035.fdocuments.us/reader035/viewer/2022081419/56816255550346895dd2a1dc/html5/thumbnails/53.jpg)
Introduction to Spatial SQL
A brief primer on working with the native spatial data types in Microsoft
SQL Server