Skip to content

Commit a6864b8

Browse files
baogorekclaude
andcommitted
Fix at-large district GEOID round-trip conversion
At-large districts (AK, DE, ND, SD, VT, WY) have GEOID ending in 00 (e.g., DE=1000) but display as XX-01 via max(cd%100, 1). The worker naively converted DE-01 back to 1001 which didn't exist in the DB. Now tries the direct conversion first, then falls back to finding the sole CD for that state's FIPS prefix (at-large case). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 8e402c7 commit a6864b8

1 file changed

Lines changed: 24 additions & 4 deletions

File tree

modal_app/worker_script.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,33 @@ def main():
6666
)
6767
elif item_type == "district":
6868
state_code, dist_num = item_id.split("-")
69-
geoid = None
69+
state_fips = None
7070
for fips, code in STATE_CODES.items():
7171
if code == state_code:
72-
geoid = f"{fips}{int(dist_num):02d}"
72+
state_fips = fips
7373
break
74-
if geoid is None:
75-
raise ValueError(f"Unknown state in district: {item_id}")
74+
if state_fips is None:
75+
raise ValueError(
76+
f"Unknown state in district: {item_id}"
77+
)
78+
79+
candidate = f"{state_fips}{int(dist_num):02d}"
80+
if candidate in cds_to_calibrate:
81+
geoid = candidate
82+
else:
83+
state_cds = [
84+
cd
85+
for cd in cds_to_calibrate
86+
if int(cd) // 100 == state_fips
87+
]
88+
if len(state_cds) == 1:
89+
geoid = state_cds[0]
90+
else:
91+
raise ValueError(
92+
f"CD {candidate} not found and "
93+
f"state {state_code} has "
94+
f"{len(state_cds)} CDs"
95+
)
7696

7797
path = build_district_h5(
7898
cd_geoid=geoid,

0 commit comments

Comments
 (0)