From affb70d1152d2dae580e54831af916d0ede3ad9b Mon Sep 17 00:00:00 2001 From: Dustin Lo Date: Thu, 26 May 2022 15:59:36 -0700 Subject: [PATCH] HC-420: optional reverse geolocation (#50) * wrapping reverse geolocation with try...except in case there is no geonames index * fixed dataset.py, put the try...except in the wrong place * bumped version Co-authored-by: dustinlo --- grq2/lib/dataset.py | 11 ++++++--- grq2/lib/geonames.py | 53 +++++++++++++++++++++++++++++--------------- setup.py | 2 +- 3 files changed, 44 insertions(+), 22 deletions(-) diff --git a/grq2/lib/dataset.py b/grq2/lib/dataset.py index 3b34b43..80d15c9 100644 --- a/grq2/lib/dataset.py +++ b/grq2/lib/dataset.py @@ -100,15 +100,20 @@ def update(update_json): if geo_json_type in (_POLYGON, _MULTIPOLYGON): mp = True if geo_json_type == _MULTIPOLYGON else False coords = location['coordinates'][0] - update_json['city'] = get_cities(coords, multipolygon=mp) + cities = get_cities(coords, multipolygon=mp) + if cities: + update_json['city'] = cities elif geo_json_type in (_POINT, _MULTIPOINT, _LINESTRING, _MULTILINESTRING): - update_json['city'] = get_nearest_cities(lon, lat) + nearest_cities = get_nearest_cities(lon, lat) + if nearest_cities: + update_json['city'] = nearest_cities else: raise TypeError('%s is not a valid GEOJson type (or un-supported): %s' % (geo_json_type, GEOJSON_TYPES)) # add closest continent continents = get_continents(lon, lat) - update_json['continent'] = continents[0]['name'] if len(continents) > 0 else None + if continents: + update_json['continent'] = continents[0]['name'] if len(continents) > 0 else None # set temporal_span if update_json.get('starttime', None) is not None and update_json.get('endtime', None) is not None: diff --git a/grq2/lib/geonames.py b/grq2/lib/geonames.py index 9dc6d39..9f28e37 100644 --- a/grq2/lib/geonames.py +++ b/grq2/lib/geonames.py @@ -6,6 +6,7 @@ standard_library.install_aliases() import json +from elasticsearch.exceptions import NotFoundError from grq2 import app, grq_es @@ -108,13 +109,18 @@ def get_cities(polygon, size=5, multipolygon=False): }) index = app.config['GEONAMES_INDEX'] - res = grq_es.search(index=index, body=query) # query for results - app.logger.debug("get_cities(): %s" % json.dumps(query)) + try: + res = grq_es.search(index=index, body=query) # query for results + app.logger.debug("get_cities(): %s" % json.dumps(query)) - results = [] - for hit in res['hits']['hits']: - results.append(hit['_source']) - return results + results = [] + for hit in res['hits']['hits']: + results.append(hit['_source']) + return results + except NotFoundError: + return None + except Exception as e: + raise Exception(e) def get_nearest_cities(lon, lat, size=5): @@ -160,14 +166,20 @@ def get_nearest_cities(lon, lat, size=5): } } } + index = app.config['GEONAMES_INDEX'] # query for results - res = grq_es.search(index=index, body=query) - app.logger.debug("get_continents(): %s" % json.dumps(query, indent=2)) + try: + res = grq_es.search(index=index, body=query) + app.logger.debug("get_continents(): %s" % json.dumps(query, indent=2)) - results = [] - for hit in res['hits']['hits']: - results.append(hit['_source']) - return results + results = [] + for hit in res['hits']['hits']: + results.append(hit['_source']) + return results + except NotFoundError: + return None + except Exception as e: + raise Exception(e) def get_continents(lon, lat): @@ -236,10 +248,15 @@ def get_continents(lon, lat): } index = app.config['GEONAMES_INDEX'] # query for results - res = grq_es.search(index=index, body=query) - app.logger.debug("get_continents(): %s" % json.dumps(query, indent=2)) + try: + res = grq_es.search(index=index, body=query) + app.logger.debug("get_continents(): %s" % json.dumps(query, indent=2)) - results = [] - for hit in res['hits']['hits']: - results.append(hit['_source']) - return results + results = [] + for hit in res['hits']['hits']: + results.append(hit['_source']) + return results + except NotFoundError: + return None + except Exception as e: + raise Exception(e) diff --git a/setup.py b/setup.py index 2d5531b..5dc306e 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ setup( name='grq2', - version='2.0.15', + version='2.0.16', long_description='GeoRegionQuery REST API using ElasticSearch backend', packages=find_packages(), include_package_data=True,