Compare commits
No commits in common. "690d3281df5c5790ae6f7a1bb2dfd5ea2e3d2f39" and "b700f88da66cf06d14001c8809954e6cf28155dc" have entirely different histories.
690d3281df
...
b700f88da6
|
@ -38,9 +38,4 @@ samples = [
|
||||||
(51.0804, -2.3263, "Zeals"),
|
(51.0804, -2.3263, "Zeals"),
|
||||||
(55.7644, -4.1770, "East Kilbride"),
|
(55.7644, -4.1770, "East Kilbride"),
|
||||||
(51.4520, -2.6210, "Hotwells, Bristol"),
|
(51.4520, -2.6210, "Hotwells, Bristol"),
|
||||||
(51.486, -0.089, "Camberwell and Peckham"),
|
|
||||||
(52.9469, 0.1324, "England"),
|
|
||||||
(52.9953, -2.1768, "Stoke-on-Trent"),
|
|
||||||
(50.8553, -0.1192, "Hollingbury Castle"),
|
|
||||||
(51.4564, -0.6486, "Windsor"),
|
|
||||||
]
|
]
|
||||||
|
|
39
lookup.py
39
lookup.py
|
@ -3,8 +3,8 @@
|
||||||
import random
|
import random
|
||||||
import typing
|
import typing
|
||||||
|
|
||||||
|
import sqlalchemy
|
||||||
from flask import Flask, jsonify, redirect, render_template, request, url_for
|
from flask import Flask, jsonify, redirect, render_template, request, url_for
|
||||||
from sqlalchemy.orm.query import Query
|
|
||||||
from werkzeug.wrappers import Response
|
from werkzeug.wrappers import Response
|
||||||
|
|
||||||
import geocode
|
import geocode
|
||||||
|
@ -16,6 +16,7 @@ app.config.from_object("config.default")
|
||||||
database.init_app(app)
|
database.init_app(app)
|
||||||
|
|
||||||
Tags = typing.Mapping[str, str]
|
Tags = typing.Mapping[str, str]
|
||||||
|
Elements = sqlalchemy.orm.query.Query[model.Polygon]
|
||||||
|
|
||||||
|
|
||||||
def get_random_lat_lon() -> tuple[float, float]:
|
def get_random_lat_lon() -> tuple[float, float]:
|
||||||
|
@ -31,7 +32,7 @@ def get_random_lat_lon() -> tuple[float, float]:
|
||||||
|
|
||||||
|
|
||||||
def do_lookup(
|
def do_lookup(
|
||||||
elements: "Query[model.Polygon]", lat: str | float, lon: str | float
|
elements: Elements, lat: str | float, lon: str | float
|
||||||
) -> wikidata.WikidataDict:
|
) -> wikidata.WikidataDict:
|
||||||
"""Do lookup."""
|
"""Do lookup."""
|
||||||
try:
|
try:
|
||||||
|
@ -124,7 +125,7 @@ def hit_from_name(
|
||||||
|
|
||||||
|
|
||||||
def osm_lookup(
|
def osm_lookup(
|
||||||
elements: "Query[model.Polygon]", lat: str | float, lon: str | float
|
elements: Elements, lat: str | float, lon: str | float
|
||||||
) -> wikidata.Hit | None:
|
) -> wikidata.Hit | None:
|
||||||
"""OSM lookup."""
|
"""OSM lookup."""
|
||||||
ret: wikidata.Hit | None
|
ret: wikidata.Hit | None
|
||||||
|
@ -188,10 +189,16 @@ def index() -> str | Response:
|
||||||
|
|
||||||
|
|
||||||
@app.route("/random")
|
@app.route("/random")
|
||||||
def random_location() -> str | Response:
|
def random_location() -> str:
|
||||||
"""Return detail page for random lat/lon."""
|
"""Return detail page for random lat/lon."""
|
||||||
lat, lon = get_random_lat_lon()
|
lat, lon = get_random_lat_lon()
|
||||||
return build_detail_page(lat, lon)
|
|
||||||
|
elements = model.Polygon.coords_within(lat, lon)
|
||||||
|
result = do_lookup(elements, lat, lon)
|
||||||
|
|
||||||
|
return render_template(
|
||||||
|
"detail.html", lat=lat, lon=lon, result=result, elements=elements
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/wikidata_tag")
|
@app.route("/wikidata_tag")
|
||||||
|
@ -217,8 +224,14 @@ def wikidata_tag() -> str:
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def build_detail_page(lat: float, lon: float) -> str:
|
@app.route("/detail")
|
||||||
"""Run lookup and build detail page."""
|
def detail_page() -> Response | str:
|
||||||
|
"""Detail page."""
|
||||||
|
try:
|
||||||
|
lat_str, lon_str = request.args["lat"], request.args["lon"]
|
||||||
|
lat, lon = float(lat_str), float(lon_str)
|
||||||
|
except TypeError:
|
||||||
|
return redirect(url_for("index"))
|
||||||
try:
|
try:
|
||||||
reply = lat_lon_to_wikidata(lat, lon)
|
reply = lat_lon_to_wikidata(lat, lon)
|
||||||
except wikidata.QueryError as e:
|
except wikidata.QueryError as e:
|
||||||
|
@ -239,17 +252,5 @@ def build_detail_page(lat: float, lon: float) -> str:
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/detail")
|
|
||||||
def detail_page() -> Response | str:
|
|
||||||
"""Detail page."""
|
|
||||||
try:
|
|
||||||
lat_str, lon_str = request.args["lat"], request.args["lon"]
|
|
||||||
lat, lon = float(lat_str), float(lon_str)
|
|
||||||
except TypeError:
|
|
||||||
return redirect(url_for("index"))
|
|
||||||
|
|
||||||
return build_detail_page(lat, lon)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app.run(host="0.0.0.0")
|
app.run(host="0.0.0.0")
|
||||||
|
|
Loading…
Reference in a new issue