parent
b631476844
commit
1625f3d5d1
|
@ -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
|
||||
|
|
20
lookup.py
20
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,
|
||||
)
|
||||
|
||||
|
|
|
@ -54,6 +54,8 @@ L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
|
|||
width: 48%
|
||||
}
|
||||
|
||||
{{ css | safe }}
|
||||
|
||||
</style>
|
||||
{% endblock %}
|
||||
|
||||
|
@ -96,6 +98,13 @@ L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
|
|||
<p>No elements found</p>
|
||||
{% endif %}
|
||||
|
||||
{% if query %}
|
||||
<p>Searching for Wikimedia Commons categories using this SPARQL query. <a href="https://query.wikidata.org/#{{ query | urlencode }}">Wikidata Query service</a></p>
|
||||
<div>
|
||||
{{ query | highlight_sparql | safe }}
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% for element in elements %}
|
||||
{% set tags = element.tags %}
|
||||
<div class="rounded border border-4 p-1 my-2{% if element_id == element.osm_id %} bg-primary-subtle{% endif %}">
|
||||
|
|
Loading…
Reference in a new issue