Spatial Data in SQL Server


Published on

fIn this presentation we review the Spatial Data Capabilities in SQL Server.


Dr. Eduardo Castro Martinez, Microsoft SQL Server MVP

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Simple types only store one value, and work with generic methods like adding, subtracting, assignment. Relational storage isn’t optimal for storing and querying hierarchies, location based data, or coping with sparse data where the data required per row differs.Some data types benefit from having complex logic for parsing and processing, and the benefit from other programming features such as inheritance. What's more the storage requirements and indexing requirements require special processing as the storage is not byte ordered.
  • Reference: data allows you to answer questions in 2 dimensions, often related to location based information. Almost every database contains location information, i.e.. Addresses, however they don’t know where those addresses are. They don’t know that 4th street is next to 3rd street and that 1st avenue intersects both of them.
  • The spatial implementation in SQL Server 2008 is based on the OGC standards.There is a hierarchy of Spatial data instances starting with a point then a line and finally a polygon. This relates to their dimensionality, a point has 0 dimensions, a line has 1 and a polygon has 2. You also have MULTI instances which contain more than one of the other types and then the collection type which can contain any of the other types
  • This is the hierarchy.Not the terms curve and surface, this implies 3 dimensions which SQL Server spatial data doesn’t support, it only supports 2 dimensions. More on that later.
  • Unlike other providers SQL splits planar and non planar data into two separate types. Geometry and geography.Non planar data is all about projections. If you think about maps of the world they have to use some form of projection to convert the elliptical surface of the earth into a flat map. In other providers they store the data in a planar projection and then perform the projection to workout the geographic calculations.We mentioned earlier that the spatial data is only in 2 dimensions. Whilst you can hold two other values Z and M for points they are not used in any of the spatial methods.
  • The geometry type covers a planar surface which is infinite your x and y can go on for ever. With the Geography type that's not the case. Latitude goes from -90 degrees to 90degrees. Longitude goes from 0 to 360.The order of how these will be parsed in the WKB (Well-Known Binary) and WKT (Well-Known Text) formats will be switched before RTM to fit in with the other products on the market.A geography shape must be contained with a single hemisphere. This doesn’t have to the north or south hemisphere but half of the earth essentially the maximum angle mad at the centre by lines to the edges cannot be greater than 180.Because the earth is a finite shape, you have to define your points in a certain order to ensure that you include the area within your shape and not the area outside of the shape. If you find you shapes are violating the hemisphere rule you might have your points going in the wrong order.
  • This demo will show how to use the SQL Server spatial data types with data from the Mondial database, which contains latitude and longitude fields for three of the tables: City, Mountain, and Island. We will use this information to construct a new column of type Geography, and then use the spatial methods of the Geography data type to perform SQL spatial queries on this data.The material for this demo can be found at the Source folder of the Spatial demo. Make sure to read the demo document to get started with this demo.
  • Reference: Some new SQL Server 2008 data types are implemented/exposed as .NET UDTs. These include the spatial data types Geometry and Geography, and also HierarchyID, mentioned later in this slideset.The Date/Time series of data type ARE NOT implemented as UDT, and therefore are referred to as "new T-SQL data types".Spatial data types are implemented as CLR User defined types. YOU DO NOT HAVE TO ENABLE CLR ON THE SERVER TO USE SPATIAL DATA TYPES.You get some benefits because of this. You can call methods on the types directly. You can also call static methods on the base type by using ::. It is the later that can be used to create instances of the types.Note: You are calling methods on the types directly and so the methods and properties are case sensitive.The OGC spec defines a set of methods that should be supported, the Geometry type supports all of them. The Geography type doesn’t. It will support more by RTM but not all of them.
  • Spatial data is created from a defined format, that is either a text base format, a binary format or an XML format.WKT is the text representationWKB is the binary formGML is the XML structure.Only WKT support passing in Z and M values.WKT is more readable but as its text it doesn’t perform as well as the binary format. It is stored in the database in a proprietary binary format. If you have that format you can also create an instance from that, if you are passing data from a client application using this format is the best to use. You create an instance by assigning a string to a type, using parse, or one of the STFROM... Methods.Much of the data in the industry is in shape file format. This is a format define by ESRI and for which you will need an application to convert to a format usable by SQL ServerFor more information on the spatial builder API and an example, see
  • You can output any of the formats that are used to create an instance.Other formats used by applications such as Google maps an virtual earth are not supported directly. You have to create these yourself more on that later.
  • SRID defines the unitof measure datum (shape of the ellipsoid) and projection. Each piece of geography data requires a spatial reference.You cannot combine data of different SRIDs, as this is like comparing apples and pears.You need to make sure you use the correct reference to ensure you calculations are correct. Lengths and areas will differ under different spatial references when dealing with geography data.There is no mechanism for translating between spatial references in SQL Server. You will need a 3rd party application to achieve that.
  • One of the killer features of spatial types is the methods they bring. You have methods that provide information about shapes.Methods that show the relationship between shapesMethods used to combine and manipulate shapes. If you’ve done set theory and venn diagrams these methods will make sense.Finally methods that provide information about the structure of the shape. Because of the limitations of SQLCLR these are not collections put rather methods that take an index to return the relevant value.
  • SQL Server extends the spatial types beyond what is in the spec. The following are the methods and there are more coming. EnveloperCentre and EnvelopeAngle provide the information used to determine the bounds of the geography type and whether it fits in a hemisphere.
  • Index is an internal table. One row represents a hierarchy of intersections of cells with in the gridEach cell that a shape intersects is recorded in the index.4 levels of grid used to provide greater accuracy. Each cell in grid is split into x more cells in the next level grid.Number of cells in each grid is defined when you create the index.
  • Top three show the level 1, Level 2 and then Level 3 and 4 intersections.If we stored all of these intersections we would have 85 matches. What we do find is that some of the cells are complete matches these don’t need to be broken down to the lower level cells. Like in Figure 4.However we still have a large number of matches, depending on the cells per object setting on the index the tessellation process will stop once it hits the limit as we have in Figure 5.This shows how imprecise the index is. It is only meant as a filter to avoid doing a very expensive calculation on all the data.
  • This demo is aimed to show the spatial capabilities of SQL Server 2008 integrated with ASP.NET. The demo relies on SQL Server to store and perform calculations on the new Geography data types; and on Virtual Earth to consume the spatial data and render it on a map.The material for this demo can be found at the Source folder of the Spatial demo. Make sure to read the demo document to get started with this demo.
  • Spatial Data in SQL Server

    1. 1. SPATIAL SUPPORT INSQL SERVER 2008 R2Ing. Eduardo Castro Martinezecastro@simsasys.com
    2. 2. Presentation Source• SQL Server 2008 R2 Update for Developers Training Kit •• Building Location-Aware Applications with the SQL Server Spatial Library • Ed Katibah, Torsten Grabs and Olivier Meyer SQL Server Microsoft Corporation
    3. 3. Relational and Non-Relational Data• Relational data uses simple data types • Each type has a single value • Generic operations work well with the types• Relational storage/query may not be optimal for • Hierarchical data • Sparse, variable, property bags• Some types • benefit by using a custom library • use extended type system (complex types, inheritance) • use custom storage and non-SQL APIs • use non-relational queries and indexing
    4. 4. Spatial Data• Spatial data provides answers to location-based queries • Which roads intersect the Microsoft campus? • Does my land claim overlap yours? • List all of the Italian restaurants within 5 kilometers• Spatial data is part of almost every database • If your database includes an address
    5. 5. Spatial Data Types• The Open Geospatial Consortium defines a hierarchy of spatial data types • Point • Linestring • Polygon • MultiPoint • MultiLinestring • MultiPolygon • GeomCollection • Non-instanciable classes based on these
    6. 6. OGC Hierarchy of Spatial Types
    7. 7. SQL Server 2008 Spatial SummaryOVERVIEW FEATURES• 2 Spatial Data Types (CLR UDT) • 2D Vector Data Support• Comprehensive set of Spatial Methods • Open Geospatial Consortium Simple• High Performance Spatial Indexes Features for SQL compatible• Spatial Library • Supported By Major GIS Vendors• Sink/Builder APIs ESRI, Intergraph, Autodesk, Pitney Bowes, Safe, etc.• Management Studio Integration • Standard feature in SQL Server Express, Workgroup, Web, Standard, Enterprise and Developer • Support for very large spatial objectsDETAILS• Geography data type for geodetic Data• Geometry data type for planar Data• Standard spatial methods STIntersects, STBuffer, STLength, STArea, etc.• Standard spatial format support WKT, WKB and GML• Multiple spatial indexes per column• Create new CLR-based spatial methods with the Builder API• Redistributable Spatial Library SQLSysClrTypes
    8. 8. SQL Server Spatial Library ResourcesSQL SERVER SPATIAL LIBRARYMicrosoft SQL Server System CLR TypesThe SQL Server System CLR Types package contains the componentsimplementing the geometry, geography, and hierarchy id types in SQL Server2008 R2. This component can be installed separately from the server to allowclient applications to use these types outside of the server.X86 Package(SQLSysClrTypes_x86.msi) – 3,342 KBX64 Package (SQLSysClrTypes._x64msi) – 3,459 KBIA64 Package(SQLSysClrTypes_ia64.msi) – 5,352 KBSearch for: Microsoft SQL Server 2008 Feature Pack, October 2008---CODEPLEX SQL Server Spatial ToolsCode Samples Utilizing the SQL Server Spatial LibrarySQL Server Spatial Tools – including source code for toolsSearch for: Codeplex SQL Server Spatial Tools
    9. 9. SQL Server 2008 and Spatial Data• SQL Server supports two spatial data types • GEOMETRY - flat earth model • GEOGRAPHY - round earth model• Both types support all of the instanciable OGC types • InstanceOf method can distinguish between them• Supports two dimension data • X and Y or Lat and Long members • Z member - elevation (user-defined semantics) • M member - measure (user-defined semantics)
    10. 10. GEOGRAPHY Requirements• GEOGRAPHY type has additional requirements• Coordinate order is • Longitude/Latitude for WKT, WKB • Latitude/Longitude for GML• Exterior polygon rings must have their describing coordinates in counter-clockwise order (left-hand rule) with interior rings (holes) in clockwise-order (right-hand rule)• A single GEOGRAPHY object cannot span more than a logical hemisphere
    11. 11. SPATIAL DATAdemo
    12. 12. 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
    13. 13. 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
    14. 14. 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
    15. 15. 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
    16. 16. Useful Methods/Properties• Descriptive • STArea • STLength • STCentroid• Relation between two instances • STIntersects • STDistance• Manipulation • STUnion • STSymDifference• Collections • STGeometryN • STPointN
    17. 17. Sample Query SELECT * Which roads intersect Microsoft’s main FROM roads campus? WHERE roads.geom.STIntersects(@ms)=1
    18. 18. 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
    19. 19. 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
    20. 20. Tessellation process
    21. 21. SPATIAL ANALYTICSdemo
    22. 22. What is CEP?Complex Event Processing (CEP) is the continuous andincremental processing of event streams from multiplesources based on declarative query and pattern specificationswith near-zero latency. Database Applications Event-driven ApplicationsQuery Ad-hoc queries or Continuous standingParadigm requests queriesLatency Seconds, hours, days Milliseconds or lessData Rate Hundreds of events/sec Tens of thousands of events/sec or more Event request output input stream response stream
    23. 23. Shuttle Tracker 519,000+ data points, covering 1 day of operation
    24. 24. 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
    25. 25. Resources• SQL Server Spatial Data Technology Center• Whitepaper: Delivering Location Intelligence with Spatial Data• MSDN Webcast: Building Spatial Applications with SQL Server 2008, Event ID: 1032353123• Whitepaper: Whats New for XML in SQL Server 2008• Whitepaper: Managing Unstructured Data with SQL Server 2008 d.mspx
    26. 26. © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.