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?
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 };
}
}
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 };
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With