Compare commits

...

3 commits

Author SHA1 Message Date
Edward Betts 690d3281df Make random page show polygon on map 2023-11-04 21:36:05 +00:00
Edward Betts ea2f172388 Fixed type hints 2023-11-04 21:35:33 +00:00
Edward Betts cde9d09eb0 More samples 2023-11-04 21:23:15 +00:00
2 changed files with 24 additions and 20 deletions

View file

@ -38,4 +38,9 @@ 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"),
] ]

View file

@ -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,7 +16,6 @@ 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]:
@ -32,7 +31,7 @@ def get_random_lat_lon() -> tuple[float, float]:
def do_lookup( def do_lookup(
elements: Elements, lat: str | float, lon: str | float elements: "Query[model.Polygon]", lat: str | float, lon: str | float
) -> wikidata.WikidataDict: ) -> wikidata.WikidataDict:
"""Do lookup.""" """Do lookup."""
try: try:
@ -125,7 +124,7 @@ def hit_from_name(
def osm_lookup( def osm_lookup(
elements: Elements, lat: str | float, lon: str | float elements: "Query[model.Polygon]", lat: str | float, lon: str | float
) -> wikidata.Hit | None: ) -> wikidata.Hit | None:
"""OSM lookup.""" """OSM lookup."""
ret: wikidata.Hit | None ret: wikidata.Hit | None
@ -189,16 +188,10 @@ def index() -> str | Response:
@app.route("/random") @app.route("/random")
def random_location() -> str: def random_location() -> str | Response:
"""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")
@ -224,14 +217,8 @@ def wikidata_tag() -> str:
) )
@app.route("/detail") def build_detail_page(lat: float, lon: float) -> str:
def detail_page() -> Response | str: """Run lookup and build detail page."""
"""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:
@ -252,5 +239,17 @@ def detail_page() -> Response | 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")