DbGeography多边形获得点

Ale*_*dre 5 entity-framework geospatial

我有一个Polygon持久化在SQL Server 2012数据库上作为Sys.Geography类型.如何获得Polygon的所有点?

我想使用AsText()方法并解析字符串,但也许有更好的选择?

Ale*_*dre 7

找到了办法,这里有一个扩展方法:

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 };
  }
}
Run Code Online (Sandbox Code Playgroud)


小智 5

我认为亚历山大几乎有这个正确,他从点列表中缺少多边形的最后一个元素.请参阅下面的更新代码.

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 };
   }
}
Run Code Online (Sandbox Code Playgroud)