17

I have a Feature Collection of polygons and I have to first write it in a temporary file to then load it with geopandas.GeoDataFrame.from_file(tmp_json_file), is there any way to not write the temporary file and to just create the GeoDataFrame from the GeoJSON object ?

Georgy
  • 12,464
  • 7
  • 65
  • 73
kwn
  • 909
  • 2
  • 13
  • 25

1 Answers1

29

You can use the GeoDataFrame.from_features() function for this. A small example (supposing you have a 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)
joris
  • 133,120
  • 36
  • 247
  • 202
  • It works like a charm for simple polygons, but I have another `FeatureColletion` composed of `Polygons` and `MultiPolygons` and I don't know how to handle the geometry of it – kwn Jun 10 '16 at 09:29
  • 1
    What do you mean with "how to handle the geometry of it"? Does it fail to create a geopandas GeoDataFrame? (as those can hold a geometry column with different geometry types, so that shouldn't be a problem) – joris Jun 10 '16 at 09:56
  • Can you open a separate question for this? (as it is something else, about how to create a FeatureCollection) – joris Jun 10 '16 at 10:59