Top Banner
ntroduction to Spatial SQL Matt Fancher GIS Specialist Public Utilities Commission of Ohio 2012 Ohio GIS Conference September 19 - 21, 2012 | Hyatt Regency Hotel | Columbus, Ohio
53

Introduction to Spatial SQL

Feb 23, 2016

Download

Documents

Braden

Introduction to Spatial SQL. Matt Fancher GIS Specialist Public Utilities Commission of Ohio. 2012 Ohio GIS Conference September 19 - 21, 2012 | Hyatt Regency Hotel | Columbus, Ohio. S tructured Q uery L anguage. SQL can select data from a database SQL can insert data in a database - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to Spatial SQL

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

Structured Query Language

Page 3: Introduction to Spatial SQL

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

“I want to SELECT some informationFROM a particular source

WHERE certain criteria are met.”

Page 5: Introduction to Spatial SQL

SELECT column1, column2FROM table1

WHERE criteria are met

Page 6: Introduction to Spatial SQL

“I want to SELECT some informationFROM a combination of sourcesWHERE certain criteria are met.”

Page 7: Introduction to Spatial SQL

SELECT table1.column, table2.columnFROM table1 JOIN table2ON table1.id = table2.idWHERE criteria are met

Page 8: Introduction to Spatial SQL

DECLARE @variable AS data type

Page 9: Introduction to Spatial SQL

SET @variable = some value

Page 10: Introduction to Spatial SQL

SET @variable = some object

Page 11: Introduction to Spatial SQL

SELECT column1, column2FROM table1

WHERE column1 = @variable

Page 12: Introduction to Spatial SQL

SpatialSQL

Page 13: Introduction to Spatial SQL

GeographyGeometry

Integer Text

VarcharDate

Time

Blob

Decimal

Long

Char

Double

Short

Numeric

Float Bit

Money

Binary

Datetime

Real

Page 14: Introduction to Spatial SQL

Well Known Text

Page 15: Introduction to Spatial SQL

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

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

Geography::STPointFromText(‘Point(x y)’, SRID)

Geometry::STPointFromText(‘Point(x y)’, SRID)

Page 18: Introduction to Spatial SQL

“ I want to INSERT a new record INTO a database table

using this list of VALUES.”

Page 19: Introduction to Spatial SQL

INSERT INTO LocationTable VALUES(101, geography::STPointFromText(‘Point(x y)’, SRID))

Page 20: Introduction to Spatial SQL

Expose Spatial PropertiesTest Spatial Relationships

Perform Spatial Operations

Page 21: Introduction to Spatial SQL

STX & STYSTLength()

STArea()STCentroid()STEnvelope()

Spatial Properties

Page 22: Introduction to Spatial SQL

SELECT LakeNameFROM Lakes

WHERE Shape.STArea > 1000000

Page 23: Introduction to Spatial SQL

STIntersects()STDisjoint()STDistance()STTouches()STWithin()

Spatial Relationships

Page 24: Introduction to Spatial SQL

SELECT Congress.District, County.CountyFROM CongressJOIN County ON

Congress.Shape.STIntersects(County.Shape) = 1

Page 25: Introduction to Spatial SQL

STIntersection()STUnion()

STDifference()STBuffer()

STConvexHull()

Spatial Operations

Page 26: Introduction to Spatial SQL

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
Page 28: Introduction to Spatial SQL
Page 29: Introduction to Spatial SQL
Page 30: Introduction to Spatial SQL
Page 31: Introduction to Spatial SQL
Page 32: Introduction to Spatial SQL
Page 33: Introduction to Spatial SQL

Address Google MapsGeocode Service

Latitude/LongitudeCoordinates

SQLStatements Result Set Display on Page

Application Process:

Page 34: Introduction to Spatial SQL

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
Page 36: Introduction to Spatial SQL
Page 37: Introduction to Spatial SQL

--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
Page 39: Introduction to Spatial SQL
Page 40: Introduction to Spatial SQL
Page 41: Introduction to Spatial SQL

The End

Page 42: Introduction to Spatial SQL
Page 43: Introduction to Spatial SQL
Page 44: Introduction to Spatial SQL
Page 45: Introduction to Spatial SQL
Page 46: Introduction to Spatial SQL

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

Loading Spatial Data

• Shape2sql:

Page 48: Introduction to Spatial SQL

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

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

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

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

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

Introduction to Spatial SQL

A brief primer on working with the native spatial data types in Microsoft

SQL Server