[GIS] How to write a multi geometries .geojson file with Shapely/Fiona

fionageojsonmulti-geometrypythonshapely

I am very new to fiona and shapely, and trying to do something that doesn't seems to be designed : I have a list of points and a shapely.MultiPolygon (Voronoi polygons from the previous list of points), and I would like to write a single geojson file that would contain both. But from what I saw, fiona.open uses a single schema, with a defined geometry type.

So is it possible to achieve that, using fiona and shapely ?


Note : I managed to make it work using shapely and the geojson package, but my question scope is focused on shapely & fiona. If it helps anyone, here is how I did it :

features = []
features.append(geojson.Feature(geometry=my_shapely_multi_polygon, properties={}))
features.append(geojson.Feature(geometry=my_shapely_multi_point, properties={}))
feature_collection = geojson.FeatureCollection(features)

with open('myfile.geojson', 'w+', encoding='utf-8') as geojson_file :
  geojson.dump(feature_collection, geojson_file)

Best Answer

import fiona
import json

shp = fiona.open(r"some_shapes.shp")

geojson = { "type": "FeatureCollection",
            "features": [] }

for f in shp:
    geojson["features"].append(f)

with open(r"some_shapes.geojson", "w") as gjs:
    json.dump(geojson, gjs)

with the script above opens a shapefile with fiona. Then adds the fiona-json-like features f to the list inside my geojson dict. Last it converts und write the structure to disk.