SQL Geography Datatypes by Jared Nielsen and the FUZION Agency

40
GEOGRAPHY DATATYPES in SQL Server by jared nielsen linkedin.com/nielsendata

Transcript of SQL Geography Datatypes by Jared Nielsen and the FUZION Agency

GEOGRAPHY DATATYPESin SQL Server

by jared nielsenlinkedin.com/nielsendata

GEOGRAPHY vs GEOMETRY

Geography Plots ellipsoidal “Round Earth” data using latitude, longitude and altitude* coordinates

Geometry Plots polygonal,

geometric and linear data using

X, Y and Z* coordinates

* not implemented well

Measuring the Earth

CoordinatesLongitude = XLatitude = YAltitude = Z

Measuring the Earth

RangesLongitude = 0 to 360°Latitude = 90° to -90°Altitude = 0 to ∞ (ft)

World Geodetic System

Global standards body that defines the coordinate systems for EarthThe latest revision is WGS 84 - referred to as EPSG:4326

www.NGA.mil

EPSG: 4326

This geodetic standard is specified in many geography datatype queries:

UPDATE SQLDevelopersSET GeoPosition = geography::Point(29.5786422, -95.2049992, 4326)

BASIC GIS CONCEPTS

Well Known TextESRI

Shape and Data Sources

Well Known Text - WKT

www.NGA.mil

Type Example Convert to Spatial

Point Point(x,y) .STPointFromText()

MultiPoint MultiPoint( (x,y), (x,y) ) .STMPointFromText()

LineString LineString( x y, x y, x y) .STLineFromText()

MultiLineString

MultiLineString( (x y, x y) (x y, x y) )

.STMLineFromText()

Polygon Polygon ( (x1 y1, x2 y2, x3 y3, x1 y1) )

.STPolyFromText()

MultiPolygon You get the idea… Keep using those parenthesis…

.STMPolyFromText()

How LONG is your LAT?

Sometimes you should use LON/LAT (WKT)Other times you need to use LAT/LON (SQL)

LAT LONSan Jacinto College = 29.578, -95.204

SET GeoPosition = geography::STGeomFromText('POINT(-95.204 29.578)', 4326)

SET GeoPosition = geography::Point(29.578, -95.204 , 4326)

Get Some Data

Positions for the International Space Station:http://sscweb.gsfc.nasa.gov/cgi-bin/Locator.cgi

Global Country Maps:http://www.vdstech.com/world-data.aspx

Zip Codes, School Districts, Demographics:http://www.data.gov

Railroads, Rivers, Cities, Volcanoes:http://webgis.wr.usgs.gov/globalgis/datasets.htm

ESRI Shapefile Converters

Convert ESRI Shapefiles to SQL Geography:http://www.sharpgis.net/page/Shape2SQL

Queries SQL Geography to a Map:http://www.sharpgis.net/page/SqlSpatial-Query-Tool

ESRI Metadata Translation:http://resources.esri.com/help/9.3/ArcGISEngine/java/gp_toolref/conversion_tools/esri_metadata_translator_conversion_.htm

SQL SERVER SPATIAL

Make a TableLoad Data

Query Spatial-ly

Making a Table

CREATE TABLE dbo.ISSPosition(Longitude decimal(18, 15) NULL,Latitude decimal(18, 15) NULL,Sampled datetime NULL,LocalTime nvarchar(50) NULL,GeoPosition geography NULL,GeomShape geometry NULL

)

Loading Data

INSERT INTO ISSPosition (Sampled, Longitude, Latitude, LocalTime) VALUES ('01/01/2015 00:00:00',-125.4,41,'15:38:18’)

Naturally I loaded more data … one data point per minute from January 1 to present It turns out that the Space Station falls fast at

17,136 mph

Convert to Geography

UPDATE ISSPositionSET GeoPosition = geography::Point([Latitude], [Longitude], 4326)GO

(note we are using the native SQL Point method so we keep LAT/LON)

Query our Data

select top 180 * from dbo.ISSPosition

Not the most thrilling outcome…Let’s try the Spatial Results Tab…

Spatial Results Tab

select top 180 * from dbo.ISSPosition

Still not that exciting… a bunch of dots

Space Station Orbit

select top 180 * from dbo.ISSPosition order by Sampled

Now we are getting somewhere!

Comma Delimited Points

DECLARE @ISSOrbitWKT nvarchar(max)SELECT @ISSOrbitWKT = STUFF((SELECT TOP 180 ',' + Convert(nvarchar(25),Longitude) + ' ' + Convert(nvarchar(25),Latitude) FROM dbo.ISSPosition ORDER BY Sampled FOR XML PATH('')) ,1,1,'')

Convert Points to LineStrings

DECLARE @ISSOrbit geographySET @ISSOrbit = geography::STLineFromText('LINESTRING('+@ISSOrbitWKT+')',4326)

Space Station Orbit

SELECT @ISSOrbit

Now you have connected points in a linestring showing the orbit…

Space Station Orbit

CREATE TABLE CoolShapes(Name Nvarchar(100) NOT NULL, GeoShape geography NULL)

GOINSERT into CoolShapes (Name, GeoShape) values (‘ISS Orbital

Path’,@ISSOrbit)

I like this shape so much I’m going to keep a copy of it

World Geography

Global DatasetsFrom ESRI Shapefiles

Load Global Maps

Load Global Maps

select @ISSOrbit union all select GeoMap from dbo.world

With a UNION, we simply plot the orbit on the Global ESRI Shapefile Map

http://www.vdstech.com/world-data.aspx

GEOGRAPHY Case Studies

INTERSECTIONBUFFERS

Spatial Methods

www.NGA.mil

Type Example Syntax

Buffer @IISOrbit.STBuffer(75000) .STBuffer(radius)

Intersect @IISOrbit.STIntersection(@China)

.STIntersection(object)

Distance @China.STDistance(@Guatemala)

.STDistance(object)

Crosses @IISOrbit.Crosses(@China) .STCrosses(object)

Within @SQLDeveloper.STWithin(@SanJacintoCollege.STBuffer(4000))

.STWithin(object)

Contains @SanJacintoCollege.STContains(@Jared)

.STContains(object)

STIntersection Method

SELECT @ISSOrbit UNION ALL SELECT GeoMap FROM World WHERE Name='China'

A human knows that the orbit crosses China, but how do we tell the computers?

http://www.vdstech.com/world-data.aspx

STIntersection Method

SELECT @ISSOrbit.STIntersects(@China)

We know it intersects… but WHERE?

http://www.vdstech.com/world-data.aspx

STIntersection Method

SELECT @ISSOrbit.STDifference(@China).STIntersection(@China)

Can you see them?

http://www.vdstech.com/world-data.aspx

STIntersection Method

SELECT @ISSOrbit.STDifference(@China).STIntersection(@China)

.STBuffer(75000) UNION ALL SELECT @China

Let’s add a few .STBuffer(s) http://www.vdstech.com/world-data.aspx

Can Your Sales Rep See the Customer at a Trade Show?

San Jacinto College

Make Some Developers

insert into SQLDevelopers (Name, Latitude, Longitude, Altitude) values ('Jared Nielsen Dark Matter', 29.5786422, -95.2049992, 15)insert into SQLDevelopers (Name, Latitude, Longitude, Altitude) values ('Jared Nielsen', 29.5786422, -95.2049992, 15)insert into SQLDevelopers (Name, Latitude, Longitude, Altitude) values ('Nancy Hidy Wilson', 29.578, -95.2049, 14)insert into SQLDevelopers (Name, Latitude, Longitude, Altitude) values ('Robert Gremillion', 29.5552929,-95.1133171,16)UPDATE SQLDevelopersSET GeoPosition = geography::Point([Latitude], [Longitude], 4326)GO

http://www.vdstech.com/world-data.aspx

Enter Tradeshow Location

ALTER TABLE CoolShapes ADD GeoPosition geography NULLGOINSERT INTO CoolShapes (Name, GeoPosition) VALUES ('San Jacinto College', geography::Point(29.5786422, -95.2049992, 4326))

http://www.vdstech.com/world-data.aspx

STIntersection Method

One Developer is Not Attending http://www.vdstech.com/world-data.aspx

SELECT GeoPosition.STBuffer(100) FROM CoolShapes WHERE Name='San Jacinto College'UNION ALLSELECT GeoPosition.STBuffer(30) FROM SQLDevelopers

STIntersection Method

Who else is here? http://www.vdstech.com/world-data.aspx

SELECT GeoPosition.STBuffer(100) FROM CoolShapes WHERE Name='San Jacinto College'UNION ALLSELECT GeoPosition.STBuffer(30) FROM SQLDevelopers WHERE NAME NOT IN ('Robert Gremillion')

More Methods

http://www.vdstech.com/world-data.aspx

DECLARE @Jared geographyDECLARE @Robert geographyDECLARE @Nancy geographyDECLARE @SanJAC GeographyDECLARE @JaredGhost geography

SELECT @Jared = GeoPosition.STBuffer(5) FROM SQLDevelopers WHERE Name = 'Jared Nielsen'SELECT @Robert = Geoposition.STBuffer(5) from SQLDevelopers WHERE Name='Robert Gremillion'SELECT @Nancy = Geoposition.STBuffer(5) from SQLDevelopers wHERE Name LIKE 'Nancy%'SELECT @SanJAC = Geoposition.STBuffer(100) from CoolShapes WHERE Name = 'San Jacinto College'SELECT @JaredGhost = Geoposition.STBuffer(2) from SQLDevelopers WHERE Name = 'Jared Nielsen Dark Matter’

More Methods

Can your sales rep see the customer?

http://www.vdstech.com/world-data.aspx

SELECT @Jared.STWithin(@SanJAC) = Boolean TrueSELECT @SanJAC.STContains(@Robert) = Boolean FalseSELECT @Jared.STDistance(@Nancy) = 61.832 meters

Declare @SalesRepVisualPerimeter geographySELECT @SalesRepVisualPerimeter = @Jared.STBuffer(10)

Declare @VisualRecognitionPerimeter geographySELECT @VisualRecognitionPerimeter= @Nancy.STBuffer(60)

More Methods

Boolean = 1= Yes http://www.vdstech.com/world-data.aspx

SELECT @VisualRecognitionPerimeter.STOverlaps(@SalesRepVisualPerimeter )

JARED NIELSENSerial Entrepreneur

InvestorSoftware Architect

Questions?