Skip to content

Commit

Permalink
HC-420: optional reverse geolocation (#50)
Browse files Browse the repository at this point in the history
* 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 <dustin.k.lo@jpl.nasa.gov>
  • Loading branch information
DustinKLo and dustinlo authored May 26, 2022
1 parent 88c70b1 commit affb70d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 22 deletions.
11 changes: 8 additions & 3 deletions grq2/lib/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
53 changes: 35 additions & 18 deletions grq2/lib/geonames.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
standard_library.install_aliases()

import json
from elasticsearch.exceptions import NotFoundError
from grq2 import app, grq_es


Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit affb70d

Please sign in to comment.