DbGeography Reverse Polygon Point Order (Ring Orientation) With Entity Framework 5, WKTString and MSSQL 2008

If you draw a polygon using Google maps in a clock wise direction and then try to create a DbGeography object from the Well Known Text String (WKTString) it will create a polygon that covers the area of the earth EXCEPT the area you drew on the map.

To reverse the Polygon Point Order (Ring Orientation), you can use the following code:

using Microsoft.SqlServer.Types;
using System;
using System.Collections.Generic;
using System.Data.Spatial;
using System.Data.SqlTypes;

namespace SomeNamespace
{
 public static class DbGeographyHelper
 {

// 4326 is most common coordinate system used by GPS/Maps
 // 4326 format puts LONGITUDE first then LATITUDE
 private static int _coordinateSystem = 4326;

public static DbGeography CreatePolygon(string wktString)
 {
 var sqlGeography =
 SqlGeography.STGeomFromText(new SqlChars(wktString), _coordinateSystem)
 .MakeValid();

var invertedSqlGeography = sqlGeography.ReorientObject();
 if (sqlGeography.STArea() > invertedSqlGeography.STArea())
 {
 sqlGeography = invertedSqlGeography;
 }

return DbSpatialServices.Default.GeographyFromProviderValue(sqlGeography);
 }

}
}

I got the idea from this great post

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s