Various improvements
This commit is contained in:
parent
474d9c6a66
commit
d24744bc29
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
|||
__pycache__
|
||||
.mypy_cache/
|
||||
config/default.py
|
||||
|
|
0
geocode/py.typed
Normal file
0
geocode/py.typed
Normal file
|
@ -153,7 +153,7 @@ def unescape_title(t: str) -> str:
|
|||
return urllib.parse.unquote(t.replace("_", " "))
|
||||
|
||||
|
||||
Hit = dict[str, str | None]
|
||||
Hit = dict[str, str | int | None]
|
||||
|
||||
|
||||
def commons_from_rows(rows: list[Row]) -> Hit | None:
|
||||
|
@ -188,6 +188,7 @@ def build_dict(hit: Hit | None, lat: str | float, lon: str | float) -> WikidataD
|
|||
"coords": coords,
|
||||
"admin_level": hit.get("admin_level"),
|
||||
"wikidata": hit["wikidata"],
|
||||
"element": hit.get("element"),
|
||||
}
|
||||
if not commons_cat:
|
||||
return ret
|
||||
|
|
21
lookup.py
21
lookup.py
|
@ -28,7 +28,7 @@ def get_random_lat_lon() -> tuple[float, float]:
|
|||
return lat, lon
|
||||
|
||||
|
||||
Elements = sqlalchemy.orm.query.Query[model.Polygon]
|
||||
Elements = sqlalchemy.orm.query.Query
|
||||
|
||||
|
||||
def do_lookup(
|
||||
|
@ -80,7 +80,7 @@ def lat_lon_to_wikidata(lat: str | float, lon: str | float) -> dict[str, typing.
|
|||
|
||||
|
||||
def osm_lookup(
|
||||
elements: Elements, lat: str | float, lon: str | float
|
||||
elements: Elements, lat: str | float, lon: str | float # type:ignore
|
||||
) -> wikidata.Hit | None:
|
||||
"""OSM lookup."""
|
||||
ret: wikidata.Hit | None
|
||||
|
@ -104,12 +104,14 @@ def osm_lookup(
|
|||
"wikidata": qid,
|
||||
"commons_cat": commons,
|
||||
"admin_level": admin_level,
|
||||
"element": e.osm_id,
|
||||
}
|
||||
gss = tags.get("ref:gss")
|
||||
if gss:
|
||||
ret = wikidata.get_commons_cat_from_gss(gss)
|
||||
if ret:
|
||||
ret["admin_level"] = admin_level
|
||||
ret["element"] = e.osm_id
|
||||
return ret
|
||||
|
||||
name = tags.get("name")
|
||||
|
@ -123,11 +125,10 @@ def osm_lookup(
|
|||
ret = wikidata.commons_from_rows(rows)
|
||||
if ret:
|
||||
ret["admin_level"] = admin_level
|
||||
ret["element"] = e.osm_id
|
||||
return ret
|
||||
|
||||
has_wikidata_tag = [
|
||||
e.tags for e in elements if e.tags.get("wikidata") # type: ignore
|
||||
]
|
||||
has_wikidata_tag = [e.tags for e in elements if e.tags.get("wikidata")]
|
||||
if len(has_wikidata_tag) != 1:
|
||||
return None
|
||||
|
||||
|
@ -159,7 +160,9 @@ def index() -> str | Response:
|
|||
lat, lon = request.args.get("lat"), request.args.get("lon")
|
||||
|
||||
if lat is not None and lon is not None:
|
||||
return jsonify(lat_lon_to_wikidata(lat, lon)["result"])
|
||||
result = lat_lon_to_wikidata(lat, lon)["result"]
|
||||
result.pop("element", None)
|
||||
return jsonify(result)
|
||||
|
||||
samples = sorted(geocode.samples, key=lambda row: row[2])
|
||||
return render_template("index.html", samples=samples)
|
||||
|
@ -215,7 +218,11 @@ def detail_page() -> Response | str:
|
|||
query, r = e.args
|
||||
return render_template("query_error.html", lat=lat, lon=lon, query=query, r=r)
|
||||
|
||||
return render_template("detail.html", lat=lat, lon=lon, **reply)
|
||||
element = reply["result"].pop("element", None)
|
||||
|
||||
return render_template(
|
||||
"detail.html", lat=lat, lon=lon, str=str, element_id=element, **reply
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
22
templates/base.html
Normal file
22
templates/base.html
Normal file
|
@ -0,0 +1,22 @@
|
|||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
|
||||
|
||||
{% block link %}{% endblock %}
|
||||
|
||||
<title>
|
||||
{% block title %}{% endblock %}
|
||||
</title>
|
||||
|
||||
{% block style %}{% endblock %}
|
||||
</head>
|
||||
|
||||
<body>
|
||||
{% block content %}{% endblock %}
|
||||
{% block script %}{% endblock %}
|
||||
</body>
|
||||
</html>
|
|
@ -1,11 +1,9 @@
|
|||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Geocode to commons</title>
|
||||
</head>
|
||||
{% extends "base.html" %}
|
||||
|
||||
<body>
|
||||
{% block title %}Geocode to Commons{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="m-3">
|
||||
<h1>Geocode coordinates to Commons Category</h1>
|
||||
|
||||
<p><a href="{{ url_for('index', lat=lat, lon=lon) }}">visit endpoint</a>
|
||||
|
@ -24,17 +22,32 @@
|
|||
|
||||
</p>
|
||||
|
||||
<pre>{{ result | pprint }}</pre>
|
||||
<h4>API returns</h4>
|
||||
<pre>{{ result | tojson(indent=2) }}</pre>
|
||||
|
||||
{% if result.wikidata %}
|
||||
<p><strong>Wikidata item</strong>: <a href="https://www.wikidata.org/wiki/{{ result.wikidata }}">{{ result.wikidata }}</a></p>
|
||||
{% endif %}
|
||||
|
||||
{% if result.commons_cat %}
|
||||
<p>({{ lat }}, {{ lon }}, {{result.commons_cat.title | pprint }}),</p>
|
||||
<p><strong>Commons category</strong>: <a href="{{ result.commons_cat.url }}">{{result.commons_cat.title }}</a></p>
|
||||
{% endif %}
|
||||
|
||||
{% if elements %}
|
||||
<p>{{ elements.count() }} surrounding elements found</p>
|
||||
{% else %}
|
||||
<p>No elements found</p>
|
||||
{% endif %}
|
||||
|
||||
{% for element in elements %}
|
||||
{% set tags = element.tags %}
|
||||
<pre>{{ element.tags | pprint }}</pre>
|
||||
<div class="rounded border border-4 p-1 my-2{% if element_id == element.osm_id %} bg-primary-subtle{% endif %}">
|
||||
{% for key, value in element.tags.items() if not (key == "way_area" or "name:" in key or key.startswith("source")) %}
|
||||
<div><strong>{{ key }}</strong>: {{ value }}</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
{% endfor %}
|
||||
|
||||
|
||||
</body>
|
||||
</html>
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
|
|
@ -1,11 +1,9 @@
|
|||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Geocode to commons</title>
|
||||
</head>
|
||||
{% extends "base.html" %}
|
||||
|
||||
<body>
|
||||
{% block title %}Geocode to Commons{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="m-3">
|
||||
<h1>Geocode coordinates to Commons Category</h1>
|
||||
|
||||
<ol>
|
||||
|
@ -33,6 +31,6 @@ Latitude/Longitude: <input name="q"/> (e.g. 54.375, -2.999) <input type="submit"
|
|||
</ul>
|
||||
|
||||
<p>source code: <a href="https://github.com/EdwardBetts/uk-geocode">https://github.com/EdwardBetts/uk-geocode</a></p>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
{% endblock %}
|
||||
|
|
Loading…
Reference in a new issue