-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpfisd_extract_shapefiles.py
More file actions
241 lines (188 loc) · 8.49 KB
/
pfisd_extract_shapefiles.py
File metadata and controls
241 lines (188 loc) · 8.49 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
"""
pfisd_extract_shapefiles.py
Extracts all layers from the Pflugerville ISD Attendance Boundaries
ArcGIS Experience Builder app and saves them as shapefiles.
Source: https://experience.arcgis.com/experience/0bc78994af534cd1a703c8959abeac9d
WebMap: https://Pflugervilleisd.maps.arcgis.com/sharing/rest/content/items/bb587c1043a949cca04f1b1904c235e3/data
The layers are embedded as inline Feature Collections (no FeatureServer endpoint).
All geometry arrives in Web Mercator (EPSG:3857) and is reprojected to WGS84 (EPSG:4326).
Dependencies:
pip install requests geopandas shapely pyproj fiona
Output:
One .shp file per layer, written to ./export/ (relative to this script)
"""
import argparse
import json
import os
import sys
from pathlib import Path
import requests
import geopandas as gpd
from shapely.geometry import Point, Polygon, MultiPolygon, shape
from pyproj import Transformer
# ─────────────────────────────────────────────
# CONFIG
# ─────────────────────────────────────────────
WEBMAP_URL = (
"https://Pflugervilleisd.maps.arcgis.com/sharing/rest/content/items/"
"bb587c1043a949cca04f1b1904c235e3/data?f=json"
)
# Output into the export/ folder inside this package
OUTPUT_DIR = Path(__file__).resolve().parent / "export"
# Source CRS (Web Mercator) → Target CRS (WGS84 lat/lon)
transformer = Transformer.from_crs("EPSG:3857", "EPSG:4326", always_xy=True)
# ─────────────────────────────────────────────
# GEOMETRY HELPERS
# ─────────────────────────────────────────────
def reproject_ring(ring):
"""Convert a list of [x, y] Web Mercator coords to (lon, lat) WGS84."""
return [transformer.transform(x, y) for x, y in ring]
def esri_polygon_to_shapely(esri_geom):
"""
Convert an ESRI polygon geometry dict (with 'rings') to a Shapely geometry.
Handles single polygons and multipolygons (multiple outer rings).
"""
rings = esri_geom.get("rings", [])
if not rings:
return None
reprojected = [reproject_ring(r) for r in rings]
# ESRI uses winding order to distinguish outer vs hole rings.
# For simplicity we treat each ring as its own polygon; Shapely's
# buffer(0) trick cleans up any self-intersections.
polys = [Polygon(r) for r in reprojected if len(r) >= 3]
if not polys:
return None
if len(polys) == 1:
return polys[0].buffer(0)
return MultiPolygon([p.buffer(0) for p in polys]).buffer(0)
def esri_point_to_shapely(esri_geom):
"""Convert an ESRI point geometry dict to a Shapely Point in WGS84."""
x = esri_geom.get("x")
y = esri_geom.get("y")
if x is None or y is None:
return None
lon, lat = transformer.transform(x, y)
return Point(lon, lat)
# ─────────────────────────────────────────────
# LAYER EXTRACTION
# ─────────────────────────────────────────────
def extract_layer(layer_data, layer_title):
"""
Given a single ESRI featureCollection layer dict, return a GeoDataFrame.
Handles both polygon and point geometry types.
"""
layer_def = layer_data.get("layerDefinition", {})
feature_set = layer_data.get("featureSet", {})
geom_type = layer_def.get("geometryType", "")
features = feature_set.get("features", [])
if not features:
print(f" [WARN] No features found in layer: {layer_title}")
return None
rows = []
skipped = 0
for feat in features:
esri_geom = feat.get("geometry", {})
attrs = feat.get("attributes", {})
if geom_type == "esriGeometryPolygon":
geom = esri_polygon_to_shapely(esri_geom)
elif geom_type == "esriGeometryPoint":
geom = esri_point_to_shapely(esri_geom)
else:
print(f" [WARN] Unsupported geometry type '{geom_type}' — skipping feature.")
skipped += 1
continue
if geom is None or geom.is_empty:
skipped += 1
continue
row = {"geometry": geom}
row.update(attrs)
rows.append(row)
if skipped:
print(f" [INFO] Skipped {skipped} invalid/empty features.")
if not rows:
print(f" [WARN] No valid geometries extracted from: {layer_title}")
return None
gdf = gpd.GeoDataFrame(rows, crs="EPSG:4326")
return gdf
# ─────────────────────────────────────────────
# MAIN
# ─────────────────────────────────────────────
def safe_filename(title):
"""Strip characters that are unsafe in filenames."""
keep = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_- "
name = "".join(c if c in keep else "_" for c in title)
return name.strip().replace(" ", "_")[:60]
def main():
parser = argparse.ArgumentParser(description="Extract PFISD attendance boundary shapefiles")
parser.add_argument(
"--local", "-l",
type=str,
default=None,
help="Path to a local WebMap JSON file (skip HTTP fetch)",
)
args = parser.parse_args()
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
print(f"Output directory: {OUTPUT_DIR.resolve()}\n")
# ------------------------------------------------------------------
# 1. Fetch the WebMap JSON (or load from local file)
# ------------------------------------------------------------------
if args.local:
local_path = Path(args.local)
print(f"Loading WebMap data from local file: {local_path}")
try:
with open(local_path) as f:
webmap = json.load(f)
except Exception as e:
print(f"[ERROR] Failed to load local file: {e}")
sys.exit(1)
else:
print("Fetching WebMap data from ArcGIS Online...")
try:
resp = requests.get(WEBMAP_URL, timeout=30)
resp.raise_for_status()
webmap = resp.json()
except Exception as e:
print(f"[ERROR] Failed to fetch WebMap: {e}")
sys.exit(1)
operational_layers = webmap.get("operationalLayers", [])
if not operational_layers:
print("[ERROR] No operationalLayers found in WebMap JSON.")
sys.exit(1)
print(f"Found {len(operational_layers)} operational layer(s).\n")
# ------------------------------------------------------------------
# 2. Iterate layers and export each as a shapefile
# ------------------------------------------------------------------
exported = 0
for layer in operational_layers:
layer_title = layer.get("title", "unnamed_layer")
feature_collection = layer.get("featureCollection", {})
sub_layers = feature_collection.get("layers", [])
if not sub_layers:
print(f"[SKIP] '{layer_title}' — no featureCollection layers found.")
continue
print(f"Processing: {layer_title}")
for i, sub_layer in enumerate(sub_layers):
suffix = f"_{i}" if len(sub_layers) > 1 else ""
fname = safe_filename(layer_title) + suffix
gdf = extract_layer(sub_layer, layer_title)
if gdf is None:
continue
out_path = OUTPUT_DIR / f"{fname}.shp"
# Shapefile field names are limited to 10 characters
gdf.columns = [c[:10] for c in gdf.columns]
try:
gdf.to_file(out_path, driver="ESRI Shapefile")
print(f" -> Saved {len(gdf)} features -> {out_path}")
exported += 1
except Exception as e:
print(f" [ERROR] Could not write {out_path}: {e}")
print()
# ------------------------------------------------------------------
# 3. Summary
# ------------------------------------------------------------------
print("-" * 50)
print(f"Done. {exported} shapefile(s) written to: {OUTPUT_DIR.resolve()}")
print("\nProjection: WGS84 (EPSG:4326)")
print("Open in QGIS, ArcGIS Pro, or any GIS tool that reads .shp files.")
if __name__ == "__main__":
main()