310 likes | 535 Views
Module 19 Working with SQL Server ® 2008 R2 Spatial Data. Module Overview. Introduction to Spatial Data Working with SQL Server Spatial Data Types Using Spatial Data in Applications. Lesson 1: Introduction to Spatial Data. Target Applications Types of Spatial Data Planar vs. Geodetic
E N D
Module 19 Working with SQL Server® 2008 R2 Spatial Data
Module Overview • Introduction to Spatial Data • Working with SQL Server Spatial Data Types • Using Spatial Data in Applications
Lesson 1: Introduction to Spatial Data • Target Applications • Types of Spatial Data • Planar vs. Geodetic • OGC Object Hierarchy • Spatial Reference Identifiers • Demonstration 1A: Spatial Reference Systems
Target Applications • There is a perception that spatial applications are quite separate to mainstream business applications • Almost every business application can benefit from spatial data and functions • Locations of customers, stores, offices • All addresses • Intersections and distances • Business Intelligence applications particularly benefit from spatial visualizations
Types of Spatial Data • Vector vs. Raster Data • Vector – series of line segments • Raster – pixels or dots • 2D, 3D, 4D
Planar vs. Geodetic • Planar systems = Flat Earth • Geodetic systems (e.g. GPS) = Round Earth
OGC Object Hierarchy • Open Geospatial Consortium is the relevant industry body • OGC defined an object tree • SQL Server data types are based on the Geometry hierarchy
Spatial Reference Identifiers • Each spatial instance has a spatial reference identifier (SRID) • SRID corresponds to a spatial reference system that is a way of performing measurements • SRID 4326 is the WGS84 system (commonly implemented as the GPS system) • SRID 0 is used when no system is needed (flat earth) • When two spatial instances are used in a calculation, their SRIDs must match • EPSG standard is used to define available SRIDs
Demonstration 1A: Spatial Reference Systems In this demonstration, you will see: • The available Spatial Reference Identifiers • The available units of measurement
Lesson 2: Working with SQL Server Spatial Data Types • SQL Server Spatial Data • System vs. User SQL CLR Types • geometry Data Type • geography Data Type • Spatial Data Formats • OGC Methods and Properties • Microsoft Extensions • Demonstration 2A: Spatial Data Types
SQL Server Spatial Data • Data Types • geometry data type (flat Earth - planar) • geography data type (round Earth - geodetic) • Bing Maps SDK updated • SQL Server Reporting Services map control • Microsoft.SqlServer.Types assembly • OGC and Microsoft extension methods • ST prefix on OGC defined methods • No prefix on Microsoft extension methods
System vs. User SQL CLR Types • System types are enabled regardless of ‘clr enabled’ setting • geometry, geography, hierarchyid use large CLR object support • Call properties and methods on CLR objects via: SELECT name,assembly_id, permission_set_desc,is_user_defined FROMsys.assemblies;
geometry Data Type • 2D data type • STX and STY properties • SRID is not relevant – defaults to zero • Comprehensive OGC coverage DECLARE @Shape geometry; SET @Shape =geometry::STGeomFromText( 'POLYGON ((10 10, 10 30, 40 40, 20 10, 10 10))',0); SELECT @Shape;
geography Data Type • 2D data type • Long and Lat properties • Order is important for polygons • Single value cannot span more than a single hemisphere SELECT Border FROMdbo.Countries WHERECountryName='Italy';
Spatial Data Formats • Internal binary format of the spatial types not normally used directly • Need to be able to input/output as strings • Parsing • WKT – Well known text • WKB – Well known binary • GML – Geography markup language (XML variant) • Parse() assumes WKT • Output • Options to output above formats including Z and M values • ToString() provides WKT
OGC Methods and Properties Common methods Common Collection Properties
Microsoft Extensions • Microsoft has provided a number of extensions to the OGC defined methods and properties • Common extensions:
Demonstration 2A: Spatial Data Types In this demonstration, you will see how to work with SQL Server spatial data types
Lesson 3: Using Spatial Data in Applications • Performance Issues in Spatial Queries • Tessellation Process • Spatial Indexes • Implementing Spatial Indexes • geometry Methods Supported by Spatial Indexes • geography Methods Supported by Spatial Indexes • Extending SQL Server Spatial • Demonstration 3A: Spatial Data in Applications
Performance Issues in Spatial Queries • Spatial queries can involve a large number of data points • Imagine trying to locate streets that intersect your suburb or region • Executing methods like STIntersectsfor a large number of points is slow • How could you simplify the problem? • Spatial indexes are designed to help avoid these unnecessary calculations
Tessellation Process • Spatial indexes allow us to break large problems into ever smaller problems as we move through relevant levels • SQL Server uses a four-level grid • Tessellation rules are applied to eliminate areas not touched by the shape
Spatial Indexes • Spatial indexes in SQL Server work in a two-phase method • Primary filter • Finds all possible candidates • False positives are ok at this stage • No false negatives • Secondary filter • Removes false positives • Applies the spatial method (that is, STIntersects) from the original predicate in your WHERE clause • Filter method shows effectiveness of the Primary filter
Implementing Spatial Indexes • Use the CREATE SPATIAL INDEX statement • geometry has a BOUNDING_BOX • ONLINE builds are not supported • Can be useful to index one column more than once with different tessellation levels • Table must have a clustered primary key CREATESPATIALINDEXIX_ObjectOutline_Shape ONdbo.ObjectOutline(Shape) WITH (BOUNDING_BOX=(0,0,512,512), GRIDS =(LOW,LOW,LOW,LOW));
geometry Methods Supported by Spatial Indexes • Not all methods benefit from spatial indexes • Not all predicate forms benefit from spatial indexes • Supported forms:
geography Methods Supported by Spatial Indexes • Not all methods benefit from spatial indexes • Not all predicate forms benefit from spatial indexes • Supported forms:
Extending SQL Server Spatial • Functions from CodePlex • IsValidGeographyFromGeometry, IsValidGeographyFromText, MakeValidGeographyFromGeography, MakeValidGeographyFromText, ConvexHullGeography, ConvexHullGeographyFromText, DensifyGeography, InterpolateBetweenGeog, InterpolateBetwenGeom, LocateAlongGeog, LocateAlongGeom, ShiftGeometry, VacuousGeographyToGeometry, VacuousGeometryToGeography • Types from CodePlex • SqlProjection (Abers Equal Area, Equirectangular, Lambert Conformal Conic, Mercator, Oblique Mercator, Transverse Mercator, Gnomonic) • AffineTransform • Aggregates from CodePlex • GeographyUnionAggregate, GeometryEnvelopeAggregate
Demonstration 3A: Spatial Data in Applications In this demonstration you will see how to use SQL Server spatial data to solve some business questions
Lab 19: Working with SQL Server Spatial Data • Exercise 1: Familiarity With Geometry Data Type • Exercise 2: Adding Spatial Data to an Existing Table • Challenge Exercise 3: Business Application of Spatial Data (Only if time permits) Logon information Estimated time: 45minutes
Lab Scenario Your organization has only recently begun to acquire spatial data within its databases. The new Marketing database was initially designed prior to the company beginning to implement spatial data. One of the developers has provided a table of the locations where prospects live. It is called Marketing.ProspectLocation. A second developer has added columns to it for Latitude and Longitude and geocoded the addresses. You will make some changes to the system to help support the need for spatial data.
Lab Review • Where would you imagine you might use spatial data in your own business applications?
Module Review and Takeaways • Review Questions • Best Practices