8

Suppose I have a string that contains raw unparsed GeoJSON data. For example:

geojson_str = '''{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "id": 1, "test": "a" }, "geometry": { "type": "Point", "coordinates": [ 3.0, 7.0 ] } },
{ "type": "Feature", "properties": { "id": 2, "test": "b" }, "geometry": { "type": "Point", "coordinates": [ 5.0, 5.0 ] } },
{ "type": "Feature", "properties": { "id": 3, "test": "c" }, "geometry": { "type": "Point", "coordinates": [ 7.0, 3.0 ] } }
]
}
'''

How can I read the data above into a GeoDataFrame?

Felipe D.
  • 2,509
  • 2
  • 14
  • 32
  • 1
    I know this seems like a small silly post, but I didn't find this info being stated anywhere else, so I just wanted to document it here. – Felipe D. Jan 04 '22 at 16:35

1 Answers1

12

You can load that string into a GeoDataFrame using the read_file method:

import geopandas as gpd
gdf = gpd.read_file(geojson_str, driver='GeoJSON')

In the code above, gdf is a CRS-aware GeoDataFrame that you can manipulate however you want.

Credit

Special credit to @tynowell for highlighting that this is possible in an unrelated thread.

Felipe D.
  • 2,509
  • 2
  • 14
  • 32