From faa0b3cb4e2c8ee7f48c17504c280f2dacfae884 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Tue, 10 Oct 2023 07:27:25 +0000 Subject: [PATCH] Bug fixes and improvements. --- geocode/model.py | 5 +++-- geocode/wikidata.py | 11 ++++++++--- lookup.py | 3 ++- templates/sparql/geosearch.sparql | 1 + 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/geocode/model.py b/geocode/model.py index 67cc45f..8230b0b 100644 --- a/geocode/model.py +++ b/geocode/model.py @@ -4,7 +4,7 @@ from sqlalchemy.types import Integer, Float, Numeric, String from sqlalchemy.dialects import postgresql from sqlalchemy.orm import column_property from sqlalchemy.ext.hybrid import hybrid_property -from sqlalchemy import func +from sqlalchemy import func, cast from geoalchemy2 import Geometry from .database import session @@ -36,8 +36,9 @@ class Polygon(Base): def coords_within(cls, lat, lon): point = func.ST_SetSRID(func.ST_MakePoint(lon, lat), 4326) return (cls.query.filter(cls.admin_level.isnot(None), + cls.admin_level.regexp_match("^\d+$"), func.ST_Within(point, cls.way)) - .order_by(cls.area)) + .order_by(cls.area, cast(cls.admin_level, Integer).desc())) class Scotland(Base): __tablename__ = "scotland" diff --git a/geocode/wikidata.py b/geocode/wikidata.py index 226ecab..7588816 100644 --- a/geocode/wikidata.py +++ b/geocode/wikidata.py @@ -148,10 +148,15 @@ def build_dict(hit, lat, lon): if hit is None: return dict(commons_cat=None, missing=True, coords=coords) commons_cat = hit["commons_cat"] - url = commons_cat_start + urllib.parse.quote(commons_cat.replace(" ", "_")) - return dict( - commons_cat={"title": commons_cat, "url": url}, + ret = dict( coords=coords, admin_level=hit.get("admin_level"), 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 diff --git a/lookup.py b/lookup.py index 7644c6e..65e6270 100755 --- a/lookup.py +++ b/lookup.py @@ -2,6 +2,7 @@ from flask import Flask, render_template, request, jsonify, redirect, url_for from geocode import wikidata, scotland, database, model +from pprint import pprint import geocode import random @@ -108,7 +109,7 @@ def osm_lookup(elements, lat, lon): ret["admin_level"] = admin_level 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: return diff --git a/templates/sparql/geosearch.sparql b/templates/sparql/geosearch.sparql index 8cbdb20..55ea42f 100644 --- a/templates/sparql/geosearch.sparql +++ b/templates/sparql/geosearch.sparql @@ -12,6 +12,7 @@ SELECT DISTINCT ?item ?distance ?itemLabel ?isa ?isaLabel ?commonsCat ?commonsSi } } MINUS { ?item wdt:P582 ?endTime . } + MINUS { ?item wdt:P31 wd:Q1497375 . } OPTIONAL { ?item wdt:P373 ?commonsCat. } OPTIONAL { ?commonsSiteLink schema:about ?item; schema:isPartOf . }