Module 19 Working with SQL Server ® 2008 R2 Spatial Data

31
Module 19 Working with SQL Server® 2008 R2 Spatial Data

description

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 - PowerPoint PPT Presentation

Transcript of Module 19 Working with SQL Server ® 2008 R2 Spatial Data

Page 1: Module 19 Working with SQL Server ®  2008 R2  Spatial Data

Module 19Working with SQL Server® 2008 R2

Spatial Data

Page 2: 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

Page 3: Module 19 Working with SQL Server ®  2008 R2  Spatial Data

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

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

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

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

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

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

Demonstration 1A: Spatial Reference SystemsIn 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

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

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

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 ExampleInstance.Property NewYork.STAreaInstance.Method() Border.MakeValid()Type::StaticMethod() geometry::STGeomFromText()

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

geometry Data Type

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

geography Data Type

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

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

OGC Methods and PropertiesCommon methods

Common Collection Properties

Method DescriptionSTDistance The distance between two shapesSTIntersects The shape formed by the intersection of

two shapesSTArea The area of a shapeSTLength The length of a shape (for a polygon, the

sum of the lengths of all sides)STUnion The shape formed by uniting two shapesSTBuffer The shape formed by providing a buffer

region around a shape

Properties DescriptionSTPointN Returns a specific point in a collection of

pointsSTGeometryN Returns a specific geometric shape from

a collection of geometries

Page 17: Module 19 Working with SQL Server ®  2008 R2  Spatial Data

Microsoft Extensions• Microsoft has provided a number of extensions to the OGC

defined methods and properties• Common extensions:

Method DescriptionMakeValid Returns a valid shape from a potentially

invalid shapeReduce Reduces the complexity of a shape without

changing its basic shapeIsNull Returns if an object is NULLAsGml 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

Demonstration 2A: Spatial Data TypesIn 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

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

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

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

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

Implementing Spatial Indexes

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

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) = 1geometry1.STDistance(geometry2) < numbergeometry1.STDistance(geometry2) <= numbergeometry1.STEquals(geometry2)= 1geometry1.STIntersects(geometry2)= 1geometry1.STOverlaps(geometry2) = 1geometry1.STTouches(geometry2) = 1geometry1.STWithin(geometry2)= 1

Page 25: Module 19 Working with SQL Server ®  2008 R2  Spatial Data

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) < numbergeography1.STDistance(geography2) <= number

Page 26: Module 19 Working with SQL Server ®  2008 R2  Spatial Data

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

Demonstration 3A: Spatial Data in ApplicationsIn 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

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-SQLUser name AdventureWorks\AdministratorPassword Pa$$w0rd

Page 29: Module 19 Working with SQL Server ®  2008 R2  Spatial Data

Lab ScenarioYour 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

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

Module Review and Takeaways• Review Questions• Best Practices