Skip to content

Instantly share code, notes, and snippets.

@lyleaf
Last active October 2, 2019 06:14
Show Gist options
  • Save lyleaf/8fcc082b9d03a9862717603311d6eaa5 to your computer and use it in GitHub Desktop.
Save lyleaf/8fcc082b9d03a9862717603311d6eaa5 to your computer and use it in GitHub Desktop.
Create geojson from shape file
import shapefile
# read the shapefile
reader = shapefile.Reader("hello.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("hello.json", "w")
geojson.write(dumps({"type": "FeatureCollection",\
"features": buffer}, indent=2) + "\n")
geojson.close()
features = geojson['features']
print(features)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment