Skip to main content
 首页 » 编程设计

c#之Bing map 中的多边形区域

2024年08月06日40zfyouxi

您好,我在计算 Bing map 中多边形的面积时遇到问题。我正在使用此代码来计算面积。

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 解决方案不同。

请您参考如下方法:

好吧,我尝试了一些代码并整理了一个简单的方法来相当准确地计算面积,而无需使用非常深入的空间数学。

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%。请注意,此方法返回以平方米为单位的面积。