如何在 h3-js 中的 h3 索引的 5 公里半径范围内找到位置(其经纬度坐标以 geo-json 格式存储的索引)?

How to find the locations (indices whose lat long co-ordinates are stored in geo-json format) within 5 Km radius of a h3 index in h3-js?

我正在创建一个超级本地交付服务应用程序。只有在距离用户 5 公里范围内有商店的情况下,我才能收到订单。我以 geojson 格式存储商店位置。 h3-js 中是否有一个函数将采用半径、商店数组、h3 索引,然后返回距离给定 h3 索引 5 公里范围内的商店列表。或者我如何使用 h3-js 实现它?

这里有几个不同的部分:

选择一个分辨率:选择一个 H3 分辨率进行查找。更好的分辨率意味着更高的准确性但更多的内存使用。 Res 8 大约是几个城市街区的大小。

索引数据: 要使用 H3 进行半径查找,您需要通过 H3 索引对商店进行索引。如果您希望这样做高效,最好提前为所有商店编制索引。你如何做到这一点取决于你;在 JS 中一个简单的方法可能是创建一个 id 数组映射:

const lookupIndexes = stores.features.reduce((map, feature) => {
  const [lon, lat] = feature.geometry.coordinates;
  const h3Index = h3.geoToH3(lat, lon, res);
  if (!map[h3Index]) map[h3Index] = [];
  map[h3Index].push(feature.id);
  return map;
}, {})

执行查找:要搜索,请为您的搜索位置编制索引,并获取某个半径内的所有 H3 索引。您可以使用 h3.edgeLength 函数获取当前分辨率下单元格的近似半径。

const origin = h3.geoToH3(searchLocation.lat, searchLocation.lon, res);
const radius = kmToRadius(searchRadiusKm, res);

// Find all the H3 indexes to search
const lookupIndexes = h3.kRing(origin, radius);

// Find all points of interest in those indexes
const results = lookupIndexes.reduce(
  (output, h3Index) => [...output, ...(lookupMap[h3Index] || [])], 
[]);

See a working example on Observable

注意事项:这是不是真正的半径搜索。 k-ring 是一个以原点为中心的大致六边形。这对于许多用例来说已经足够好了,并且比传统的 Haversine 半径搜索快得多,尤其是当您有很多行要搜索时。但是如果你关心确切的距离 H3 可能不合适(或者,在某些情况下,H3 可能没问题,但你可能希望索引在 "true" 圆内 - 这里的一个选择是将你的圆转换为close-to-circular 多边形,然后通过 h3.polyfill).

获取索引