New member
- Joined
- Feb 14, 2023
- Messages
- 10
- Thread Author
- #1
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:
I read the data with the following code:
And with that code I can access the elements:
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:
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]]]
}
}
]
}
Code:
import json
with open('sf_example.geojson', "r") as read_file:
data = json.load(read_file)
Code:
data['features'][0]['properties']['name']
data['features'][0]['geometry']['coordinates'][0]
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