forked from ChelseaKR/nearmiss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_us_state_boundaries.py
More file actions
222 lines (197 loc) · 7.51 KB
/
Copy pathbuild_us_state_boundaries.py
File metadata and controls
222 lines (197 loc) · 7.51 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
#!/usr/bin/env python3
"""Build the public 2024 US state boundary asset from a pinned Census KML.
The visualization uses geometry only for orientation. Fatal-crash values remain
exclusively sourced from the separately verified FARS projection. The input is
the Census Bureau's national 1:20,000,000 cartographic boundary archive; the
output deliberately retains only the 50 states and District of Columbia.
"""
from __future__ import annotations
import argparse
import hashlib
import io
import json
import urllib.request
import zipfile
from pathlib import Path
from typing import TypedDict, cast
from xml.etree import ElementTree
SOURCE_URL = "https://www2.census.gov/geo/tiger/GENZ2024/kml/cb_2024_us_state_20m.zip"
SOURCE_SHA256 = "37337db59415f010c594fba96a48aa6e950e633dc0e555cc7b1ce8edd794c673"
KML_MEMBER = "cb_2024_us_state_20m.kml"
KML = "{http://www.opengis.net/kml/2.2}"
EXPECTED_STATES = {
"01": ("AL", "Alabama"),
"02": ("AK", "Alaska"),
"04": ("AZ", "Arizona"),
"05": ("AR", "Arkansas"),
"06": ("CA", "California"),
"08": ("CO", "Colorado"),
"09": ("CT", "Connecticut"),
"10": ("DE", "Delaware"),
"11": ("DC", "District of Columbia"),
"12": ("FL", "Florida"),
"13": ("GA", "Georgia"),
"15": ("HI", "Hawaii"),
"16": ("ID", "Idaho"),
"17": ("IL", "Illinois"),
"18": ("IN", "Indiana"),
"19": ("IA", "Iowa"),
"20": ("KS", "Kansas"),
"21": ("KY", "Kentucky"),
"22": ("LA", "Louisiana"),
"23": ("ME", "Maine"),
"24": ("MD", "Maryland"),
"25": ("MA", "Massachusetts"),
"26": ("MI", "Michigan"),
"27": ("MN", "Minnesota"),
"28": ("MS", "Mississippi"),
"29": ("MO", "Missouri"),
"30": ("MT", "Montana"),
"31": ("NE", "Nebraska"),
"32": ("NV", "Nevada"),
"33": ("NH", "New Hampshire"),
"34": ("NJ", "New Jersey"),
"35": ("NM", "New Mexico"),
"36": ("NY", "New York"),
"37": ("NC", "North Carolina"),
"38": ("ND", "North Dakota"),
"39": ("OH", "Ohio"),
"40": ("OK", "Oklahoma"),
"41": ("OR", "Oregon"),
"42": ("PA", "Pennsylvania"),
"44": ("RI", "Rhode Island"),
"45": ("SC", "South Carolina"),
"46": ("SD", "South Dakota"),
"47": ("TN", "Tennessee"),
"48": ("TX", "Texas"),
"49": ("UT", "Utah"),
"50": ("VT", "Vermont"),
"51": ("VA", "Virginia"),
"53": ("WA", "Washington"),
"54": ("WV", "West Virginia"),
"55": ("WI", "Wisconsin"),
"56": ("WY", "Wyoming"),
}
class BoundaryFeature(TypedDict):
type: str
id: str
properties: dict[str, str]
geometry: dict[str, object]
class BoundaryCollection(TypedDict):
type: str
name: str
source: dict[str, object]
features: list[BoundaryFeature]
def _coordinates(text: str | None) -> list[list[float]]:
if not text:
raise ValueError("Census polygon ring has no coordinates")
ring = [
[round(float(parts[0]), 6), round(float(parts[1]), 6)]
for token in text.split()
if len(parts := token.split(",")) >= 2
]
if len(ring) < 4:
raise ValueError("Census polygon ring has fewer than four positions")
if ring[0] != ring[-1]:
ring.append(ring[0])
return ring
def _ring(boundary: ElementTree.Element | None) -> list[list[float]]:
if boundary is None:
raise ValueError("Census polygon is missing a boundary")
coordinates = boundary.find(f"{KML}LinearRing/{KML}coordinates")
return _coordinates(coordinates.text if coordinates is not None else None)
def _geometry(placemark: ElementTree.Element) -> dict[str, object]:
polygons: list[list[list[list[float]]]] = []
for polygon in placemark.findall(f".//{KML}Polygon"):
rings = [_ring(polygon.find(f"{KML}outerBoundaryIs"))]
rings.extend(_ring(inner) for inner in polygon.findall(f"{KML}innerBoundaryIs"))
polygons.append(rings)
if not polygons:
raise ValueError("Census state placemark has no polygon")
if len(polygons) == 1:
return {"type": "Polygon", "coordinates": polygons[0]}
return {"type": "MultiPolygon", "coordinates": polygons}
def convert(archive: bytes) -> BoundaryCollection:
actual_sha = hashlib.sha256(archive).hexdigest()
if actual_sha != SOURCE_SHA256:
raise ValueError(
f"Census archive SHA-256 mismatch: expected {SOURCE_SHA256}, got {actual_sha}"
)
with zipfile.ZipFile(io.BytesIO(archive)) as source_zip:
root = ElementTree.fromstring(source_zip.read(KML_MEMBER))
features: list[BoundaryFeature] = []
seen: set[str] = set()
for placemark in root.findall(f".//{KML}Placemark"):
values = {
item.attrib.get("name", ""): (item.text or "")
for item in placemark.findall(f".//{KML}SimpleData")
}
state_fips = values.get("STATEFP")
if state_fips not in EXPECTED_STATES:
continue
abbreviation, name = EXPECTED_STATES[state_fips]
if values.get("STUSPS") != abbreviation or values.get("NAME") != name:
raise ValueError(f"unexpected Census state crosswalk for FIPS {state_fips}")
if state_fips in seen:
raise ValueError(f"duplicate Census state placemark for FIPS {state_fips}")
seen.add(state_fips)
features.append(
{
"type": "Feature",
"id": abbreviation,
"properties": {
"state_fips": state_fips,
"state_abbreviation": abbreviation,
"state_name": name,
},
"geometry": _geometry(placemark),
}
)
missing = set(EXPECTED_STATES) - seen
if missing:
raise ValueError(f"Census archive is missing expected states: {sorted(missing)}")
features.sort(key=lambda feature: str(feature["properties"]["state_fips"]))
return {
"type": "FeatureCollection",
"name": "2024 Census cartographic boundaries for the 50 states and DC",
"source": {
"name": "U.S. Census Bureau 2024 Cartographic Boundary Files",
"vintage": 2024,
"resolution": "1:20,000,000",
"distribution_url": SOURCE_URL,
"raw_zip_sha256": SOURCE_SHA256,
"raw_zip_size_bytes": len(archive),
"conversion": (
"KML polygons to RFC 7946 GeoJSON; coordinates rounded to 6 decimals; "
"50 states and DC retained"
),
},
"features": features,
}
def _download() -> bytes:
request = urllib.request.Request(
SOURCE_URL,
headers={"User-Agent": "nearmiss-boundary-builder/1"},
)
with urllib.request.urlopen(request, timeout=60) as response:
return bytes(response.read())
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--archive", type=Path, help="use an already-downloaded pinned Census ZIP")
parser.add_argument(
"--out",
type=Path,
default=Path("data/published/us-state-boundaries-2024.json"),
)
args = parser.parse_args()
archive_path = cast(Path | None, args.archive)
out_path = cast(Path, args.out)
archive = archive_path.read_bytes() if archive_path else _download()
boundary_data = convert(archive)
out_path.parent.mkdir(parents=True, exist_ok=True)
payload = json.dumps(boundary_data, ensure_ascii=False, separators=(",", ":")) + "\n"
out_path.write_text(payload, encoding="utf-8")
print(f"boundaries: {out_path} ({len(boundary_data['features'])} jurisdictions)")
return 0
if __name__ == "__main__":
raise SystemExit(main())