Post on 23-Feb-2016
description
Introduction to Spatial SQL
Matt FancherGIS Specialist
Public Utilities Commission of Ohio
2012 Ohio GIS ConferenceSeptember 19 - 21, 2012 | Hyatt Regency Hotel | Columbus, Ohio
Structured Query Language
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
“I want to SELECT some informationFROM a particular source
WHERE certain criteria are met.”
SELECT column1, column2FROM table1
WHERE criteria are met
“I want to SELECT some informationFROM a combination of sourcesWHERE certain criteria are met.”
SELECT table1.column, table2.columnFROM table1 JOIN table2ON table1.id = table2.idWHERE criteria are met
DECLARE @variable AS data type
SET @variable = some value
SET @variable = some object
SELECT column1, column2FROM table1
WHERE column1 = @variable
SpatialSQL
GeographyGeometry
Integer Text
VarcharDate
Time
Blob
Decimal
Long
Char
Double
Short
Numeric
Float Bit
Money
Binary
Datetime
Real
Well Known Text
Point(x y)
Line(x1 y1, x2 y2, … , xn yn)
Polygon(x1 y1, x2 y2, … , xn yn, x1 y1)
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)
Geography::STPointFromText(‘Point(x y)’, SRID)
Geometry::STPointFromText(‘Point(x y)’, SRID)
“ I want to INSERT a new record INTO a database table
using this list of VALUES.”
INSERT INTO LocationTable VALUES(101, geography::STPointFromText(‘Point(x y)’, SRID))
Expose Spatial PropertiesTest Spatial Relationships
Perform Spatial Operations
STX & STYSTLength()
STArea()STCentroid()STEnvelope()
Spatial Properties
SELECT LakeNameFROM Lakes
WHERE Shape.STArea > 1000000
STIntersects()STDisjoint()STDistance()STTouches()STWithin()
Spatial Relationships
SELECT Congress.District, County.CountyFROM CongressJOIN County ON
Congress.Shape.STIntersects(County.Shape) = 1
STIntersection()STUnion()
STDifference()STBuffer()
STConvexHull()
Spatial Operations
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;
Address Google MapsGeocode Service
Latitude/LongitudeCoordinates
SQLStatements Result Set Display on Page
Application Process:
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;
--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:
The End
Web Server
ColdFusion Script
T-SQLStatements
Web MapBuilt on
Google MapsAPI
MS SQL ServerDatabase
Request
Request Result Set
Response
T-SQL Script Implementation:
Loading Spatial Data
• Shape2sql:
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:
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:
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
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
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
Introduction to Spatial SQL
A brief primer on working with the native spatial data types in Microsoft
SQL Server