代码之家  ›  专栏  ›  技术社区  ›  kms

将几何体从边界转换为多边形

  •  0
  • kms  · 技术社区  · 3 年前

    我有一个带有边界几何图形的geopandas DataFrame。

    import pandas as pd
    import geopandas as gpd
    
    gdf = gpd.GeoDataFrame({
                            'id': [0, 1],
                            'b': [((40.6494140625, -86.7919921875), (40.69335937...)), 
                                  ((39.55078125, -93.8232421875), (39.5947265625...))]
                          })
    
    gdf['b'][0]
    
    Bounds(sw=SouthWest(lat=32.8271484375, lon=-96.8115234375), ne=NorthEast(lat=32.87109375, lon=-96.767578125))
    
    print(type(gdf['b'][0]))
    
    <class 'geolib.geohash.Bounds'>
    

    我该怎么转弯 Bounds 进入 Polygon 几何类型?喜欢

    Polygon((40.6494140625, -86.7919921875), (40.69335937...))
    
    0 回复  |  直到 3 年前
        1
  •  2
  •   swatchai    3 年前

    假设您有:-

    # bound1 = The geohash.Bounds object.
    

    您可以继续:-

    from shapely.geometry import box
    bounds_pgon = box(bounds1.sw.lon, bounds1.sw.lat,
                      bounds1.ne.lon, bounds1.ne.lat)
    # Check the result
    bounds_pgon.wkt
    

    输出将类似于此:-

    'POLYGON ((-27.9986 70.2987, -27.9986 70.3001, -28.0000 70.3001, -28.0000 70.2987, -27.9986 70.2987))'
        2
  •  1
  •   Rob Raymond    3 年前

    这确实是@swatchi提供的答案。

    几何体的形状由哈希的精度定义。参见参考资料: geohash

    import geolib.geohash
    import shapely.geometry
    import geopandas as gpd
    import pandas as pd
    
    # Bounds(sw=SouthWest(lat=32.8271484375, lon=-96.8115234375), ne=NorthEast(lat=32.87109375, lon=-96.767578125))
    # regenerate the referenced geohad bounds
    b = geolib.geohash.bounds(
        geolib.geohash.encode(lat=32.8271484375, lon=-96.8115234375, precision=5)
    )
    
    print(b)
    
    gdf = gpd.GeoDataFrame(
        pd.DataFrame({"id": [0], "b": [b]}).assign(
            geometry=lambda d: d["b"].apply(
                lambda b: shapely.geometry.box(b.sw.lon, b.sw.lat, b.ne.lon, b.ne.lat)
            )
        ),
        crs="epsg:4326",
    )
    
    gdf.explore(height=300, width=300)
    

    输出

    Bounds(sw=SouthWest(lat=32.8271484375, lon=-96.8115234375), ne=NorthEast(lat=32.87109375, lon=-96.767578125))
    

    enter image description here