Thursday, 3 May 2012

SQL Geospatial Data Types


Geospatial Data Types is a new data type available in MS SQL server 2008. This is feature is so useful to store the information such as to store and manipulate shapes, sizes, and locations using the new geometry and geography data types. It is so helpful in calculating areas, distances, and intersections above that spatial data. SQL Server 2008 is designed specifically to enable the rapid development of application for spatial capabilities in database without requiring deep knowledge of how to program spatial calculations

For eg, this feature is so useful for user to find a nearest gas station from the user current geographic location, similarly we can use this data type for different application based on the user requirement.

Types of Spatial Models
There are two types of Spatial Models in SQL server. They are Planar (Flat-Earth) Model and Geodetic (Round-Earth) Model.

Planar (Flat-Earth) Model: It is specifically designed for the 2-Dimensional geometric figures especially an object in a 2-D plane like world map in an atlas book. Normally the 2-D are represented by (X,Y ) Coordinates. This is so useful to find the distance, area, perimeter of any surface.

Geodetic (Round-Earth) Model: It is specifically designed for the 3-Dimensional geometric figures especially like a world globe. Normally the 3-D are represented by latitude and longitude points in world.

DataTypes
  •    The geometry data type is provided to store and process spatial data for the planar (flat-earth) model. 
  •     The geography data type is provided to store and process spatial data for the geodetic (round-earth) model.

Shapes are projected onto spatial models using vector objects which are collections of points, lines, and polygons (closed shapes). Both the geometry and geography data types support the same Well-Known Text (WKT) markup language, which is a convention that expresses the vector objects.

WKT String
Description
POINT(1,9)
A single point at xy-coordinates 1, 9
POINT(-4.06  5.08)
A single point on the earth (longitude/latitude coordinates)
LINESTRING(1 5,11 55,10 15)
A two-part line, drawn between three points specified as xy-coordinates
POLYGON((

-7.17031 3.95601, -7.16786 3.95778, -7.17921 3.96874, -7.18441 3.96512, -7.17031 3.95601))
An enclosed shape on the earth drawn between the points specified as longitude/latitude coordinates

Geometry Example
The below sample example show how to define geometry data type for City table.


CREATE TABLE City
 (CityId    int PRIMARY KEY,
  CityName  nvarchar(20),
  CityGeo   geometry)

INSERT INTO City VALUES (1, 'New york',
 geometry::Parse('POLYGON ((0 0, 150 0, 150 150, 0 150, 0 0))'))

Geography Example 
To create a map of occurrences of event in different locations across different area is represented by using geography example.

CREATE TABLE EventRegion
 (RegionId    int PRIMARY KEY,
  RegionName  nvarchar(32),
  MapShape    geography)
 
INSERT INTO EventRegion VALUES (1, 'Suite 203', geography::Parse('POLYGON((-7.17031 3.95601, -7.16786 3.95778, -7.17921 3.96874,
 -7.18441 3.96512, -7.17031 3.95601))'))

No comments:

Post a Comment