pyshp icon indicating copy to clipboard operation
pyshp copied to clipboard

Add read geojson capability

Open ncgl-syngenta opened this issue 3 years ago • 1 comments

Describe the feature request

Looking for the ability to easily read a geojson file into the Reader.

Contributions

  • [ ] I am interested in implementing the described feature request and submit as a PR.

ncgl-syngenta avatar Jun 13 '22 12:06 ncgl-syngenta

Directly reading the contents of a geojson file is a bit outside the scope of this library, but there are convenience methods for writing shapes from geojson dicts, which can then be opened with a Reader. Perhaps something like this:

import shapefile
import json

geoj = json.loads(open('fromfile.geojson').read())
w = shapefile.Writer('tofile.shp')
for feat in geoj['features']:
    w.shape(feat['geometry'])
w.close()

r = shapefile.Reader('tofile.shp')

Some more work required to add fields and records, and detecting the correct field types, depending on your use-case.

karimbahgat avatar Jan 10 '23 23:01 karimbahgat