Skip to content

Commit 3c8a164

Browse files
committed
Fix ZCTA universe parsing and table-ACS ZCTA ingestion
1 parent c4081be commit 3c8a164

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

geocompare/database/Database.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,13 +1198,22 @@ def __init__(self, path, progress_callback=None):
11981198
# 310 = Metro/Micro Area
11991199
# 400 = Urban Area
12001200
# 860 = ZCTA
1201-
rows = [
1202-
row
1203-
for row in rows
1204-
if row[1] in {"010", "160", "050", "040", "860", "310", "400"}
1205-
and len(row[3]) >= 5
1206-
and row[3][3:5] == "00"
1207-
]
1201+
allowed_sumlevels = {"010", "160", "050", "040", "860", "310", "400"}
1202+
1203+
def _keep_geography_row(row):
1204+
sumlevel = row[1]
1205+
geoid = row[3]
1206+
if sumlevel not in allowed_sumlevels or len(geoid) < 5:
1207+
return False
1208+
1209+
# Table-based ACS uses non-"00" GEOID middle codes for some
1210+
# summary levels (notably ZCTA = 860Z200USxxxxx).
1211+
if self.acs_layout == "table":
1212+
return "US" in geoid
1213+
1214+
return geoid[3:5] == "00"
1215+
1216+
rows = [row for row in rows if _keep_geography_row(row)]
12081217
rows = [
12091218
[
12101219
row[0], # STUSAB [lowercase]

geocompare/tools/query_syntax.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,12 @@ def build_context(
102102
elif in_zcta:
103103
group = in_zcta
104104

105-
if universe and group:
106-
return f"{universe}+{group}"
107-
if universe:
108-
return f"{universe}+"
105+
normalized_universe = universe.lower() if universe else None
106+
107+
if normalized_universe and group:
108+
return f"{normalized_universe}+{group}"
109+
if normalized_universe:
110+
return f"{normalized_universe}+"
109111
if group:
110112
return group
111113
return ""

geocompare/tools/summary_level_parser.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def __init__(self):
3636
}
3737

3838
def is_summary_level_keyword(self, input_str: str) -> bool:
39-
return input_str in self.keyword_to_code
39+
return input_str.lower() in self.keyword_to_code
4040

4141
def is_summary_level_code(self, input_str: str) -> bool:
4242
return input_str in self.code_to_keyword
@@ -48,6 +48,7 @@ def parse_context(self, context: str) -> Tuple[Optional[str], Optional[str], Opt
4848
if context:
4949
if "+" in context:
5050
universe_sl, group = context.split("+", 1)
51+
universe_sl = universe_sl.lower()
5152
if self.is_summary_level_keyword(universe_sl):
5253
universe_sl = self.keyword_to_code[universe_sl]
5354
elif not self.is_summary_level_code(universe_sl):

0 commit comments

Comments
 (0)