javascript - 使用javascript使用经度和纬度计算多边形的面积

标签 javascript geometry polygon area

我一直在研究的方法是实现正弦投影来获取 x,y 坐标,然后使用函数计算平面中不规则多边形的面积。 下面是我一直在处理的代码(points 变量是在程序中其他地方定义的铯笛卡尔点数组)。

https://stackoverflow.com/a/4682656/7924630这是一个非常有用的答案,帮助我解决了这个问题

function polygonArea(X, Y, numPoints) {
  let area = 0;         // Accumulates area in the loop
  let j = numPoints-1;  // The last vertex is the 'previous' one to the first

  for (i=0; i<numPoints; i++) {
    area = area +  (X[j]+X[i]) * (Y[j]-Y[i]);
    j = i;  //j is previous vertex to i
  }
  return area/2;
}


let xpoints = [];
let ypoints = [];
let lat_dist = (6371009 * Math.PI) / 180;
var i;
for (i = 0; i < points.length; i++) {
    let cartoPoint = Cesium.Cartographic.fromCartesian(points[i]);
    let lng = cartoPoint.longitude;
    let lat = cartoPoint.latitude;
    xpoints[i] = lng * lat_dist * Math.cos(lat);
    ypoints[i] = lat * lat_dist;
};
surfaceArea = polygonArea(xpoints, ypoints, xpoints.length);

出于某种原因,这会返回该区域的非常小的值,我不明白为什么。例如,我在矩形区域上对此进行了测试。该面积应约为 45 平方米,但返回 0.0137 平方米。我尝试过遵循此方法的其他实现,但未能找到任何对 native Javascript 有用的内容。

最佳答案

您正在使用shoelace formula对于多边形区域,这里给出了球面多边形的近似值。最后添加 Math/abs 以独立于遍历方向获得结果,并注意赤道和第 0 条经线的坐标符号变化。

但是由于这个问题,计算不正确:

Cesium.Cartographic.fromCartesian 函数返回结果坐标 in radians

当您将它们视为度时(快速检查:45/(57*57)=0.0138)。

因此进行修正就足够了:

 let lat_dist = 6371009;

关于javascript - 使用javascript使用经度和纬度计算多边形的面积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53604117/

相关文章:

c# - 如何计算多边形的圆角?

javascript - D3 条形图列溢出

javascript - 如何在 Vue3 组合 API 上使用 Vue2 插件?

javascript - 谷歌地图 : Finding polygon by Id

javascript - 找到 map 未成形多边形的中心点

c - 在哪里可以找到最小边界框算法的 c/c++ 实现?

javascript - "Add to favorites"在 Opera 中使用 JavaScript

javascript - Bower:警告:EPERM。 Grunt 无法完成操作

html - 通过 CSS 显示为圆形时,DIV 背景颜色小于 div 大小

geometry - 如何获取旋转矩形的大小