Spatial queries in SQL Server 2008 SQL Bits III – 13 th September 2008
New data types geometry and geography Spatial references Spatial operations Spatial indexes Case study
80-90% of all data has a spatial element Where are your customers? Where are your assets? Where are potential customers? Where are the flood risks? Where are your complaints coming from? Where are the accident black-spots? Where are crimes happening?
Postcodes in Glasgow Zoned Historical reasons G5 adjacent to G42 G40 in an island Postcodes designed for delivering letters
The data analysis engine No useful rendering engine Virtual Earth Map Point Other GIS systems OGC Standards compliance Plus some “extension” methods of their own
X/Y coordinate on a planar grid British National Grid Works well to ~750,000km 2 Different projections
Geodetic coordinates Covers larger areas International datasets Approximation Earth actually flattened sphere (oblate spheroid) Different models Airy 1830 (used by OS) WGS84 (used by GPS)
Spatial Reference Identifier All spatial data has an SRID SRIDs must match for spatial operations Null returned if SRIDs don’t match Geometry can have an SRID of 0 Not Geography.
Point LineString Polygon GeomCollection MultiPolygon MultiLineString MultiPoint From BOL
SELECT a.Name AS StartVenue, b.Name AS EndVenue, a.Location.STDistance(b.Location) / As Distance FROM Venue AS a INNER JOIN Venue AS b ON a.Id < b.Id ORDER BY a.Id, b.Id GCU60 Dundee U62102 MS TVP MS Edin’ HBOSGCUDundee UMS TVP
A linestring is a series of coordinates 1 dimension Defines a linear object Road Railway line River
Can use STGeomFromText STGeomFromWKB STLineFromText STLineFromWKB Parse
Geography uses SI Units Geometry uses the units of the planar system The square of the hypotenuse is equal to the sum of the square of the other two sides Not to scale 3 units 4 units ? units Distance from A to B: √( ) = 5 A B
A series of coordinates in a closed ring First and last coordinate are the same 2 dimensions Defines an area
Interior is everything inside an anti-clockwise ring Everything on the left- hand side of the perimeter line.
The specified input does not represent a valid geography instance because it exceeds a single hemisphere. Each geography instance must fit inside a single hemisphere. A common reason for this error is that a polygon has the wrong ring orientation.
Can use STGeomFromText STGeomFromWKB STPolygonFromText STPolygonFromWKB Parse
Estate Agent Filter by price, # bedrooms, type – EASY! Filter by location? Until now very vague
N e a r a r a i l w a y s t a t i o n N e a r m y w o r k N e a r a m o t o r w a y j u n c t i o n Near a good school I n s i d e t h e c i t y O u t s i d e t h e c i t y
Railway data Stations Routes
Edinburgh - Glenrothes (via Kirkcaldy) Name DistKM Edinburgh Waverley Station 0 Haymarket South Gyle Burntisland Dalmeny Kinghorn Aberdour North Queensferry Dalgety Bay Inverkeithing Kirkcaldy Glenrothes With Thornton
Decomposes space into 4 levels of grid Level 1 is the top Cells are uniform in a level A level can be a 4x4, 8x8 or 16x16 grid 8x8 by default
Table must have a primary key Primary key cannot subsequently be changed. Not on views Maximum of 249 Spatial indexes per column
Where geometry/ geography sizes vary e.g. Rail routes Small: Suburban lines Large: Intercity lines
Supports STIntersects STEquals STDistance One Geography must be a point Both sides of the spatial operation must have the same SRID
Must specify boundary of spatial area Additional methods supported STContains STOverlaps STTouches STWithin
CodePlex project More spatial methods Aggregations Scripts
GML Import and export M and Z Can store Cannot operate. Other spatial Operations Geometry has more! Visualisation .NET application integration Data Importing
Slide Deck on my website Blog posts on Spatial Data