Skip to content

Instantly share code, notes, and snippets.

@hcarvalhoalves
Created June 13, 2024 18:30
Show Gist options
  • Save hcarvalhoalves/8769bdf074e8214361f8b481f8ec6740 to your computer and use it in GitHub Desktop.
Save hcarvalhoalves/8769bdf074e8214361f8b481f8ec6740 to your computer and use it in GitHub Desktop.
# Exemplo usando a biblioteca https://pypi.org/project/pyshp/1.1.7/
import shapefile
from json import dumps
# 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
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