Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DbGeography polygon get points

I have a Polygon persisted on a SQL Server 2012 database as Sys.Geography type. How can I obtain all points for the Polygon?

I'm thinking to use AsText() method and parse the string, but maybe there is a better choice?

like image 727
Alexandre Avatar asked Aug 19 '14 00:08

Alexandre


2 Answers

Found a way, here is an extension method:

public static IEnumerable<MyEntityWithLatAndLng> GetPointsFromPolygon(this System.Data.Entity.Spatial.DbGeography geo)
{
   for (int i = 1; i < geo.PointCount; i++)
   {
     var p = geo.PointAt(i);
     yield return new MyEntityWithLatAndLng(){ Latitude = p.Latitude.Value, Longitude = p.Longitude.Value };
  }
}
like image 65
Alexandre Avatar answered Oct 07 '22 01:10

Alexandre


I think Alexandre nearly has this correct, he is missing the last element of the polygon from the points list. See the updated code below.

public static IEnumerable<MyEntityWithLatAndLng> GetPointsFromPolygon(this System.Data.Entity.Spatial.DbGeography geo)
{
   for (int i = 1; i <= geo.PointCount; i++)
   {
      var p = geo.PointAt(i);
      yield return new MyEntityWithLatAndLng(){ Latitude = p.Latitude.Value, Longitude = p.Longitude.Value };
   }
}
like image 30
Darth Coder Avatar answered Oct 07 '22 03:10

Darth Coder