Repository navigation
A blank or junk country is not found, and the site tool's prefill finds every country the geocoder names - #80
Open
HughRunyan wants to merge 2 commits into
Conversation
… the geocoder names
_iso3_for ended with pycountry's fuzzy search, which scores every country for
an empty name and scores a country for any of its subdivisions that match. So
"" and " " were the United Kingdom (the most subdivisions), "None" Italy
(Frosinone), "nan" Thailand (Nan province), and None raised AttributeError
rather than LookupError.
_iso3_for now raises LookupError for anything that isn't a non-blank string,
strips the name, and takes the fuzzy answer only when the name is whole words
of that country's own name or its initials ("UK", "UAE").
The site tool's prefill (sdst_prepopulate) crashed with AttributeError for a
point Nominatim can't place or gives no country, and failed for 11 English names
Nominatim uses that neither SWEET nor pycountry spell its way (Turkey, Ivory
Coast, Cape Verde, ...). _iso3_at tries the geocoder's name, then its ISO 3166-1
code, and the prefill says "No country at <lat>, <lon>." where there is none.
Over 306 reverse-geocoded points (222 country names): 210 resolve as before,
the 11 now resolve, Sint Maarten moves from NLD to SXM. Every code and name in
SWEET's own table resolves as before.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Bug
_iso3_for(incity_params.py) turns a caller's country into the ISO3 code SWEET's defaults are keyed by. Its last resort is pycountry's fuzzy search, which scores every country for an empty name and scores a country for any of its subdivisions that match. So junk came back as a real country, silently:""," ""None""nan""-","0"None, NaNAttributeErrorLookupError, so callers crash instead of saying "not found"The site tool's lat/lon prefill (
sdst_prepopulate) names the country with Nominatim's English name, and had two more failures:None; in Antarctica the address has no country. Both raisedAttributeError.ValueError("Country '...' not found."). WasteMAP answered 500 for a custom site in Turkey.Environment: SWEET_python
custom-location-runs-for-kosovo(#77,2b95a2b), pycountry 26.2.16, geopy 2.4.1, Python 3.13.Reproduction:
Expected:
LookupErrorfrom_iso3_for, sodst_baseline_blank/cityparams_obj_for_blank_siteraiseValueError("Country '' not found."); the prefill resolves every country Nominatim names, and says so where there is none.Actual: the UK's model, Italy's, Thailand's;
AttributeError; "Country 'Turkey' not found."Fix
_iso3_forraisesLookupErrorfor anything that isn't a non-blank string, and strips the name first (" Kosovo "was Serbia: SWEET's table missed the padded name and the fuzzy search found Kosovo-Metohija).name,official_nameorcommon_name, or their initials ("UK", "UAE"). The best-ranked such country wins, so a subdivision can no longer carry a match. A province's own name is now not found even where the old answer was right ("England" → GBR)._iso3_at): the English name first, then Nominatim'scountry_code. The name goes first because SWEET has its own codes for Aruba, Curaçao and Sint Maarten, which Nominatim codes as the Netherlands. Where there's no country, it raisesValueError("No country at <lat>, <lon>.").Effect (model-output change for the prefill in Sint Maarten)
I reverse-geocoded 306 points through Nominatim with
accept-language=en, assdst_prepopulatedoes: one OpenStreetMapplace=countrylabel per country, plus hand-picked points for 81 territories and edge cases. That gave 222 distinct country names. I compared #77's lookup with this branch's, using Nominatim's owncountry_codeas the reference:ge)nl)AttributeErrordst_baseline_blank,cityparams_obj_for_blank_siteandsdst_prepopulatecall_iso3_for, and all three are WasteMAP paths.Junk after the change:
""," ",None, NaN,"None","nan","null","undefined","N/A","-","0","*","?",".",","all raise. Real inputs still resolve:"UK","UAE","CAR","826","GB","MUS","Niger","United States","Brunei","Vatican","Timor".Tests
tests/test_country_lookup.pygains 41 cases, and 31 of them fail on #77:LookupError"None","nan","-","0","England","Kurdistan"are not countriesdst_baseline_blank("")andcityparams_obj_for_blank_site(" ")raise "not found"The other 10 cases guard behaviour that already worked (Aruba, Curaçao, Abkhazia, "UK", the numeric code).
Full suite: 330 passed (Python 3.13 locally; CI runs 3.12).
Paired PR
RMI/WasteMAP#859, on the same branch name, so WasteMAP CI installs this branch. It makes the site tool's two endpoints answer 400 (not 500) when SWEET can't find the country. Its 4 new fast tests fail against #77 and pass against this branch. Merge #77 first, then this, then the WasteMAP PR.
Notes for review
custom-location-runs-for-kosovo). GitHub retargets this tomainwhen A Custom Location in Kosovo builds, and the name "Kosovo" no longer means Serbia #77 merges."Korea"is still North Korea (a pre-existing tie broken alphabetically), and"province"is Taiwan ("Taiwan, Province of China"). Neither is an input WasteMAP sends.Acceptance criteria
_iso3_forraisesLookupErrorfor"", whitespace,None, NaN and other non-strings._iso3_for("None"),("nan"),("-"),("0")raise rather than resolve via a subdivision._iso3_for("MUS") == "MUS","Niger"→ NER,"United States"→ USA (tests/test_population_growth.py).ValueError(notAttributeError) for a point in no country.changelog/2026-10.md,changelog/README.md).Definition of Done
Acceptance criteria met · tests & checks pass · docs (changelog) updated · reviewed & merged.
🤖 Generated with Claude Code