0

I try to write a geoJSON from a shp with the fiona package. Works, but fiona does not include the CRS information.

with fiona.open(path) as shp:
    takeCRS = shp.crs
    takeSCHEMA = shp.schema
    with fiona.open(str(newPath) + "someName.geojson", "w", driver = "GeoJSON", crs = takeCRS, schema = takeSCHEMA) as newFile:
        ....

Why is it leaving out the CRS information?!

2

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")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.