Spatial queries in SQL Server 2008 SQL Bits III – 13 th September 2008.
-
date post
19-Dec-2015 -
Category
Documents
-
view
216 -
download
1
Transcript of Spatial queries in SQL Server 2008 SQL Bits III – 13 th September 2008.
![Page 1: Spatial queries in SQL Server 2008 SQL Bits III – 13 th September 2008.](https://reader037.fdocuments.in/reader037/viewer/2022110207/56649d2d5503460f94a04245/html5/thumbnails/1.jpg)
COLIN MACKAY
WHERE’S MY DATA?
Spatial queries in SQL Server 2008
SQL Bits III – 13th September 2008
![Page 2: Spatial queries in SQL Server 2008 SQL Bits III – 13 th September 2008.](https://reader037.fdocuments.in/reader037/viewer/2022110207/56649d2d5503460f94a04245/html5/thumbnails/2.jpg)
HTTP://WWW.COLINMACKAY.NET
WHAT’S COVERED IN THIS TALK
New data types geometry and geography
Spatial references Spatial operations Spatial indexes Case study
![Page 3: Spatial queries in SQL Server 2008 SQL Bits III – 13 th September 2008.](https://reader037.fdocuments.in/reader037/viewer/2022110207/56649d2d5503460f94a04245/html5/thumbnails/3.jpg)
HTTP://WWW.COLINMACKAY.NET
WHY SHOULD I CARE ABOUT SPATIAL DATA?
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?
![Page 4: Spatial queries in SQL Server 2008 SQL Bits III – 13 th September 2008.](https://reader037.fdocuments.in/reader037/viewer/2022110207/56649d2d5503460f94a04245/html5/thumbnails/4.jpg)
HTTP://WWW.COLINMACKAY.NET
COULD FUDGE THE QUERIES
Postcodes in Glasgow
Zoned Historical reasons G5 adjacent to G42 G40 in an island Postcodes designed
for delivering letters
![Page 5: Spatial queries in SQL Server 2008 SQL Bits III – 13 th September 2008.](https://reader037.fdocuments.in/reader037/viewer/2022110207/56649d2d5503460f94a04245/html5/thumbnails/5.jpg)
HTTP://WWW.COLINMACKAY.NET
WHAT DOES SQL SERVER 2008 PROVIDE? 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
![Page 6: Spatial queries in SQL Server 2008 SQL Bits III – 13 th September 2008.](https://reader037.fdocuments.in/reader037/viewer/2022110207/56649d2d5503460f94a04245/html5/thumbnails/6.jpg)
HTTP://WWW.COLINMACKAY.NET
GEOMETRY
X/Y coordinate on a planar grid
British National Grid Works well to
~750,000km2 Different projections
![Page 7: Spatial queries in SQL Server 2008 SQL Bits III – 13 th September 2008.](https://reader037.fdocuments.in/reader037/viewer/2022110207/56649d2d5503460f94a04245/html5/thumbnails/7.jpg)
HTTP://WWW.COLINMACKAY.NET
GEOGRAPHY
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)
![Page 8: Spatial queries in SQL Server 2008 SQL Bits III – 13 th September 2008.](https://reader037.fdocuments.in/reader037/viewer/2022110207/56649d2d5503460f94a04245/html5/thumbnails/8.jpg)
HTTP://WWW.COLINMACKAY.NET
SRID
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.
![Page 9: Spatial queries in SQL Server 2008 SQL Bits III – 13 th September 2008.](https://reader037.fdocuments.in/reader037/viewer/2022110207/56649d2d5503460f94a04245/html5/thumbnails/9.jpg)
HTTP://WWW.COLINMACKAY.NET
TYPES OF SPATIAL DATA
Point LineString Polygon GeomCollection MultiPolygon MultiLineString MultiPoint
From BOL
![Page 11: Spatial queries in SQL Server 2008 SQL Bits III – 13 th September 2008.](https://reader037.fdocuments.in/reader037/viewer/2022110207/56649d2d5503460f94a04245/html5/thumbnails/11.jpg)
HTTP://WWW.COLINMACKAY.NET
FINDING DISTANCES
SELECT a.Name AS StartVenue, b.Name AS EndVenue, a.Location.STDistance(b.Location) / 1000.0 As
DistanceFROM Venue AS aINNER JOIN Venue AS b ON a.Id < b.IdORDER BY a.Id, b.Id
GCU 60
Dundee U
62 102
MS TVP 520 537 572
MS Edin’
6 66 58 521
HBOS GCU Dundee U
MS TVP
![Page 12: Spatial queries in SQL Server 2008 SQL Bits III – 13 th September 2008.](https://reader037.fdocuments.in/reader037/viewer/2022110207/56649d2d5503460f94a04245/html5/thumbnails/12.jpg)
HTTP://WWW.COLINMACKAY.NET
LINESTRING
A linestring is a series of coordinates 1 dimension Defines a linear object
Road Railway line River
![Page 13: Spatial queries in SQL Server 2008 SQL Bits III – 13 th September 2008.](https://reader037.fdocuments.in/reader037/viewer/2022110207/56649d2d5503460f94a04245/html5/thumbnails/13.jpg)
HTTP://WWW.COLINMACKAY.NET
CREATING LINESTRINGS
Can use STGeomFromText STGeomFromWKB STLineFromText STLineFromWKB Parse
![Page 15: Spatial queries in SQL Server 2008 SQL Bits III – 13 th September 2008.](https://reader037.fdocuments.in/reader037/viewer/2022110207/56649d2d5503460f94a04245/html5/thumbnails/15.jpg)
HTTP://WWW.COLINMACKAY.NET
GEOMETRY LENGTHS & DISTANCES
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:√(32+42) = 5
A
B
![Page 17: Spatial queries in SQL Server 2008 SQL Bits III – 13 th September 2008.](https://reader037.fdocuments.in/reader037/viewer/2022110207/56649d2d5503460f94a04245/html5/thumbnails/17.jpg)
HTTP://WWW.COLINMACKAY.NET
POLYGON
A series of coordinates in a closed ring First and last coordinate are the same
2 dimensions Defines an area
![Page 18: Spatial queries in SQL Server 2008 SQL Bits III – 13 th September 2008.](https://reader037.fdocuments.in/reader037/viewer/2022110207/56649d2d5503460f94a04245/html5/thumbnails/18.jpg)
HTTP://WWW.COLINMACKAY.NET
GEOGRAPHY POLYGON ORIENTATION
Interior is everything inside an anti-clockwise ring Everything on the
left-hand side of the perimeter line.
![Page 19: Spatial queries in SQL Server 2008 SQL Bits III – 13 th September 2008.](https://reader037.fdocuments.in/reader037/viewer/2022110207/56649d2d5503460f94a04245/html5/thumbnails/19.jpg)
HTTP://WWW.COLINMACKAY.NET
GEOGRAPHY POLYGON ORIENTATION
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.
![Page 20: Spatial queries in SQL Server 2008 SQL Bits III – 13 th September 2008.](https://reader037.fdocuments.in/reader037/viewer/2022110207/56649d2d5503460f94a04245/html5/thumbnails/20.jpg)
HTTP://WWW.COLINMACKAY.NET
CREATING POLYGONS
Can use STGeomFromText STGeomFromWKB STPolygonFromText STPolygonFromWKB Parse
![Page 22: Spatial queries in SQL Server 2008 SQL Bits III – 13 th September 2008.](https://reader037.fdocuments.in/reader037/viewer/2022110207/56649d2d5503460f94a04245/html5/thumbnails/22.jpg)
HTTP://WWW.COLINMACKAY.NET
FICTIONAL CASE STUDY
Estate Agent Filter by price, # bedrooms, type – EASY! Filter by location?
Until now very vague
![Page 23: Spatial queries in SQL Server 2008 SQL Bits III – 13 th September 2008.](https://reader037.fdocuments.in/reader037/viewer/2022110207/56649d2d5503460f94a04245/html5/thumbnails/23.jpg)
HTTP://WWW.COLINMACKAY.NET
COMMON SPATIAL QUESTIONS
Near a
railwa
y
statio
n
Near my
work
Near a motorway junctionNear a good
school Inside the city
Outside the city
![Page 24: Spatial queries in SQL Server 2008 SQL Bits III – 13 th September 2008.](https://reader037.fdocuments.in/reader037/viewer/2022110207/56649d2d5503460f94a04245/html5/thumbnails/24.jpg)
HTTP://WWW.COLINMACKAY.NET
THE DATA
Railway data Stations Routes
![Page 26: Spatial queries in SQL Server 2008 SQL Bits III – 13 th September 2008.](https://reader037.fdocuments.in/reader037/viewer/2022110207/56649d2d5503460f94a04245/html5/thumbnails/26.jpg)
HTTP://WWW.COLINMACKAY.NET
DOESN’T ALWAYS WORK
Edinburgh - Glenrothes (via Kirkcaldy)
Name DistKM-------------------------- ----------------Edinburgh Waverley Station 0Haymarket 1.89298770395887South Gyle 6.95446540329848Burntisland 12.086984317701Dalmeny 12.49585147351Kinghorn 13.1439998546632Aberdour 13.3392361220632North Queensferry 14.3833962761344Dalgety Bay 15.0082794626365Inverkeithing 15.7316327831032Kirkcaldy 17.9484646860063Glenrothes With Thornton 23.7022936117453
![Page 28: Spatial queries in SQL Server 2008 SQL Bits III – 13 th September 2008.](https://reader037.fdocuments.in/reader037/viewer/2022110207/56649d2d5503460f94a04245/html5/thumbnails/28.jpg)
HTTP://WWW.COLINMACKAY.NET
DISPLAY THE PROPERTIES
![Page 29: Spatial queries in SQL Server 2008 SQL Bits III – 13 th September 2008.](https://reader037.fdocuments.in/reader037/viewer/2022110207/56649d2d5503460f94a04245/html5/thumbnails/29.jpg)
HTTP://WWW.COLINMACKAY.NET
SPATIAL INDEXING
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
![Page 30: Spatial queries in SQL Server 2008 SQL Bits III – 13 th September 2008.](https://reader037.fdocuments.in/reader037/viewer/2022110207/56649d2d5503460f94a04245/html5/thumbnails/30.jpg)
HTTP://WWW.COLINMACKAY.NET
RESTRICTIONS
Table must have a primary key Primary key cannot subsequently be
changed. Not on views Maximum of 249 Spatial indexes per
column
![Page 31: Spatial queries in SQL Server 2008 SQL Bits III – 13 th September 2008.](https://reader037.fdocuments.in/reader037/viewer/2022110207/56649d2d5503460f94a04245/html5/thumbnails/31.jpg)
HTTP://WWW.COLINMACKAY.NET
WHY HAVE MULTIPLE INDEXES ON ONE COLUMN
Where geometry/ geography sizes vary
e.g. Rail routes Small: Suburban
lines Large: Intercity lines
![Page 32: Spatial queries in SQL Server 2008 SQL Bits III – 13 th September 2008.](https://reader037.fdocuments.in/reader037/viewer/2022110207/56649d2d5503460f94a04245/html5/thumbnails/32.jpg)
HTTP://WWW.COLINMACKAY.NET
SPATIAL INDEXING
Supports STIntersects STEquals STDistance
One Geography must be a point
Both sides of the spatial operation must have the same SRID
![Page 33: Spatial queries in SQL Server 2008 SQL Bits III – 13 th September 2008.](https://reader037.fdocuments.in/reader037/viewer/2022110207/56649d2d5503460f94a04245/html5/thumbnails/33.jpg)
HTTP://WWW.COLINMACKAY.NET
SPATIAL INDEXING ON A GEOMETRY
Must specify boundary of spatial area Additional methods supported
STContains STOverlaps STTouches STWithin
![Page 34: Spatial queries in SQL Server 2008 SQL Bits III – 13 th September 2008.](https://reader037.fdocuments.in/reader037/viewer/2022110207/56649d2d5503460f94a04245/html5/thumbnails/34.jpg)
HTTP://WWW.COLINMACKAY.NET
AND THERE’S MORE!
CodePlex project More spatial methods Aggregations Scripts
http://www.codeplex.com/sqlspatialtools
![Page 35: Spatial queries in SQL Server 2008 SQL Bits III – 13 th September 2008.](https://reader037.fdocuments.in/reader037/viewer/2022110207/56649d2d5503460f94a04245/html5/thumbnails/35.jpg)
HTTP://WWW.COLINMACKAY.NET
WHAT I’VE NOT MENTIONED
GML Import and export
M and Z Can store Cannot operate.
Other spatial Operations Geometry has more!
Visualisation .NET application
integration Data Importing
![Page 36: Spatial queries in SQL Server 2008 SQL Bits III – 13 th September 2008.](https://reader037.fdocuments.in/reader037/viewer/2022110207/56649d2d5503460f94a04245/html5/thumbnails/36.jpg)
HTTP://WWW.COLINMACKAY.NET
GET MORE INFORMATION
Slide Deck on my website http://www.colinmackay.net
Blog posts on Spatial Data http://blog.colinmackay.net
![Page 37: Spatial queries in SQL Server 2008 SQL Bits III – 13 th September 2008.](https://reader037.fdocuments.in/reader037/viewer/2022110207/56649d2d5503460f94a04245/html5/thumbnails/37.jpg)
HTTP://WWW.COLINMACKAY.NET
QUESTIONS
?