Bug fixes and improvements.

This commit is contained in:
Edward Betts 2023-10-10 07:27:25 +00:00
parent 89549b750f
commit faa0b3cb4e
4 changed files with 14 additions and 6 deletions

View file

@ -4,7 +4,7 @@ from sqlalchemy.types import Integer, Float, Numeric, String
from sqlalchemy.dialects import postgresql from sqlalchemy.dialects import postgresql
from sqlalchemy.orm import column_property from sqlalchemy.orm import column_property
from sqlalchemy.ext.hybrid import hybrid_property from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy import func from sqlalchemy import func, cast
from geoalchemy2 import Geometry from geoalchemy2 import Geometry
from .database import session from .database import session
@ -36,8 +36,9 @@ class Polygon(Base):
def coords_within(cls, lat, lon): def coords_within(cls, lat, lon):
point = func.ST_SetSRID(func.ST_MakePoint(lon, lat), 4326) point = func.ST_SetSRID(func.ST_MakePoint(lon, lat), 4326)
return (cls.query.filter(cls.admin_level.isnot(None), return (cls.query.filter(cls.admin_level.isnot(None),
cls.admin_level.regexp_match("^\d+$"),
func.ST_Within(point, cls.way)) func.ST_Within(point, cls.way))
.order_by(cls.area)) .order_by(cls.area, cast(cls.admin_level, Integer).desc()))
class Scotland(Base): class Scotland(Base):
__tablename__ = "scotland" __tablename__ = "scotland"

View file

@ -148,10 +148,15 @@ def build_dict(hit, lat, lon):
if hit is None: if hit is None:
return dict(commons_cat=None, missing=True, coords=coords) return dict(commons_cat=None, missing=True, coords=coords)
commons_cat = hit["commons_cat"] commons_cat = hit["commons_cat"]
url = commons_cat_start + urllib.parse.quote(commons_cat.replace(" ", "_")) ret = dict(
return dict(
commons_cat={"title": commons_cat, "url": url},
coords=coords, coords=coords,
admin_level=hit.get("admin_level"), admin_level=hit.get("admin_level"),
wikidata=hit["wikidata"], wikidata=hit["wikidata"],
) )
if not commons_cat:
return ret
url = commons_cat_start + urllib.parse.quote(commons_cat.replace(" ", "_"))
ret["commons_cat"] = {"title": commons_cat, "url": url}
return ret

View file

@ -2,6 +2,7 @@
from flask import Flask, render_template, request, jsonify, redirect, url_for from flask import Flask, render_template, request, jsonify, redirect, url_for
from geocode import wikidata, scotland, database, model from geocode import wikidata, scotland, database, model
from pprint import pprint
import geocode import geocode
import random import random
@ -108,7 +109,7 @@ def osm_lookup(elements, lat, lon):
ret["admin_level"] = admin_level ret["admin_level"] = admin_level
return ret return ret
has_wikidata_tag = [e["tags"] for e in elements if "wikidata" in e["tags"]] has_wikidata_tag = [e.tags for e in elements if e.tags.get("wikidata")]
if len(has_wikidata_tag) != 1: if len(has_wikidata_tag) != 1:
return return

View file

@ -12,6 +12,7 @@ SELECT DISTINCT ?item ?distance ?itemLabel ?isa ?isaLabel ?commonsCat ?commonsSi
} }
} }
MINUS { ?item wdt:P582 ?endTime . } MINUS { ?item wdt:P582 ?endTime . }
MINUS { ?item wdt:P31 wd:Q1497375 . }
OPTIONAL { ?item wdt:P373 ?commonsCat. } OPTIONAL { ?item wdt:P373 ?commonsCat. }
OPTIONAL { ?commonsSiteLink schema:about ?item; OPTIONAL { ?commonsSiteLink schema:about ?item;
schema:isPartOf <https://commons.wikimedia.org/>. } schema:isPartOf <https://commons.wikimedia.org/>. }