这里有几个不同的部分:
选择分辨率:选择 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] || [])],
[]);
查看 Observable 的工作示例 https://observablehq.com/@nrabinowitz/h3-radius-lookup
Caveats:这是not真正的半径搜索。 K 形环是一个以原点为中心的大致六边形形状。这对于许多用例来说已经足够好了,并且比传统的半正弦半径搜索快得多,特别是当您有很多行需要搜索时。但是,如果您关心确切的距离,H3 可能不合适(或者,在某些情况下,H3 可能没问题,但您可能希望索引位于“真正的”圆内 - 这里的一个选择是将圆转换为闭合圆 -到圆形多边形,然后通过获取索引h3.polyfill
).