Compare commits

..

No commits in common. "690d3281df5c5790ae6f7a1bb2dfd5ea2e3d2f39" and "b700f88da66cf06d14001c8809954e6cf28155dc" have entirely different histories.

2 changed files with 20 additions and 24 deletions

View file

@ -38,9 +38,4 @@ samples = [
(51.0804, -2.3263, "Zeals"),
(55.7644, -4.1770, "East Kilbride"),
(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"),
]

View file

@ -3,8 +3,8 @@
import random
import typing
import sqlalchemy
from flask import Flask, jsonify, redirect, render_template, request, url_for
from sqlalchemy.orm.query import Query
from werkzeug.wrappers import Response
import geocode
@ -16,6 +16,7 @@ app.config.from_object("config.default")
database.init_app(app)
Tags = typing.Mapping[str, str]
Elements = sqlalchemy.orm.query.Query[model.Polygon]
def get_random_lat_lon() -> tuple[float, float]:
@ -31,7 +32,7 @@ def get_random_lat_lon() -> tuple[float, float]:
def do_lookup(
elements: "Query[model.Polygon]", lat: str | float, lon: str | float
elements: Elements, lat: str | float, lon: str | float
) -> wikidata.WikidataDict:
"""Do lookup."""
try:
@ -124,7 +125,7 @@ def hit_from_name(
def osm_lookup(
elements: "Query[model.Polygon]", lat: str | float, lon: str | float
elements: Elements, lat: str | float, lon: str | float
) -> wikidata.Hit | None:
"""OSM lookup."""
ret: wikidata.Hit | None
@ -188,10 +189,16 @@ def index() -> str | Response:
@app.route("/random")
def random_location() -> str | Response:
def random_location() -> str:
"""Return detail page for 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")
@ -217,8 +224,14 @@ def wikidata_tag() -> str:
)
def build_detail_page(lat: float, lon: float) -> str:
"""Run lookup and build detail page."""
@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"))
try:
reply = lat_lon_to_wikidata(lat, lon)
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__":
app.run(host="0.0.0.0")