Spatial
Spatial
demo
Properties and Methods
• The spatial data types are exposed as SQLCLR
UDTs
− Use '.' syntax for properties
− Use '.' syntax for instance methods
− Use '::' syntax for static methods
− Methods and Properties are case-sensitive
• Each type uses a set of properties and methods that
correspond to OGC functionality
− With Extensions
− Geometry implements all OGC properties and methods
− Geography implements most OGC properties and methods
− 2-D vector only implemented
Input
• Spatial data is stored in a proprietary binary format
• Instance of the type can be NULL
• Can be input as
− Well Known binary - ST[Type]FromWKB
− Well Known text - ST[Type]FromText
− Geography Markup Language (GML) - GeomFromGml
• Can also use SQLCLR functions
− Parse
− Point - extension function
• Input from SQLCLR Type - SqlGeometry, SqlGeography
• Spatial builder API – Populate,
IGeometrySink/IGeographySink
Output
• Spatial Data Can Be Output As
− Well Known binary - STAsBinary
− Well Known text - STAsText
− GML - AsGml
− Text with Z and M values - AsTextZM
• SQLCLR standard method
− ToString - returns Well Known text
• As SQLCLR object - SqlGeometry, SqlGeography
• Other useful formats are GeoRSS, KML
− Not Directly Supported
SRID
• Each instance of a spatial type must have an SRID
− Spatial Reference Identifier
• SRID specifies the specification used to compute it
− SRID 4326 - GPS, default for GEOGRAPHY
− SRID 4269 - usually used by ESRI
− SRID 0 - no special reference, default for GEOMETRY
• Methods that use multiple spatial types (e.g.,
STDistance) must have types with matching SRID
− Else method returns NULL
• Geography instance must reference one of these
SRID stored in sys.spatial_reference_systems
Useful Methods/Properties
• Descriptive
− STArea
− STLength
− STCentroid
• Relation between two instances
− STIntersects
− STDistance
• Manipulation
− STUnion
− STSymDifference
• Collections
− STGeometryN
− STPointN
Sample Query
WhichSELECT
roads* intersect Microsoft’s main
FROM roads
campus?
WHERE roads.geom.STIntersects(@ms)=1
Extension Methods
• SQL Server 2008 extends OGC methods
− MakeValid - Converts to OGC valid instance
− BufferWithTolerence - similar to STBuffer, allows
approximation and variation
− Reduce - Simplify a complex geography or geometry
− NumRings, RingN - polygons with multiple rings
− GML support
− Z and M properties and AsTextZM method
− Filter - provides a quick intersection set but with false
positives
− EnvelopeCenter,EnvelopeAngle for Geography types
Spatial Indexes
• SQL Server Spatial Indexes Based on B-Trees
− Uses tessellation to tile 2D to linear
− Divides space into grid of cells(uses Hilbert algorithm)
• Meant as a first level of row elimination
− Can produce false positives
− Never false negatives
• You specify
− Bounding box of top level grid - GEOMETRY index only
− Cells per object - number of cells recorded for matching
− Grids
− Four Grid Levels
− Three Grid Densities Per Level - Low, Medium, High
Tessellation process
Spatial Analytics
demo
Review
• Spatial data provides answers to location-based
queries
• SQL Server supports two spatial data types
− GEOMETRY - flat earth model
− GEOGRAPHY - round earth model
• Spatial data has
− Useful properties and functions
− Library of spatial functions
− Three standard input and output formats
− Spatial indexes
Resources
• SQL Server Spatial Data Technology Center
http://www.microsoft.com/sql/2008/technologies/spatial.mspx