diff --git a/geocode/wikidata.py b/geocode/wikidata.py index b7f8b79..ba209b9 100644 --- a/geocode/wikidata.py +++ b/geocode/wikidata.py @@ -102,17 +102,17 @@ def wd_uri_to_qid(value: str) -> str: return value[len(wd_entity) - 1 :] -def geosearch_query(lat: float, lon: float) -> list[Row]: +def geosearch_query(lat: float, lon: float) -> str: """Geosearch via WDQS.""" lat_str, lon_str = f"{lat:f}", f"{lon:f}" query = render_template("sparql/geosearch.sparql", lat=lat_str, lon=lon_str) - return wdqs(query) + return query def geosearch(lat: float, lon: float) -> Row | None: """Geosearch.""" default_max_dist = 1 - rows = geosearch_query(lat, lon) + rows = wdqs(geosearch_query(lat, lon)) max_dist = { "Q188509": 1, # suburb "Q3957": 2, # town diff --git a/lookup.py b/lookup.py index d939258..df246a9 100755 --- a/lookup.py +++ b/lookup.py @@ -12,6 +12,9 @@ from time import time import sqlalchemy.exc import werkzeug.debug.tbtools from flask import Flask, jsonify, redirect, render_template, request, url_for +from pygments import highlight +from pygments.formatters import HtmlFormatter +from pygments.lexers import SparqlLexer from sqlalchemy import func from sqlalchemy.orm.query import Query from werkzeug.wrappers import Response @@ -138,7 +141,9 @@ def lat_lon_to_wikidata(lat: float, lon: float) -> dict[str, typing.Any]: elements = [] result = wikidata.build_dict(hit, lat, lon) - return {"elements": elements, "result": result} + query = wikidata.geosearch_query(lat, lon) + + return {"elements": elements, "result": result, "query": query} def get_admin_level(tags: Tags) -> int | None: @@ -301,6 +306,16 @@ def wikidata_tag() -> str: ) +def highlight_sparql(query: str) -> str: + """Highlight SPARQL query syntax using Pygments.""" + lexer = SparqlLexer() + formatter = HtmlFormatter() + return highlight(query, lexer, formatter) + + +app.jinja_env.filters["highlight_sparql"] = highlight_sparql + + def build_detail_page(lat: float, lon: float) -> str: """Run lookup and build detail page.""" if lat < -90 or lat > 90 or lon < -180 or lon > 180: @@ -319,6 +334,8 @@ def build_detail_page(lat: float, lon: float) -> str: element = reply["result"].pop("element", None) geojson = reply["result"].pop("geojson", None) + css = HtmlFormatter().get_style_defs(".highlight") + return render_template( "detail.html", lat=lat, @@ -326,6 +343,7 @@ def build_detail_page(lat: float, lon: float) -> str: str=str, element_id=element, geojson=geojson, + css=css, **reply, ) diff --git a/templates/detail.html b/templates/detail.html index 2504d9a..0b33d62 100644 --- a/templates/detail.html +++ b/templates/detail.html @@ -54,6 +54,8 @@ L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', { width: 48% } +{{ css | safe }} + {% endblock %} @@ -96,6 +98,13 @@ L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {

No elements found

{% endif %} +{% if query %} +

Searching for Wikimedia Commons categories using this SPARQL query. Wikidata Query service

+
+{{ query | highlight_sparql | safe }} +
+{% endif %} + {% for element in elements %} {% set tags = element.tags %}