簡體   English   中英

如何使用 h3-js 在多邊形內找到點(lat,lng)?

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

我有一個用polyfill創建的多邊形,是否可以知道point = [lat, lng]是否在該多邊形中(僅使用 h3-js)?

是的,這是可能的,盡管涉及簡化 - polyfill返回其中心位於多邊形內的單元格,因此您將在多邊形的邊緣遇到邊緣附近的特定點將被錯誤地包含或排除在多邊形中的情況。 權衡是包含檢查應該比傳統的多點算法快得多,並且代碼更簡單:

// 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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM