Module 19 Working with SQL Server® 2008 R2 Spatial Data.
Transcript of Module 19 Working with SQL Server® 2008 R2 Spatial Data.
![Page 1: Module 19 Working with SQL Server® 2008 R2 Spatial Data.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649cc15503460f94987b86/html5/thumbnails/1.jpg)
Module 19
Working with SQL Server® 2008 R2
Spatial Data
![Page 2: Module 19 Working with SQL Server® 2008 R2 Spatial Data.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649cc15503460f94987b86/html5/thumbnails/2.jpg)
Module Overview
• Introduction to Spatial Data
• Working with SQL Server Spatial Data Types
• Using Spatial Data in Applications
![Page 3: Module 19 Working with SQL Server® 2008 R2 Spatial Data.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649cc15503460f94987b86/html5/thumbnails/3.jpg)
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
![Page 4: Module 19 Working with SQL Server® 2008 R2 Spatial Data.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649cc15503460f94987b86/html5/thumbnails/4.jpg)
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
![Page 5: Module 19 Working with SQL Server® 2008 R2 Spatial Data.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649cc15503460f94987b86/html5/thumbnails/5.jpg)
Types of Spatial Data
• Vector vs. Raster Data Vector – series of line segments Raster – pixels or dots
• 2D, 3D, 4D
![Page 6: Module 19 Working with SQL Server® 2008 R2 Spatial Data.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649cc15503460f94987b86/html5/thumbnails/6.jpg)
Planar vs. Geodetic
• Planar systems = Flat Earth
• Geodetic systems (e.g. GPS) = Round Earth
![Page 7: Module 19 Working with SQL Server® 2008 R2 Spatial Data.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649cc15503460f94987b86/html5/thumbnails/7.jpg)
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
![Page 8: Module 19 Working with SQL Server® 2008 R2 Spatial Data.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649cc15503460f94987b86/html5/thumbnails/8.jpg)
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
![Page 9: Module 19 Working with SQL Server® 2008 R2 Spatial Data.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649cc15503460f94987b86/html5/thumbnails/9.jpg)
Demonstration 1A: Spatial Reference Systems
In this demonstration, you will see:
• The available Spatial Reference Identifiers
• The available units of measurement
![Page 10: Module 19 Working with SQL Server® 2008 R2 Spatial Data.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649cc15503460f94987b86/html5/thumbnails/10.jpg)
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
![Page 11: Module 19 Working with SQL Server® 2008 R2 Spatial Data.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649cc15503460f94987b86/html5/thumbnails/11.jpg)
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
![Page 12: Module 19 Working with SQL Server® 2008 R2 Spatial Data.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649cc15503460f94987b86/html5/thumbnails/12.jpg)
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:
Calling Method Example
Instance.Property NewYork.STArea
Instance.Method() Border.MakeValid()
Type::StaticMethod() geometry::STGeomFromText()
SELECT name, assembly_id, permission_set_desc, is_user_defined FROM sys.assemblies;
SELECT name, assembly_id, permission_set_desc, is_user_defined FROM sys.assemblies;
![Page 13: Module 19 Working with SQL Server® 2008 R2 Spatial Data.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649cc15503460f94987b86/html5/thumbnails/13.jpg)
geometry Data Type
DECLARE @Shape geometry;SET @Shape = geometry::STGeomFromText( 'POLYGON ((10 10, 10 30, 40 40, 20 10, 10 10))',0);SELECT @Shape;
DECLARE @Shape geometry;SET @Shape = geometry::STGeomFromText( 'POLYGON ((10 10, 10 30, 40 40, 20 10, 10 10))',0);SELECT @Shape;
• 2D data type
• STX and STY properties
• SRID is not relevant – defaults to zero
• Comprehensive OGC coverage
![Page 14: Module 19 Working with SQL Server® 2008 R2 Spatial Data.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649cc15503460f94987b86/html5/thumbnails/14.jpg)
geography Data Type
SELECT Border FROM dbo.CountriesWHERE CountryName = 'Italy';SELECT Border FROM dbo.CountriesWHERE CountryName = 'Italy';
• 2D data type
• Long and Lat properties
• Order is important for polygons
• Single value cannot span more than a single hemisphere
![Page 15: Module 19 Working with SQL Server® 2008 R2 Spatial Data.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649cc15503460f94987b86/html5/thumbnails/15.jpg)
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
![Page 16: Module 19 Working with SQL Server® 2008 R2 Spatial Data.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649cc15503460f94987b86/html5/thumbnails/16.jpg)
OGC Methods and Properties
Common methods
Common Collection Properties
Method Description
STDistance The distance between two shapes
STIntersects The shape formed by the intersection of two shapes
STArea The area of a shape
STLength The length of a shape (for a polygon, the sum of the lengths of all sides)
STUnion The shape formed by uniting two shapes
STBuffer The shape formed by providing a buffer region around a shape
Properties Description
STPointN Returns a specific point in a collection of points
STGeometryN Returns a specific geometric shape from a collection of geometries
![Page 17: Module 19 Working with SQL Server® 2008 R2 Spatial Data.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649cc15503460f94987b86/html5/thumbnails/17.jpg)
Microsoft Extensions
• Microsoft has provided a number of extensions to the OGC defined methods and properties
• Common extensions:
Method Description
MakeValid Returns a valid shape from a potentially invalid shape
Reduce Reduces the complexity of a shape without changing its basic shape
IsNull Returns if an object is NULL
AsGml Returns the object coded as GML (Geographic Markup Language)
BufferWithTolerance Returns a buffer around an object but uses a tolerance value to allow for rounding errors
![Page 18: Module 19 Working with SQL Server® 2008 R2 Spatial Data.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649cc15503460f94987b86/html5/thumbnails/18.jpg)
Demonstration 2A: Spatial Data Types
In this demonstration, you will see how to work with SQL Server spatial data types
![Page 19: Module 19 Working with SQL Server® 2008 R2 Spatial Data.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649cc15503460f94987b86/html5/thumbnails/19.jpg)
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
![Page 20: Module 19 Working with SQL Server® 2008 R2 Spatial Data.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649cc15503460f94987b86/html5/thumbnails/20.jpg)
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 STIntersects for a large number of points is slow
• How could you simplify the problem?
• Spatial indexes are designed to help avoid these unnecessary calculations
![Page 21: Module 19 Working with SQL Server® 2008 R2 Spatial Data.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649cc15503460f94987b86/html5/thumbnails/21.jpg)
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
![Page 22: Module 19 Working with SQL Server® 2008 R2 Spatial Data.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649cc15503460f94987b86/html5/thumbnails/22.jpg)
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
![Page 23: Module 19 Working with SQL Server® 2008 R2 Spatial Data.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649cc15503460f94987b86/html5/thumbnails/23.jpg)
Implementing Spatial Indexes
CREATE SPATIAL INDEX IX_ObjectOutline_ShapeON dbo.ObjectOutline (Shape)WITH (BOUNDING_BOX=(0,0,512,512), GRIDS =(LOW,LOW,LOW,LOW));
CREATE SPATIAL INDEX IX_ObjectOutline_ShapeON dbo.ObjectOutline (Shape)WITH (BOUNDING_BOX=(0,0,512,512), GRIDS =(LOW,LOW,LOW,LOW));
• 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
![Page 24: Module 19 Working with SQL Server® 2008 R2 Spatial Data.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649cc15503460f94987b86/html5/thumbnails/24.jpg)
geometry Methods Supported by Spatial Indexes
• Not all methods benefit from spatial indexes
• Not all predicate forms benefit from spatial indexes
• Supported forms:
geometry1.STContains(geometry2) = 1
geometry1.STDistance(geometry2) < number
geometry1.STDistance(geometry2) <= number
geometry1.STEquals(geometry2)= 1
geometry1.STIntersects(geometry2)= 1
geometry1.STOverlaps(geometry2) = 1
geometry1.STTouches(geometry2) = 1
geometry1.STWithin(geometry2)= 1
![Page 25: Module 19 Working with SQL Server® 2008 R2 Spatial Data.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649cc15503460f94987b86/html5/thumbnails/25.jpg)
geography Methods Supported by Spatial Indexes
• Not all methods benefit from spatial indexes
• Not all predicate forms benefit from spatial indexes
• Supported forms:
geography1.STIntersects(geography2)= 1
geography1.STEquals(geography2)= 1
geography1.STDistance(geography2) < number
geography1.STDistance(geography2) <= number
![Page 26: Module 19 Working with SQL Server® 2008 R2 Spatial Data.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649cc15503460f94987b86/html5/thumbnails/26.jpg)
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
![Page 27: Module 19 Working with SQL Server® 2008 R2 Spatial Data.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649cc15503460f94987b86/html5/thumbnails/27.jpg)
Demonstration 3A: Spatial Data in Applications
In this demonstration you will see how to use SQL Server spatial data to solve some business questions
![Page 28: Module 19 Working with SQL Server® 2008 R2 Spatial Data.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649cc15503460f94987b86/html5/thumbnails/28.jpg)
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: 45 minutes
Virtual machine 623XB-MIA-SQL
User name AdventureWorks\Administrator
Password Pa$$w0rd
![Page 29: Module 19 Working with SQL Server® 2008 R2 Spatial Data.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649cc15503460f94987b86/html5/thumbnails/29.jpg)
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.
![Page 30: Module 19 Working with SQL Server® 2008 R2 Spatial Data.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649cc15503460f94987b86/html5/thumbnails/30.jpg)
Lab Review
• Where would you imagine you might use spatial data in your own business applications?
![Page 31: Module 19 Working with SQL Server® 2008 R2 Spatial Data.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649cc15503460f94987b86/html5/thumbnails/31.jpg)
Module Review and Takeaways
• Review Questions
• Best Practices