Skip to content

Instantly share code, notes, and snippets.

@juan-fdz-hawa
Forked from frankrowe/shp2gj.py
Created November 19, 2016 12:04
Show Gist options
  • Save juan-fdz-hawa/bbd0bdaddb5d7c48dd68d47bdcac6902 to your computer and use it in GitHub Desktop.
Save juan-fdz-hawa/bbd0bdaddb5d7c48dd68d47bdcac6902 to your computer and use it in GitHub Desktop.
PyShp, shp to geojson in python
import shapefile
# read the shapefile
reader = shapefile.Reader("my.shp")
fields = reader.fields[1:]
field_names = [field[0] for field in fields]
buffer = []
for sr in reader.shapeRecords():
atr = dict(zip(field_names, sr.record))
geom = sr.shape.__geo_interface__
buffer.append(dict(type="Feature", \
geometry=geom, properties=atr))
# write the GeoJSON file
from json import dumps
geojson = open("pyshp-demo.json", "w")
geojson.write(dumps({"type": "FeatureCollection",\
"features": buffer}, indent=2) + "\n")
geojson.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment