Fix wiki lookup to match on name+state+country instead of osm_key/osm_value

- Remove osm_key/osm_value from wiki_places lookup query
- Add fallback matching: try state first, then country only
- Parse state/country from wikipedia extratag when address is empty
- Add US states and Canadian provinces parsing for wikipedia titles
- Apply wiki enrichment to cached results (was missing)

Fixes wiki_summary and wiki_url not appearing for boundary/administrative
places like Joliet, IL where OSM returns boundary/administrative but
wiki_places has place/city.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Matt 2026-05-03 00:17:49 +00:00
commit 248f4bded4
2 changed files with 89 additions and 27 deletions

View file

@ -52,13 +52,13 @@ def _get_db():
def lookup_wiki(place_name, osm_key, osm_value, state, country_code):
"""
Look up wiki data for a place by exact match.
Look up wiki data for a place by name and country, with optional state.
Args:
place_name: Name of the place (e.g., "Twin Falls")
osm_key: OSM key (e.g., "place", "natural", "waterway")
osm_value: OSM value (e.g., "city", "peak", "river")
state: State/province name (may be None)
osm_key: OSM key (unused, kept for API compatibility)
osm_value: OSM value (unused, kept for API compatibility)
state: State/province name (may be None or empty)
country_code: ISO country code (e.g., "us", "ca")
Returns:
@ -71,33 +71,51 @@ def lookup_wiki(place_name, osm_key, osm_value, state, country_code):
# Normalize inputs
place_name = (place_name or '').strip()
osm_key = (osm_key or '').strip().lower()
osm_value = (osm_value or '').strip().lower()
state = (state or '').strip()
state = (state or '').strip() if state else ''
country_code = (country_code or '').strip().lower()
if not place_name or not osm_key or not osm_value or not country_code:
if not place_name or not country_code:
return None
try:
# Direct match query
row = db.execute("""
SELECT
summary,
wikipedia_title,
wikivoyage_title,
wikipedia_exists,
wikivoyage_exists,
wiki_population
FROM wiki_places
WHERE place_name = ?
AND osm_key = ?
AND osm_value = ?
AND COALESCE(state, '') = ?
AND country_code = ?
AND wikipedia_exists = 1
LIMIT 1
""", (place_name, osm_key, osm_value, state, country_code)).fetchone()
row = None
# Try exact match with state first (if state provided)
if state:
row = db.execute("""
SELECT
summary,
wikipedia_title,
wikivoyage_title,
wikipedia_exists,
wikivoyage_exists,
wiki_population
FROM wiki_places
WHERE place_name = ?
AND state = ?
AND country_code = ?
AND summary IS NOT NULL
ORDER BY importance DESC
LIMIT 1
""", (place_name, state, country_code)).fetchone()
# Fall back to name + country only (for places without state in query)
if not row:
row = db.execute("""
SELECT
summary,
wikipedia_title,
wikivoyage_title,
wikipedia_exists,
wikivoyage_exists,
wiki_population
FROM wiki_places
WHERE place_name = ?
AND country_code = ?
AND summary IS NOT NULL
ORDER BY importance DESC
LIMIT 1
""", (place_name, country_code)).fetchone()
if not row:
return None