Bing地图中的多边形区域

时间:2015-01-30 17:18:25

标签: c# bing-maps

嗨我在Bing地图中计算多边形面积时遇到问题。我使用此代码计算面积。

public static double PolygonArea(LocationCollection points, double resolution)
    {
        int n = points.Count;
        var partialSum = 0.0;
        var sum = 0.0;

        for (int i = 0; i < n - 1; i++)
        {
            partialSum = (points[i].Longitude * points[i + 1].Latitude) -
                (points[i + 1].Longitude * points[i].Latitude);
            sum += partialSum;
        }

        var area = 0.5 * sum / Math.Pow(resolution, 2);
        area = Math.Abs(area);
        return area;
    }

这是解决方法

public static double Resolution(double latitude, double zoomLevel)
  {                
      double groundResolution = Math.Cos(latitude * Math.PI / 180) *
       2 * Math.PI * EARTH_RADIUS_METERS / (256 * Math.Pow(2, zoomLevel));

            return groundResolution;
   }

我怎样才能在m ^ 2中进行转换?

EDIT1:我尝试了你的答案,但是如果我改变了缩放级别,我注意到区域会发生变化。

我试着从另一个角度解释我的问题。我必须移植使用此算法计算面积的iOS应用程序

-(long double)calcArea :(CLLocationCoordinate2D*) pastureCordinates :(long) count {
  long double area  = 0.0;
  long double scale = 0.0;

  for (int cnt = 1; cnt < count; cnt++) {
      area +=    (MKMapPointForCoordinate(pastureCordinates[cnt - 1]).x)
      * (MKMapPointForCoordinate(pastureCordinates[cnt]).y)
      - (MKMapPointForCoordinate(pastureCordinates[cnt]).x)
      * (MKMapPointForCoordinate(pastureCordinates[cnt - 1]).y);
  }

  area +=    (MKMapPointForCoordinate(pastureCordinates[count - 1]).x)
  * (MKMapPointForCoordinate(pastureCordinates[0]).y)
  - (MKMapPointForCoordinate(pastureCordinates[0]).x)
  * (MKMapPointForCoordinate(pastureCordinates[count - 1]).y);

  scale = MKMapPointsPerMeterAtLatitude(pastureCordinates[count -1].latitude);
  area  = (area / (long double)2) / pow(scale,2);

  area = fabsl(area);
  return area;

}

我使用了这里找到的函数:https://msdn.microsoft.com/en-us/library/bb259689.aspx来计算比例,地面分辨率,但结果与iOS解决方案相比有所不同。

2 个答案:

答案 0 :(得分:3)

好的,我已经玩了一些代码并组合了一个简单的方法,可以相当准确地计算区域,而无需使用真正深入的空间数学。

private double CalculateArea(LocationCollection locs)
{
    double area = 0;
    for (var i = 0; i < locs.Count - 1; i++)
    {
        area += Math.Atan(
            Math.Tan(Math.PI / 180 * (locs[i + 1].Longitude - locs[i].Longitude) / 2) *
            Math.Sin(Math.PI / 180 * (locs[i + 1].Latitude + locs[i].Latitude) / 2) /
            Math.Cos(Math.PI / 180 * (locs[i + 1].Latitude - locs[i].Latitude) / 2));
    }

    if (area < 0)
    {
        area *= -1;
    }

    return area * 2 * Math.Pow(6378137.0, 2);
}

使用各种多边形测试它并将它们与SQL中的计算区域进行比较,我发现在更糟糕的情况下,当使用可笑的大多边形时差异大约为0.673%。当测试大小约为0.5平方公里的多边形时,差异约为0.06%。请注意,此方法返回以平方米为单位的区域。

答案 1 :(得分:0)

计算地图上多边形的面积非常复杂,因为世界是一个球体,而您实际上是在尝试计算在球体表面上拉伸的多边形的面积。由于您使用的是WPF,我建议您轻松使用SQL Server中可用的空间库。 SQL Server中的所有空间功能都可以作为dll使用,您可以在WPF应用程序中使用它。您可以轻松地使用此库来准确计算多边形的面积,并执行许多其他非常强大的功能。首先,如果您有SQL instelled,您可以找到位于C:\ Program Files(x86)\ Microsoft SQL Server \ 110 \ Shared目录中的SQL Spatial Library(Microsoft.SqlServer.Types)。如果您没有安装SQL Server,请不要担心,您不必安装它,此库可在此处以Nuget包的形式提供:https://www.nuget.org/packages/Microsoft.SqlServer.Types

使用.NET中的SQL空间工具了解这个动手实验室的信息:http://view.officeapps.live.com/op/view.aspx?src=http%3A%2F%2Fecn.channel9.msdn.com%2Fo9%2Flearn%2FSQL2008R2TrainingKit%2FLabs%2FUsingSpatialDataInManagedCode%2FLab.docx

拥有此库后,您可以从多边形创建SQL Geography对象。完成此操作后,您可以使用STArea方法计算多边形的面积。还有很多其他空间方法可用于创建一个非常强大的地图应用程序。

相关问题