Fix hierarchical geoinfo for sumlevels that don't nest in the scope - #5
Merged
Merged
Conversation
Fetching place-county parts (155) for a county scope failed with HTTP 400: the last required geography was never exploded, so every place in the state was sent in one request. The fallback also fetched all state places once per scope county and returned parts outside the scope. Find the places intersecting the scope with one pseudo query, explode every level, drop duplicate parents, and keep only results inside the scope. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
CensusAPI(Endpoint('dec/pl', 2020), 'region15', sumlevel=SumLevel('155'), variables=['P1_001N'])(place-county parts) failed withUnboundLocalError: json. Underneath, it was an HTTP 400, hidden bymorpc.req.get_json_safely; that's fixed in morpc 0.7.3.155requiresstateandplace, andplacedoesn't nest under county, so the code falls back togeoinfo_for_hierarchical_geos(). That function had three bugs:exploderan onlyif len(in_scope) < len(requires)). The final request sent all 1,265 Ohio places, comma-joined, in onein=place:…clause, and the API allows only one place.Change (
morpc_census/geos.py)050$1600000, which gives 191 places for region15). If that isn't available, fall back to the existing per-row for/in lookup.county).Region15
155now takes about 191 requests per call (about 100 s).Limitation: the place list comes from current (2024) geoinfo, so places that no longer exist aren't returned. For example, Hidden Lakes CDP (16 people in 2020) is missing.
Testing
tests/test_geos_hierarchical.pywith 3 tests: scope filtering, one request per place, and the fallback path. All 3 fail before the change.dec/pl2000/2010/2020 for region15155:CHANGELOG (
0.6.3) andreference/dev_notes.mdare updated.🤖 Generated with Claude Code