Compare commits
3 commits
b700f88da6
...
690d3281df
Author | SHA1 | Date | |
---|---|---|---|
Edward Betts | 690d3281df | ||
Edward Betts | ea2f172388 | ||
Edward Betts | cde9d09eb0 |
|
@ -38,4 +38,9 @@ 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"),
|
||||
]
|
||||
|
|
39
lookup.py
39
lookup.py
|
@ -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,7 +16,6 @@ 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]:
|
||||
|
@ -32,7 +31,7 @@ def get_random_lat_lon() -> tuple[float, float]:
|
|||
|
||||
|
||||
def do_lookup(
|
||||
elements: Elements, lat: str | float, lon: str | float
|
||||
elements: "Query[model.Polygon]", lat: str | float, lon: str | float
|
||||
) -> wikidata.WikidataDict:
|
||||
"""Do lookup."""
|
||||
try:
|
||||
|
@ -125,7 +124,7 @@ def hit_from_name(
|
|||
|
||||
|
||||
def osm_lookup(
|
||||
elements: Elements, lat: str | float, lon: str | float
|
||||
elements: "Query[model.Polygon]", lat: str | float, lon: str | float
|
||||
) -> wikidata.Hit | None:
|
||||
"""OSM lookup."""
|
||||
ret: wikidata.Hit | None
|
||||
|
@ -189,16 +188,10 @@ def index() -> str | Response:
|
|||
|
||||
|
||||
@app.route("/random")
|
||||
def random_location() -> str:
|
||||
def random_location() -> str | Response:
|
||||
"""Return detail page for random lat/lon."""
|
||||
lat, lon = get_random_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
|
||||
)
|
||||
return build_detail_page(lat, lon)
|
||||
|
||||
|
||||
@app.route("/wikidata_tag")
|
||||
|
@ -224,14 +217,8 @@ def wikidata_tag() -> 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"))
|
||||
def build_detail_page(lat: float, lon: float) -> str:
|
||||
"""Run lookup and build detail page."""
|
||||
try:
|
||||
reply = lat_lon_to_wikidata(lat, lon)
|
||||
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__":
|
||||
app.run(host="0.0.0.0")
|
||||
|
|
Loading…
Reference in a new issue