我有一组 GeoJSON 格式的多边形,例如:
{
"type": "MultiPolygon",
"coordinates": [
[[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]],
[[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]],
[[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]]
]
}
我正在尝试使用 H3 库中的
polyfill_geojson
方法来获取落在其中的六边形。但这个方法似乎只支持多边形而不支持多重多边形:
>>> h3.polyfill_geojson(geojson, 8)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "python3.7/site-packages/h3/api/_api_template.py", line 486, in polyfill_geojson
mv = _cy.polyfill_geojson(geojson, res)
File "geo.pyx", line 186, in h3._cy.geo.polyfill_geojson
ValueError: Only Polygon GeoJSON supported
如何从 Multipolygon GeoJSON 中获取 H3 六边形?
简单....这将为您提供一个集合列表(您可以轻松加入)。
[h3.polyfill(shapely.geometry.mapping(x)) for x in list(shapely.geometry.loads(geojson))]
显然,h3.polyfill 之类的函数尚不支持多重多边形。
我建议您以编程方式将多边形与多重多边形分开,并评估这些单个多边形中每一个的 polyfill 函数。
或者,如果您正在处理少量多边形,请使用 https://www.keene.edu/campus/maps/tool/ 工具创建与多重多边形匹配的多边形。
抱歉,如果这没有太大帮助。
这里可能有帮助的功能是 Mosaic 库中的
mosaicfill
,它是 Apache Spark 框架的扩展,可以轻松快速地处理非常大的地理空间数据集 https://databrickslabs.github.io/mosaic/
https://databrickslabs.github.io/mosaic/api/spatial-functions.html?highlight=mosaic_fill#mosaicfill
与原生 H3 实现不同,Mosaic 包装函数确实可以处理多种几何图形
您可以考虑使用 geopandas.GeoDataFrame.explode 将多边形转换为多边形。
之后,你可能可以解决剩下的问题(并且做得更优雅!),但我这样做了:
hexagons = [] for index, row in geofence.iterrows(): hexagons.extend(h3.polyfill(row['geometry'].__geo_interface__, h3resolution, geo_json_conformant=True))
对于任何使用 H3-js 的人
灵感:https://github.com/vasturiano/ Three-globe/blob/7717c8e54c98dcb3e92df5a95923f51387a496a1/src/layers/hexedPolygons.js#L110
const hexagons = []
features.forEach(feature=> {
switch (feature.geometry.type) {
case 'Polygon':
h3.polygonToCells(features.geometry.coordinates, resolution, true).forEach(h3Idx => hexagons.push(h3.cellToBoundary(h3Idx, true).reverse()));
break;
case 'MultiPolygon':
feature.geometry.coordinates.forEach(coords =>
h3.polygonToCells(coords, resolution, true).forEach(h3Idx => hexagons.push(h3.cellToBoundary(h3Idx, true).reverse()))
);
break;
default:
console.warn(`Unsupported geometry type: ${features.geometry.type}. Skipping this feature`);
break;
}
})
const geojson = {
type: 'FeatureCollection',
features: hexagons.map(hexagon => ({
type: 'Feature',
geometry: {
type: 'Polygon',
coordinates: [hexagon]
},
properties: {}
}))
};