Skip to main content
5 of 6
edited body
gene
  • 55.4k
  • 3
  • 113
  • 191

You don't need Fiona to write a GeoJSON file

features = [feat for feat in fiona.open("a_shapefile.shp")
layer = {
     "type": "FeatureCollection",
      "features": features}
# save json file
import json #or GeoJSON
 with open('result.geojson', 'w') as outfile:
     json.dump(my_layer,outfile)

No CRS is explicitly defined inside the original GeoJSON format but you can add a crs in my_layer. The coordinate reference system must be in a special format (OGC CRS URN, see What's up with the GeoJSON spec and CRS as a IRM?) not compatible with the Fiona .crs (PROJ4 format)

  layer = {
     "type": "FeatureCollection",
      "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::31370" } },
       "features": features}

Or you can use PyGeoJ

import PygeoJ
newfile= pygeoj.new()
for feat in fiona.open('a_shapefile.shp"):
     newfile.add_feature(feat) 
# now you can add many parameters 
newfile.add_all_bboxes()
newfile.add_unique_id()
newfile.define_crs(type="link", link="http://spatialreference.org/ref/epsg/31370/esriwkt/", link_type="esriwkt")
newfile.save("test.geojson")
gene
  • 55.4k
  • 3
  • 113
  • 191