Site sponsored by: Idera Try Idera’s new SQL admin toolset
SQL Server Performance

  • Home
  • Articles
  • Forums
  • Tips
  • Quiz
  • FAQ's
  • Blogs
  • Software
  • Books
  • About Us
RSS Feeds
Sign in | Join


Article Topics

All Articles
Performance Tuning
Audit
Business Intelligence
Clustering
Reporting Services
Developer
General DBA
ASP.NET / ADO.NET

Write for Us

Share you SQL Server knowledge with others and raise your profile in the community More...
Latest Articles

Compare Dates
Filtered Indexes in SQL Server 2008
Importance of Database Backups and Recovery Plan
Data Compression in SQL Server 2008

More     
 
Latest FAQ's

ALTER TABLE SWITCH statement failed because the object '%.*ls' is not ...
ALTER TABLE SWITCH statement failed because column '%.*ls' at ordinal %d ...
ALTER TABLE SWITCH statement failed because table '%.*ls' has %d columns ...
SQL Server Reporting Server (SSRS) service is failing to start ...

More     
   
Latest Software Reviews

Spotlight on ApexSQL Doc 2008
ApexSQL Enforce
Embarcadero Change Manager
SQL Server DBA Dashboard

More     

articles >> developer >> New Data Types in SQL Server 2008 ...

New Data Types in SQL Server 2008 Part 3

By : Dinesh Asanka
Feb 01, 2008

Page 2 / 3

Polygon
Polygon is the two-dimensional surface which contains multiple points connected to each other. Polygon is a closed shape.

DECLARE @g geometry;
SET @g = geometry::Parse('POLYGON((0 0, 0 5, 5 5, 5 0, 0 0))')

As Polygon is a closed shape, you can see from the example above that the start and end points are the same. As a Polygon is a closed object, you can use all the function you used with LineString shapes. There are a few additional functions which you can also use for Polygon shape.

Method

Usage

Syntax

Result for Above Sample

STArea()

Area of the Polygon

Select @g.STArea()

25

STCentroid ()

Centre point of the Polygon

Select @g. STCentroid().ToString();

POINT (2.5,2.5)

SELECT @g.STArea();
SELECT @g.STCentroid().ToString();

MultiPoint MultiLineString MultiPolygon
Multipoint is collection of zero or more points.

DECLARE @g geometry;
SET @g = geometry::STMPointFromText('MULTIPOINT((10 20), (3 4 5))', 0);

If you want to extract only one point you can use the following method.

SELECT @g.STGeometryN(1).ToString();

MultiLineString and MultiPolygon are also as same as MultiPoint. The following examples give you an understanding of these functions.

SET @g = geometry::Parse('MULTILINESTRING((0 0, 2 1), (3 1 0, 1 1))');
SELECT @g.STGeometryN(2).ToString();
SET @g = geometry::Parse('MULTIPOLYGON(((0 0, 0 3, 3 3, 3 0, 0 0)),((9 9, 9 10, 10 9, 9 9)))');
SELECT @g.STGeometryN(1).ToString();

For overlapping polygons you may need to execute an additional method named MakeValid(). Let us illustrate this with three polygons.

((0 0, 0 3, 3 3, 3 0, 0 0)), ((1 1, 1 2, 2 1, 1 1)), ((9 9, 9 10, 10 9, 9 9)))

You can see that the second polygon falls inside the first polygon and this does not follow the OGC standards. If you run the following script an error will be returned.

DECLARE @g geometry;
SET @g = geometry::Parse('MULTIPOLYGON(((0 0, 0 3, 3 3, 3 0, 0 0)), ((1 1, 1 2, 2 1, 1 1)), ((9 9, 9 10, 10 9, 9 9)))');
SELECT @g.STGeometryN(2).STAsText();

The Error is;

System.ArgumentException: 24144: This operation cannot be completed because the instance is not valid. Use MakeValid to convert the instance to a valid instance. Note that MakeValid may cause the points of a geometry instance to shift slightly.

A an error has been specified, we need to use the MakeValid function. The MakeValid function will shift the first two polygons into one polygon.

DECLARE @g geometry;
SET @g = geometry::Parse('MULTIPOLYGON(((0 0, 0 3, 3 3, 3 0, 0 0)), ((1 1, 1 2, 2 1, 1 1)), ((9 9, 9 10, 10 9, 9 9)))');
If  @g.STIsValid() = 0
SET @g = @g.MakeValid();
SELECT @g.STGeometryN(2).STAsText();

The STisValid() method will verify whether the polygon follows the the OGC standards. If not MakeValid() method will convert the above polygon into a valid polygon.


<< Prev Page     Next Page>>    








Home | Peformance Articles | Audit Articles | Business Intelligence Articles | Clustering Articles | Developer Articles | Reporting Services Articles | DBA Articles | ASP.NET / ADO.NET Articles | DBA FAQ's | Developer Peformance FAQ's | DBA Peformance FAQ's | Developer FAQ's | Clustering FAQ's | Error Messages | Audit Tool Reviews | Backup Tool Reviews | Coding Tool Reviews | Compare Tool Reviews | Documentation Tool Reviews | Design Tool Reviews | Monitoring Tool Reviews | Log Tool Reviews | Reporting Tool Reviews | Clustering Tool Reviews | Security Tool Reviews | Change Management Tool Reviews | Remote Access Tool Reviews | Book Reviews | Security Tool Reviews | QDPMA Performance Tuning | ADO.NET / ASP.NET | Administration | Analysis/OLAP Services | Application Development | Configuration | Components | ETL | Hardware | High Availability | Hints | Index | Misc | Operating Systems | Performance Tuning | Replication | T-SQL | Views


              © 1999-2008 by T10 Media. All rights reserved