Skip to content

Instantly share code, notes, and snippets.

@morkapronczay
Created May 7, 2019 14:04
Show Gist options
  • Save morkapronczay/a1c5397af454f4e9b50808f3e7aca596 to your computer and use it in GitHub Desktop.
Save morkapronczay/a1c5397af454f4e9b50808f3e7aca596 to your computer and use it in GitHub Desktop.
def json_to_geojson(data, districts):
# create a geojson from a list of dictionaries
# containing coordinates with the name of the polygon
# in our case a polygon is a district
assert type(data) == list, "The parameter data should be a list of coordinates with a name argument!"
geojson = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry" : {
"type": "Polygon",
"name": district,
"coordinates": [[[d["lon"], d["lat"]] for d in data if d['name'] == district]],
},
"properties" : {'name': district},
} for district in districts]
}
return geojson
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment