diff --git a/lookup.py b/lookup.py index 8344c98..9c6ad53 100755 --- a/lookup.py +++ b/lookup.py @@ -2,6 +2,7 @@ """Reverse geocode: convert lat/lon to Wikidata item & Wikimedia Commons category.""" import inspect +import json import random import re import socket @@ -535,7 +536,8 @@ def reports() -> str: @app.route("/pin//") def pin_detail(lat: str, lon: str) -> Response: """Details for map pin location.""" - reply = lat_lon_to_wikidata(float(lat), float(lon)) + needs_commons = request.args.get("needs_commons", "true").lower() != "false" + reply = lat_lon_to_wikidata(float(lat), float(lon), needs_commons) element = reply["result"].pop("element", None) geojson = reply["result"].pop("geojson", None) @@ -548,18 +550,22 @@ def pin_detail(lat: str, lon: str) -> Response: str=str, element_id=element, geojson=geojson, + needs_commons=needs_commons, css=css, **reply, ) - return jsonify(html=html) + geojson_data = json.loads(geojson) if geojson else None + return jsonify(html=html, geojson=geojson_data) @app.route("/map") def map_page() -> str: """Map page.""" css = HtmlFormatter().get_style_defs(".highlight") - return render_template("map.html", css=css) + lat = request.args.get("lat", type=float) + lon = request.args.get("lon", type=float) + return render_template("map.html", css=css, init_lat=lat, init_lon=lon) if __name__ == "__main__": diff --git a/templates/map.html b/templates/map.html index 8dc9533..08cb863 100644 --- a/templates/map.html +++ b/templates/map.html @@ -1,6 +1,6 @@ {% extends "base.html" %} -{% block title %}Geocode to Commons{% endblock %} +{% block title %}Geocode to Commons — Map{% endblock %} {% block link %} {% endblock %} {% block style %} {% endblock %} {% block content %}
-
-

Geocode coordinates to Commons Category

- -
Click on the map
- +
+

Geocode to Commons

+

Click anywhere on the map to look up the Wikidata item and Commons category.

+
+ + +
+
- {% endblock %} diff --git a/templates/pin_detail.html b/templates/pin_detail.html index feca33c..2572468 100644 --- a/templates/pin_detail.html +++ b/templates/pin_detail.html @@ -1,34 +1,90 @@ -

API returns

-
{{ result | tojson(indent=2) }}
- -{% if result.wikidata %} -

Wikidata item: {{ result.wikidata }}

-{% endif %} - {% if result.commons_cat %} -

Commons category: {{result.commons_cat.title }}

+
{{ result.commons_cat.title }}
+{% elif result.wikidata %} +
{{ result.wikidata }}
+{% else %} +
No result
{% endif %} +

{{ lat }}, {{ lon }}

+ +
+
+
+ {% if result.wikidata %} +
+ Wikidata + {{ result.wikidata }} +
+ {% endif %} + {% if result.commons_cat %} +
+ Commons category + {{ result.commons_cat.title }} +
+ {% endif %} + {% if result.admin_level %} +
+ Admin level + {{ result.admin_level }} +
+ {% endif %} +
+
+
+ +
+ Detail page + OpenStreetMap + {% if result.commons_cat %} + Commons category + {% endif %} + {% if result.wikidata %} + {{ result.wikidata }} + {% endif %} +
+ +
+ API response +
{{ result | tojson(indent=2) }}
+
{% if elements %} -

{{ elements.count() }} surrounding elements found

+{% set elem_count = elements.count() %} +
{{ elem_count }} surrounding OSM element{{ 's' if elem_count != 1 }}
+{% for element in elements %} +{% set tags = element.tags %} +
+
+
+ + {{ tags.name or ('relation' if element.osm_id < 0 else 'way') ~ ' ' ~ element.osm_id|abs }} + +
+ {% if tags.wikidata %} + {{ tags.wikidata }} + {% endif %} + OSM +
+
+
+ {% for key, value in tags.items() if not (key == "way_area" or "name:" in key or key.startswith("source") or key == "name" or key == "wikidata") %} +
+ {{ key }}{{ value }} +
+ {% endfor %} +
+
+
+{% endfor %} {% else %} -

No elements found

+

No surrounding elements found.

{% endif %} {% if query %} -

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

-
-{{ query | highlight_sparql | safe }} -
+
+ SPARQL geosearch query + run on Wikidata ↗ + +
{{ query | highlight_sparql | safe }}
+
{% endif %} - -{% for element in elements %} -{% set tags = element.tags %} -
- {% for key, value in element.tags.items() if not (key == "way_area" or "name:" in key or key.startswith("source")) %} -
{{ key }}: {{ value }}
- {% endfor %} -
-{% endfor %} - -