Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

How to extract nested list data from a GeoJSON file in each key into dataframe

New member
Joined
Feb 14, 2023
Messages
10
I have a GeoJSON file with multiple keys. I want to extract the name and coordinates. I can access each of the elements, but I do not know how to loop over them to add them into a dataframe or another appropriate format maybe like dict. I want the name with the corresponding coordinates (lat,lon). The final use of the dataframe or dict will be to plot it with Plotly.

The GeoJSON looks like this:

Code:
{
    "type": "FeatureCollection",
    "name": "sf_example",
    "crs": {
        "type": "name",
        "properties": {
            "name": "urn:ogc:def:crs:OGC:1.3:CRS84"
        }
    },
    "features": [{
            "type": "Feature",
            "properties": {
                "name": "FirstRoad"
            },
            "geometry": {
                "type": "MultiLineString",
                "coordinates": [[[-0.209607278927995, 51.516851589085569], [-0.209607278927995, 51.516851589085569], [-0.210671042775843, 51.51666770991379], [-0.217526409795308, 51.515064252076257]]]
            }
        }, {
            "type": "Feature",
            "properties": {
                "name": "SecondRoad"
            },
            "geometry": {
                "type": "MultiLineString",
                "coordinates": [[[-0.208969020619286, 51.516005738748845], [-0.208969020619286, 51.516005738748845], [-0.211096548314982, 51.512688382789257]]]
            }
        }, {
            "type": "Feature",
            "properties": {
                "name": "ThirdRoad"
            },
            "geometry": {
                "type": "MultiLineString",
                "coordinates": [[[-0.204725784826204, 51.517020757267986], [-0.204725784826204, 51.517020757267986], [-0.207798880386654, 51.517211990108905]]]
            }
        }
    ]
}
I read the data with the following code:
Code:
import json
with open('sf_example.geojson', "r") as read_file:
    data = json.load(read_file)
And with that code I can access the elements:

Code:
data['features'][0]['properties']['name']
data['features'][0]['geometry']['coordinates'][0]
How do I loop so that all information will be extracted? What I want as a result is a dataframe or dict like this for example:

Code:
name,lat,lon
FirstRoad,-0.209607278927995, 51.516851589085569,
FirstRoad,-0.209607278927995, 51.516851589085569,
FirstRoad,-0.210671042775843, 51.51666770991379,
FirstRoad,-0.217526409795308, 51.515064252076257,
SecondRoad,-0.208969020619286, 51.516005738748845,
SecondRoad,-0.208969020619286, 51.516005738748845,
SecondRoad,-0.211096548314982, 51.512688382789257
 
Top