如何使用 h3-js 在多边形内找到点(lat,lng)?

How to find point (lat, lng) inside a polygon with h3-js?

我有一个用 polyfill 创建的多边形,是否可以知道 point = [lat, lng] 是否在该多边形中(仅使用 h3-js)?

是的,这是可能的,尽管涉及简化 - polyfill returns 中心落在多边形内的单元格,因此您会在多边形的边缘遇到特定点附近的情况边缘将错误地包含在多边形中或从多边形中排除。权衡是包含检查应该比传统的 point-in-poly 算法快得多,并且代码更简单:

// Create a set of H3 indexes representing the polygon. Using a finer
// value for H3_RES will give you better fidelity to the original 
// polygon, at the cost of more memory usage 
const polygonSet = new Set(h3.polyfill(myPolygon, H3_RES));

// Check whether a given point is in the polygon
function isPointInPoly(point) {
  const h3Index = h3.geoToH3(point.lat, point.lng, H3_RES);
  return polygonSet.has(h3Index);
}