-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconvert_to_geojson.py
More file actions
29 lines (25 loc) · 854 Bytes
/
convert_to_geojson.py
File metadata and controls
29 lines (25 loc) · 854 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import shapefile
from json import dumps
import os
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parent
shape_path = Path(
os.getenv(
"RESPECT_PLANH_SHAPEFILE",
str(REPO_ROOT / "PLANH2316" / "PLANH2316.shp"),
)
)
output_path = Path(os.getenv("RESPECT_PLANH_GEOJSON_OUT", str(REPO_ROOT / "PLANH2316.geojson")))
# read the shapefile
reader = shapefile.Reader(str(shape_path))
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(output_path, "w")
geojson.write(dumps({"type": "FeatureCollection", "features": buffer}, indent=2) + "\n")
geojson.close()