Turf js绘制具有中心点和半径的完美正方形

Turf js drawing perfect square with center point and radius

我正在尝试根据中心点和半径生成 p 个正方形多边形。如下图。

bboxPolygon(square(bbox(circle(_circle.center, 0.5, { steps: 64 }))))

所有函数来自turf.js

我相信应该生成完美的正方形或至少接近正方形。但是,它 returns 是矩形的。

我不确定这是 turf 库的问题还是我用错了。

圆形geojson

{
  "type": "Feature",
  "properties": {},
  "geometry": {
    "type": "Polygon",
    "coordinates": [
      [
        [
          -73.93524199999999,
          40.734656941636764
        ],
        [
          -73.93791238162646,
          40.73411472349626
        ],
        [
          -73.93986713367875,
          40.73263337851494
        ],
        [
          -73.94058248193825,
          40.730609876934174
        ],
        [
          -73.93986685239045,
          40.72858643688632
        ],
        [
          -73.93791210033818,
          40.72710521497083
        ],
        [
          -73.93524199999999,
          40.72656305836324
        ],
        [
          -73.93257189966181,
          40.72710521497083
        ],
        [
          -73.93061714760952,
          40.72858643688632
        ],
        [
          -73.92990151806174,
          40.730609876934174
        ],
        [
          -73.93061686632124,
          40.73263337851494
        ],
        [
          -73.93257161837353,
          40.73411472349626
        ],
        [
          -73.93524199999999,
          40.734656941636764
        ]
      ]
    ]
  }
}

bboxPolygon 结果

{
  "type": "Feature",
  "bbox": [
    -73.93928894163675,
    40.72656305836324,
    -73.93119505836323,
    40.734656941636764
  ],
  "properties": {},
  "geometry": {
    "type": "Polygon",
    "coordinates": [
      [
        [
          -73.93928894163675,
          40.72656305836324
        ],
        [
          -73.93119505836323,
          40.72656305836324
        ],
        [
          -73.93119505836323,
          40.734656941636764
        ],
        [
          -73.93928894163675,
          40.734656941636764
        ],
        [
          -73.93928894163675,
          40.72656305836324
        ]
      ]
    ]
  }
}

我认为问题出在 turf.square 函数上。它没有按照我的预期进行。

如果你更换,你应该得到非常接近正方形的东西 bboxPolygon(square(bbox(circle(_circle.center, 0.5, { steps: 64 }))))

bboxPolygon(bbox(circle(_circle.center, 0.5, { steps: 64 })))

这是输出的示例。 red 是圆,green 是使用正方形得到的输出,blue 是不使用正方形。

如果你查看 squarethe source code,你会注意到它实际上并没有创建一个两边距离相等的 bbox。然而,它创建了一个具有相同程度变化的 bbox。由于我们在经度和纬度上工作,因此通常不会是距离为正方形的多边形。

我不确定为什么有人会需要这个 square 函数,因为如果它的距离是正方形,我会发现它更有用,但是我不知道这是否是预期的行为。

TLDR 尝试 bboxPolygon(bbox(circle(_circle.center, 0.5, { steps: 64 }))) 而不是

@the_cheff 的上述回答非常低效,因为它迭代了 64 个点来计算一个矩形。另一种解决方案是类似于 Turf 实现 turf-circle:

的实现
const square = (center: [number, number], radius: number): GeoJSON.Feature<GeoJSON.Geometry> => {
   const cross = Math.sqrt(2 * radius ** 2);
   const coordinates = [];
   
   for (let i = 0; i < 4; i++) {
    coordinates.push(destination(center, cross, i * -360 / 4 + 45, {}).geometry.coordinates);
   }
   coordinates.push(coordinates[0]);

   return polygon([coordinates], {});
}

希望这对以后的任何人都有帮助。