geocode/templates/sparql/geosearch.sparql
Edward Betts cd9d8779d3 Add needs_commons=false option and redesign detail and index pages
Add a needs_commons parameter (default true) to both the API endpoint and
the detail page. When needs_commons=false, look up Wikidata items by OSM
relation ID (P402) via WDQS to return the most specific matching item even
if it has no Wikimedia Commons category. Only activate this path when the
matched item has no Commons category, so that locations with a Commons cat
always get the same result regardless of the parameter.

Remove the nearest-polygon fallback that was returning incorrect results for
inland points in broad admin areas (e.g. returning Falmer for a point in
Brighton). That fallback found the nearest polygon by boundary distance
without requiring containment, so the pin would appear outside the polygon.
The geosearch handles these cases correctly.

Redesign the detail page: place name as heading, result card, collapsible
API response and SPARQL query, improved OSM element cards with left-border
highlight on the matched element, and a toggle button between modes.

Redesign the index page: two-column layout with numbered steps and API
documentation including the needs_commons parameter, Bootstrap form, and
examples as a table.

Closes #28 (Add support for returning Wikidata item instead of commons category)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-18 20:22:54 +01:00

26 lines
1.2 KiB
SPARQL

SELECT DISTINCT ?item ?distance ?itemLabel ?isa ?isaLabel ?commonsCat ?commonsSiteLink ?osmRelation ?osmWay WHERE {
{
SELECT DISTINCT ?item ?location ?distance ?isa WHERE {
VALUES ?want { wd:Q486972 wd:Q56061 }
?item wdt:P31/wdt:P279* ?want .
?item wdt:P31 ?isa .
SERVICE wikibase:around {
?item wdt:P625 ?location.
bd:serviceParam wikibase:center "Point({{ lon }} {{ lat }})"^^geo:wktLiteral;
wikibase:radius 5;
wikibase:distance ?distance.
}
}
}
MINUS { ?item wdt:P582 ?endTime . }
MINUS {
VALUES ?isNot { wd:Q1497375 wd:Q1497364 wd:Q92086 wd:Q31028835 wd:Q160742 wd:Q17485079 wd:Q44613 wd:Q98116669 wd:Q3146899 wd:Q708676 wd:Q18917976 wd:Q2750108 wd:Q6021560 wd:Q39614 wd:Q513550 wd:Q31028695 wd:Q31028314 wd:Q839954 wd:Q744099 wd:Q28045079 wd:Q106626840 }
?item wdt:P31 ?isNot .
}
OPTIONAL { ?item wdt:P373 ?commonsCat. }
OPTIONAL { ?commonsSiteLink schema:about ?item;
schema:isPartOf <https://commons.wikimedia.org/>. }
OPTIONAL { ?item wdt:P402 ?osmRelation. }
OPTIONAL { ?item wdt:P10689 ?osmWay. }
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
} ORDER BY (?distance)