【问题标题】:Convert geopandas shapely polygon to geojson将geopandas形状多边形转换为geojson
【发布时间】:2021-09-18 16:52:09
【问题描述】:

我使用 geopandas 创建了一个圆,它返回了一个形状优美的多边形:

POLYGON: ((...))

我想要这个与 geojson 对象相同的多边形。我遇到了这个:

shapely.geometry.mapping(shapelyObject)

返回这个:

{'type': 'Polygon', 'coordinates': (((570909.9247264927, 125477.71811034005)...}

但是当我尝试在 mapbox 中映射它时,它没有显示任何内容。我想也许它不完全是一个 geojson 对象。

【问题讨论】:

标签: python geojson geopandas


【解决方案1】:

如果你不想手动创建这个dict,你也可以依赖geopandas创建它:

In [1]: import shapely.geometry

In [2]: import geopandas

In [3]: shapely_polygon = shapely.geometry.Polygon([(0, 0), (0, 1), (1, 0)])

In [4]: geopandas.GeoSeries([shapely_polygon]).__geo_interface__
Out[4]: 
{'bbox': (0.0, 0.0, 1.0, 1.0),
 'features': [{'bbox': (0.0, 0.0, 1.0, 1.0),
   'geometry': {'coordinates': (((0.0, 0.0),
      (0.0, 1.0),
      (1.0, 0.0),
      (0.0, 0.0)),),
    'type': 'Polygon'},
   'id': '0',
   'properties': {},
   'type': 'Feature'}],
 'type': 'FeatureCollection'}

(请注意,这给出了一个 FeatureCollection 而不是单个特征。)

或者到一个字符串(或文件):

In [4]: geopandas.GeoSeries([shapely_polygon]).to_json()
Out[4]: '{"features": [{"bbox": [0.0, 0.0, 1.0, 1.0], "geometry": {"coordinates": [[[0.0, 0.0], [0.0, 1.0], [1.0, 0.0], [0.0, 0.0]]], "type": "Polygon"}, "properties": {}, "id": "0", "type": "Feature"}], "bbox": [0.0, 0.0, 1.0, 1.0], "type": "FeatureCollection"}'

【讨论】:

  • 我明白了:AttributeError: 'Series' object has no attribute 'geo_interface'
【解决方案2】:

Shapely 返回一个 python 字典,其中所有坐标都在元组中。您需要转换为 JSON 以便 mapbox 等...正确接受它。

json.dumps(shapely.geometry.mapping(shapelyObject))

【讨论】:

    【解决方案3】:

    这样的事情应该可以解决问题:

    features = [{'type': 'Feature', 'properties': {}, 'geometry': shapely.geometry.mapping(shapelyObject)}]
    

    现在您可以尝试在 mapbox 中映射 features。 希望这会有所帮助。

    参考: https://gis.stackexchange.com/questions/213717/geometry-workflow-from-shapely-to-geojson

    【讨论】:

      【解决方案4】:

      要使用 pandas 编写标准 geojson 对象,您应使用fiona 提供的驱动程序,如documentation 中推荐的那样

      gdf.to_file('path/to/file.geojson', driver='GeoJSON')
      

      请参阅import fiona; fiona.supported_drivers 以获取完全支持的驱动程序列表

      【讨论】:

        【解决方案5】:

        你也可以使用PyShp

        import shapefile
        
        with shapefile.Reader("shapefile.shp") as shp:
            geojson_data = shp.__geo_interface__
        

        geojson_data = shapefile.Reader("shapefile.shp").__geo_interface__
        

        示例用法:

        >>> geojson_data["type"]
        
        'MultiPolygon'
        

        【讨论】:

          【解决方案6】:

          假设polygon_listshapely.geometry.Polygon 的列表。

          geo_dict = {}
          geo_dict["type"] = "FeatureCollection"
          geo_dict["features"] = [{"type": "Feature", "geometry": a} for a in [geometry.mapping(b) for b in polygon_list]]
          my_geojson = json.dumps(geo_dict) # str in json format
          

          【讨论】:

            【解决方案7】:

            使用fiona提供的驱动:

            data=shapefile.to_file("file.geojson",driver='GeoJSON')
            
            data=geopandas.read_file("file.geojson")
            
            data
            

            【讨论】:

            • 嗨,你能提供更多关于你的答案的细节吗?例如,您可以添加解决方案的工作原理、发布问题的用户缺少什么等。这将有助于将来到达该帖子的其他人更多地了解您的解决方案。谢谢
            • geopandas 使用 fiona 进行数据转换,fiona 有驱动程序可以这样做,例如从 geodataframe 创建 shapefile、geojson 等。如果您需要在 fiona 中破解驱动程序,只需键入 >import fiona >fiona.supported_drivers 结果应该是{'ESRI Shapefile':'raw','ARCGEN':'r','PCIDSK':'r','SUA':'r','DGN':'raw','SEGY':'r' , 'MapInfo 文件': 'raw', 'GeoJSON': 'rw', 'PDS': 'r', 'FileGDB': 'raw', 'GPX': 'raw', 'DXF': 'raw', 'GMT':'raw','Idrisi':'r','GPKG':'rw','OpenFileGDB':'r','BNA':'raw','AeronavFAA':'r','GPSTrackMaker ':'原始'}
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-08-27
            • 2020-07-16
            • 1970-01-01
            相关资源
            最近更新 更多