two newlines between functions

This commit is contained in:
Edward Betts 2021-04-16 16:14:32 +01:00
parent 365d81a1f0
commit b8e217d9e4

View file

@ -112,6 +112,7 @@ def wikidata_tag():
return render_template('wikidata_tag.html', lat=lat, lon=lon, result=result, elements=elements) return render_template('wikidata_tag.html', lat=lat, lon=lon, result=result, elements=elements)
@app.route("/detail") @app.route("/detail")
def detail_page(): def detail_page():
try: try:
@ -121,6 +122,7 @@ def detail_page():
reply = lat_lon_to_wikidata(lat, lon) reply = lat_lon_to_wikidata(lat, lon)
return render_template('random.html', lat=lat, lon=lon, **reply) return render_template('random.html', lat=lat, lon=lon, **reply)
def bounding_box_area(element): def bounding_box_area(element):
bbox = element['bounds'] bbox = element['bounds']
@ -129,15 +131,18 @@ def bounding_box_area(element):
return x.km * y.km return x.km * y.km
def wd_to_qid(wd): def wd_to_qid(wd):
# expecting {'type': 'url', 'value': 'https://www.wikidata.org/wiki/Q30'} # expecting {'type': 'url', 'value': 'https://www.wikidata.org/wiki/Q30'}
if wd['type'] == 'uri': if wd['type'] == 'uri':
return wd_uri_to_qid(wd['value']) return wd_uri_to_qid(wd['value'])
def wd_uri_to_qid(value): def wd_uri_to_qid(value):
assert value.startswith(wd_entity) assert value.startswith(wd_entity)
return value[len(wd_entity) - 1:] return value[len(wd_entity) - 1:]
def build_dict(hit, lat, lon): def build_dict(hit, lat, lon):
coords = {'lat': lat, 'lon': lon} coords = {'lat': lat, 'lon': lon}
if hit is None: if hit is None:
@ -162,6 +167,7 @@ def do_lookup(elements, lat, lon):
return build_dict(hit, lat, lon) return build_dict(hit, lat, lon)
def get_scotland_code(lat, lon): def get_scotland_code(lat, lon):
conn = psycopg2.connect(dbname='geocode', user='geocode', password='ooK3ohgh', host='localhost') conn = psycopg2.connect(dbname='geocode', user='geocode', password='ooK3ohgh', host='localhost')
cur = conn.cursor() cur = conn.cursor()
@ -179,6 +185,7 @@ def get_scotland_code(lat, lon):
if row: if row:
return row[0] return row[0]
def wdqs_geosearch_query(lat, lon): def wdqs_geosearch_query(lat, lon):
if isinstance(lat, float): if isinstance(lat, float):
lat = f'{lat:f}' lat = f'{lat:f}'
@ -211,6 +218,7 @@ SELECT DISTINCT ?item ?distance ?itemLabel ?isa ?isaLabel ?commonsCat ?commonsSi
reply = wdqs(query) reply = wdqs(query)
return reply['results']['bindings'] return reply['results']['bindings']
def wdqs_geosearch(lat, lon): def wdqs_geosearch(lat, lon):
default_max_dist = 1 default_max_dist = 1
rows = wdqs_geosearch_query(lat, lon) rows = wdqs_geosearch_query(lat, lon)
@ -237,6 +245,7 @@ def wdqs_geosearch(lat, lon):
return row return row
def lat_lon_to_wikidata(lat, lon): def lat_lon_to_wikidata(lat, lon):
scotland_code = get_scotland_code(lat, lon) scotland_code = get_scotland_code(lat, lon)
@ -268,6 +277,7 @@ def lat_lon_to_wikidata(lat, lon):
return {'elements': elements, 'result': result} return {'elements': elements, 'result': result}
@app.route("/") @app.route("/")
def index(): def index():
q = request.args.get('q') q = request.args.get('q')
@ -283,6 +293,7 @@ def index():
return jsonify(lat_lon_to_wikidata(lat, lon)['result']) return jsonify(lat_lon_to_wikidata(lat, lon)['result'])
def wikidata_api_call(params): def wikidata_api_call(params):
call_params = { call_params = {
'format': 'json', 'format': 'json',
@ -303,6 +314,7 @@ def get_entity(qid):
if 'missing' not in entity: if 'missing' not in entity:
return entity return entity
def qid_to_commons_category(qid): def qid_to_commons_category(qid):
entity = get_entity(qid) entity = get_entity(qid)
try: try:
@ -312,6 +324,7 @@ def qid_to_commons_category(qid):
return commons_cat return commons_cat
def wdqs(query): def wdqs(query):
r = requests.post(wikidata_query_api_url, r = requests.post(wikidata_query_api_url,
data={'query': query, 'format': 'json'}, data={'query': query, 'format': 'json'},
@ -322,17 +335,21 @@ def wdqs(query):
except simplejson.errors.JSONDecodeError: except simplejson.errors.JSONDecodeError:
raise QueryError(query, r) raise QueryError(query, r)
def endpoint(): def endpoint():
return OVERPASS_URL + '/api/interpreter' return OVERPASS_URL + '/api/interpreter'
def run_query(oql, error_on_rate_limit=True): def run_query(oql, error_on_rate_limit=True):
return requests.post(endpoint(), return requests.post(endpoint(),
data=oql.encode('utf-8'), data=oql.encode('utf-8'),
headers=headers) headers=headers)
def get_elements(oql): def get_elements(oql):
return run_query(oql).json()['elements'] return run_query(oql).json()['elements']
def is_in_lat_lon(lat, lon): def is_in_lat_lon(lat, lon):
oql = f''' oql = f'''
[out:json][timeout:25]; [out:json][timeout:25];
@ -342,6 +359,7 @@ out bb tags qt;'''
return run_query(oql) return run_query(oql)
def lookup_scottish_parish_in_wikidata(code): def lookup_scottish_parish_in_wikidata(code):
query = ''' query = '''
SELECT ?item ?itemLabel ?commonsSiteLink ?commonsCat WHERE { SELECT ?item ?itemLabel ?commonsSiteLink ?commonsCat WHERE {
@ -356,6 +374,7 @@ SELECT ?item ?itemLabel ?commonsSiteLink ?commonsCat WHERE {
reply = wdqs(query) reply = wdqs(query)
return reply['results']['bindings'] return reply['results']['bindings']
def lookup_gss_in_wikidata(gss): def lookup_gss_in_wikidata(gss):
query = ''' query = '''
SELECT ?item ?itemLabel ?commonsSiteLink ?commonsCat WHERE { SELECT ?item ?itemLabel ?commonsSiteLink ?commonsCat WHERE {
@ -369,6 +388,7 @@ SELECT ?item ?itemLabel ?commonsSiteLink ?commonsCat WHERE {
reply = wdqs(query) reply = wdqs(query)
return reply['results']['bindings'] return reply['results']['bindings']
def lookup_wikidata_by_name(name, lat, lon): def lookup_wikidata_by_name(name, lat, lon):
query = ''' query = '''
SELECT DISTINCT ?item ?itemLabel ?commonsSiteLink ?commonsCat WHERE { SELECT DISTINCT ?item ?itemLabel ?commonsSiteLink ?commonsCat WHERE {
@ -389,9 +409,11 @@ SELECT DISTINCT ?item ?itemLabel ?commonsSiteLink ?commonsCat WHERE {
reply = wdqs(query) reply = wdqs(query)
return reply['results']['bindings'] return reply['results']['bindings']
def unescape_title(t): def unescape_title(t):
return urllib.parse.unquote(t.replace('_', ' ')) return urllib.parse.unquote(t.replace('_', ' '))
def commons_from_rows(rows): def commons_from_rows(rows):
for row in rows: for row in rows:
if 'commonsCat' in row: if 'commonsCat' in row:
@ -404,11 +426,13 @@ def commons_from_rows(rows):
cat = unescape_title(site_link[len(commons_cat_start):]) cat = unescape_title(site_link[len(commons_cat_start):])
return {'wikidata': qid, 'commons_cat': cat} return {'wikidata': qid, 'commons_cat': cat}
def get_commons_cat_from_gss(gss): def get_commons_cat_from_gss(gss):
print('GSS:', gss) print('GSS:', gss)
rows = lookup_gss_in_wikidata(gss) rows = lookup_gss_in_wikidata(gss)
return commons_from_rows(rows) return commons_from_rows(rows)
def get_osm_elements(lat, lon): def get_osm_elements(lat, lon):
filename = f'cache/{lat}_{lon}.json' filename = f'cache/{lat}_{lon}.json'
@ -422,6 +446,7 @@ def get_osm_elements(lat, lon):
return elements return elements
def osm_lookup(elements, lat, lon): def osm_lookup(elements, lat, lon):
is_in = [] is_in = []
elements.sort(key=lambda e: bounding_box_area(e)) elements.sort(key=lambda e: bounding_box_area(e))