GEOGRAPHY DATATYPES in SQL Server by jared nielsen linkedin.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 Coordinates Longitude = X Latitude = Y Altitude = Z
Measuring the Earth Ranges Longitude = 0 to 360° Latitude = 90° to -90° Altitude = 0 to ∞ (ft)
World Geodetic System Global standards body that defines the coordinate systems for Earth The latest revision is WGS 84 - referred to as EPSG:
EPSG: 4326 This geodetic standard is specified in many geography datatype queries: UPDATE SQLDevelopers SET GeoPosition = geography:: Point( , , 4326 )
BASIC GIS CONCEPTS Well Known Text ESRI Shape and Data Sources
Well Known Text - WKT TypeExampleConvert to Spatial PointPoint(x,y).STPointFromText() MultiPointMultiPoint( (x,y), (x,y) ).STMPointFromText() LineStringLineString( x y, x y, x y).STLineFromText() MultiLineStringMultiLineString( (x y, x y) (x y, x y) ).STMLineFromText() Polygon Polygon ( ( x1 y1, x2 y2, x3 y3, x1 y1 ) ).STPolyFromText() MultiPolygonYou 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 LON San Jacinto College = , SET GeoPosition = geography:: STGeomFromText('POINT( )', 4326) SET GeoPosition = geography:: Point(29.578, , 4326)
Get Some Data Positions for the International Space Station: Global Country Maps: Zip Codes, School Districts, Demographics: Railroads, Rivers, Cities, Volcanoes:
ESRI Shapefile Converters Convert ESRI Shapefiles to SQL Geography: Queries SQL Geography to a Map: ESRI Metadata Translation: /gp_toolref/conversion_tools/esri_metadata_translator _conversion_.htm
SQL SERVER SPATIAL Make a Table Load 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/ :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 ISSPosition SET 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 nvarchar(max) = 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 geography = geography::STLineFromText('LINESTRING('
Space Station Orbit 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) GO INSERT into CoolShapes (Name, GeoShape) values (‘ISS Orbital I like this shape so much I’m going to keep a copy of it
World Geography Global Datasets From ESRI Shapefiles
Load Global Maps
union all select GeoMap from dbo.world With a UNION, we simply plot the orbit on the Global ESRI Shapefile Map
GEOGRAPHY Case Studies INTERSECTION BUFFERS
Spatial Methods TypeExampleSyntax
STIntersection Method UNION ALL SELECT GeoMap FROM World WHERE Name='China' A human knows that the orbit crosses China, but how do we tell the computers?
STIntersection Method SELECT We know it intersects… but WHERE?
STIntersection Method SELECT Can you see them?
STIntersection Method SELECT UNION ALL Let’s add a few.STBuffer(s)
MAKING IT LOCAL San Jacinto College
Make Some Developers insert into SQLDevelopers (Name, Latitude, Longitude, Altitude) values ('Jared Nielsen Dark Matter', , , 15) insert into SQLDevelopers (Name, Latitude, Longitude, Altitude) values ('Jared Nielsen', , , 15) insert into SQLDevelopers (Name, Latitude, Longitude, Altitude) values ('Nancy Hidy Wilson', , , 14) insert into SQLDevelopers (Name, Latitude, Longitude, Altitude) values ('Robert Gremillion', , ,16) UPDATE SQLDevelopers SET GeoPosition = geography::Point([Latitude], [Longitude], 4326) GO
San Jacinto College is Cool ALTER TABLE CoolShapes ADD GeoPosition geography NULL GO INSERT INTO CoolShapes (Name, GeoPosition) VALUES ('San Jacinto College', geography::Point( , , 4326))
STIntersection Method One Developer is Not Attending SELECT GeoPosition.STBuffer(100) FROM CoolShapes WHERE Name='San Jacinto College' UNION ALL SELECT GeoPosition.STBuffer(30) FROM SQLDevelopers
STIntersection Method Who else is here? SELECT GeoPosition.STBuffer(100) FROM CoolShapes WHERE Name='San Jacinto College' UNION ALL SELECT GeoPosition.STBuffer(30) FROM SQLDevelopers WHERE NAME NOT IN ('Robert Gremillion')
More Methods geography geography geography Geography geography = GeoPosition.STBuffer(5) FROM SQLDevelopers WHERE Name = 'Jared Nielsen' = Geoposition.STBuffer(5) from SQLDevelopers WHERE Name='Robert Gremillion' = Geoposition.STBuffer(5) from SQLDevelopers wHERE Name LIKE 'Nancy%' = Geoposition.STBuffer(100) from CoolShapes WHERE Name = 'San Jacinto College' = Geoposition.STBuffer(2) from SQLDevelopers WHERE Name = 'Jared Nielsen Dark Matter’
More Methods Do I stink? SELECT = Boolean True SELECT = Boolean False SELECT = meters geography
More Methods Boolean = 1
JARED NIELSEN Serial Entrepreneur Investor Software Architect Questions?