Reputation: 333
I have a python list that looks like this:
type(route)
>>>list
print(route)
[{'type': 'LineString',
'coordinates': [[-94.586472, 39.098705],
[-64.586487, 39.098716],
[-64.585969, 39.094146],
[-64.586037, 39.093936],
[-64.586037, 39.093936],
[-64.586046, 39.093933]]},
{'type': 'LineString',
'coordinates': [[-94.581459, 39.093506],
[-64.581451, 39.09351],
[-64.581444, 39.093506],
[-64.581459, 39.093433],
[-64.581726, 39.093418],
[-64.588631, 39.087582]]},
{'type': 'LineString',
'coordinates': [[-94.584312, 39.042758],
[-64.584312, 39.042758],
[-64.583225, 39.099256],
[-64.584328, 39.09932]]}]
How can I convert this into a valid GeoJSON file? I've tried test = FeatureCollection(features=route)
, but that created an invalid file when I later dumped it.
Upvotes: 1
Views: 2892
Reputation: 4010
It looks like FeatureCollection
needs each item to be a type of Feature
which has a different schema than your current routes. The simplest solution is to use list comprehension to map each route into the Feature
schema.
def route_to_feature(idx, route):
return {
'type': 'Feature',
'geometry': route,
'properties': {
'name': f'Route #{idx}'
}
}
Which can be used like so.
geojson.FeatureCollection([
route_to_feature(i, route)
for i, route
in enumerate(routes)
])
Upvotes: 1