如何有效地将一组 geohashes 转换为多边形?

How to efficiently convert a set of geohashes into a polygon?

我一直在寻找一种有效的方法来将一组 geohashes 转换为 Python 中的多边形,此外有时我会获得多边形而不是多边形,这可能是因为缺少一些内部 geohashes。我目前正在使用 python-geohash and shapely,我的方法包括以下步骤:

  1. 我通过提取其角坐标将每个 geohash 转换为多边形。

    def to_polygon(geohash):
        box = Geohash.bbox(geohash)
        return Polygon([(box['e'], box['n']), (box['w'], box['n']), (box['w'], box['s']), (box['e'], box['s'])])
    
  2. 然后我 map 在 geohashes 上 iterable 执行前面解释的转换。

    polygons = [to_polygon(geohash) for geohash in geohashes]
    
  3. 最后,我使用多边形的方法 union.

    将所有这些多边形合并为一个多边形
    polygon = functools.reduce(lambda a, b: a.union(b), polygons)
    

如果一组 geohashes 大约是几千,可能需要几分钟。

使用geohashlite

# GeoHash to GeoJSON
converter_1 = geohashlite.GeoJsonHasher()
x = ['u09k', 'u095', 'u08g', 'u09h', 'u09e', 'u097']
converter_1.geohash_codes = x
converter_1.decode_geohash(multipolygon=True)
print(converter_1.geojson)

我创建了一个库 (polygon-geohasher) 来实现这个:

from polygon_geohasher.polygon_geohasher import geohashes_to_polygon

geohashes = ['9bc1db2',
             '9bc1db6', 
             '9bc1db1', 
             '9bc1db0',
             '9bc1db4',
             '9bc1db9', 
             '9bc1db8',
             '9bc1dbd', 
             '9bc1db3']
polygon = geohashes_to_polygon(geohashes)
print(polygon) 
# POLYGON ((-99.71878051757812 4.483795166015625, -99.71878051757812 4.482421875, -99.72015380859375 4.482421875, -99.72152709960938 4.482421875, -99.722900390625 4.482421875, -99.722900390625 4.483795166015625, -99.722900390625 4.48516845703125, -99.722900390625 4.486541748046875, -99.72152709960938 4.486541748046875, -99.72015380859375 4.486541748046875, -99.71878051757812 4.486541748046875, -99.71878051757812 4.48516845703125, -99.71878051757812 4.483795166015625))