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

从GeoJSON对象创建GeoDataFrame

  •  15
  • kwn  · 技术社区  · 9 年前

    我有一个多边形的Feature Collection,我必须先将其写入一个临时文件,然后用它加载 geopandas.GeoDataFrame.from_file(tmp_json_file) ,有没有办法不写临时文件,只创建 GeoDataFrame 来自 GeoJSON 对象

    1 回复  |  直到 5 年前
        1
  •  25
  •   joris    9 年前

    您可以使用 GeoDataFrame.from_features() 功能。一个小例子(假设您有一个geojson FeatureCollection):

    In [1]: from geojson import Feature, Point, FeatureCollection
    
    In [2]: my_feature = Feature(geometry=Point((1.6432, -19.123)), properties={"country": "Spain"})
    
    In [3]: my_other_feature = Feature(geometry=Point((-80.234, -22.532)), properties={'country': 'Brazil'})
    
    In [4]: collection = FeatureCollection([my_feature, my_other_feature])
    
    In [6]: import geopandas
    
    In [7]: geopandas.GeoDataFrame.from_features(collection['features'])
    Out[7]:
      country                 geometry
    0   Spain   POINT (1.6432 -19.123)
    1  Brazil  POINT (-80.234 -22.532)