Storing and Querying Spatial Data in SQL Server

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 30

Module 15

Storing and Querying Spatial Data


in SQL Server
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: Spatial Reference Systems
Target Applications

• There is a perception that spatial applications are


separate to mainstream business applications
• Almost every business application can benefit
from spatial data and functions
• Locations of customers, stores, and offices
• All addresses
• Intersections and distances

• Business intelligence applications particularly


benefit from spatial visualizations
Types of Spatial Data

• Vector versus raster data


• Vector: a series of line segments
• Raster: a series of pixels or dots
Planar vs. Geodetic

• Planar systems represent the Earth as flat


• Geodetic systems (for example GPS) represent the Earth
as round
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: Spatial Reference Systems

In this demonstration, you will see how to:


• View the available special reference systems
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: Spatial Data Types
SQL Server Spatial Data

• Data types
• geometry data type (flat-earth; planar)
• geography data type (round-earth; geodetic)

• Bing maps SDK


• SQL Server Reporting Services map control
• 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 turned on regardless of the “clr


enabled” setting
• geometry, geography, and hierarchyid use large
CLR object support
• Call properties and methods on CLR objects by
using:
• Instance.Property—for example, NewYork.STArea
Border
• Instance-Method()—for example, Border.MakeValid()
• Type::StaticMethod()—for example,
geometry::STGeomFromText()
geometry Data Type

• Two-dimensional data type


• Has STX and STY properties
• SRID is not relevant, it defaults to zero
• Comprehensive OGC coverage
geography Data Type

• Two-dimensional data type


• Has Long and Lat properties
• Order is important for polygons
Spatial Data Formats

• Internal binary format of the spatial types is not normally


used directly
• Must be able to enter data and return results as strings
• Parsing
• Well-known text (WKT)
• Well-known binary (WKB)
• Geography markup language (GML) an XML variant
• Parse() assumes WKT
• Output
• Options to generate output for the above formats including Z and
M values
• ToString() provides WKT
OGC Methods and Properties

• Common methods
• STDistance: the distance between shapes
• STIntersects: the intersection of two shapes
• STArea: the area of a shape
• STLength: the length of a shape
• STUnion: the shape formed by uniting two shapes
• STBuffer: the shape formed by points around a shape

• Common collection properties


• STPointN: returns a specific point in a collection of
points
• STGeometry: returns a specific geometric shape from a
collection of geometries
Microsoft Extensions

• Microsoft has provided a number of extensions


to the OGC-defined methods and properties
• Common extensions include:
• MakeValid: returns a valid shape from a potentially
invalid shape
• Reduce: reduces the complexity of a shape without
changing its basic shape
• IsNull: returns 1 if an object is NULL
• AsGML: returns the object coded as GML
• BufferWithTolerance: returns a buffer around an object,
but uses a tolerance value to allow for rounding errors
Demonstration: Spatial Data Types

In this demonstration, you will see how to:


• Work with spatial data types in SQL Server
Lesson 3: Using Spatial Data in Applications

Performance Issues in Spatial Queries


Spatial Indexes
Tessellation Process
Implementing Spatial Indexes
geometry Methods Supported by Spatial Indexes
geography Methods Supported by Spatial Indexes
• Demonstration: Spatial Data in Applications
Performance Issues in Spatial Queries

• Spatial queries can involve a large number of


data points, causing performance problems
Spatial Indexes

• Spatial indexes in SQL Server work in a two-


phase manner
• Primary filter
• Finds all possible candidate rows
• Can include false positives

• Secondary filter
• Removes false positives

• Filter method shows effectiveness of the primary


filter
Tessellation Process

• Spatial indexes use a tessellation process to


produce a list of rows which are of interest
• Four-level hierarchical grid used for breaking
problems down
• Three tessellation rules applied
• Covering rule
• Cells-per-object rule
• Deepest cell rule

• Two tessellation schemes depending on data


type
Implementing Spatial Indexes

• Created using CREATE SPATIAL INDEX


statement
• geometry has a BOUNDING_BOX setting
• ONLINE index builds are not supported

• A single column can have more than one spatial


index
• The table must have a clustered primary key
geometry Methods Supported by Spatial Indexes

• Not all geometry methods benefit from spatial


indexes
• Not all predicate forms benefit from spatial
indexes
geography Methods Supported by Spatial
Indexes

• Not all geography methods benefit from spatial


indexes
• Not all predicate forms benefit from spatial
indexes
Demonstration: Spatial Data in Applications

In this demonstration, you will see how to:


• Use spatial data in SQL Server to solve some
business questions
Lab: Working with SQL Server Spatial Data

Exercise 1: Become Familiar with the geometry Data


Type
Exercise 2: Add Spatial Data to an Existing Table
• Exercise 3: Find Nearby Locations

Logon Information
Virtual machine: 20762C-MIA-SQL
User name: ADVENTUREWORKS\Student
Password: Pa55w.rd

Estimated Time: 45 Minutes


Lab Scenario

Your organization has recently started to acquire


spatial data within its databases. The new
Marketing database was designed before the
company started to implement spatial data. One
of the developers has provided a table of the
locations where prospects live, 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

• This lab exercise explored the use of SQL Server


spatial data types. You learned how to create
different shapes using the GEOMETRY data type.
You also saw how to alter a table to hold spatial
data, including creating an index and creating a
stored procedure to use the data.
Module Review and Takeaways

Best Practice
• Common Issues and Troubleshooting Tips

You might also like