Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix geocoding autocompletion in curator UI #2530

Merged
merged 2 commits into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions geocoding/location-service/src/app/geocoder.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import iso3166
import json
import logging
import ratelimiter
import sys

from lru import LRU

from src.integration.mapbox_client import mapbox_geocode

h = logging.StreamHandler(sys.stdout)
logger = logging.getLogger(__name__)
logger.addHandler(h)
logger.setLevel(logging.INFO)

class Geocoder:
Country = 'Country'
Expand Down Expand Up @@ -57,17 +65,30 @@ def getISO3166Code(self, countryName):
countryName: The country name, or sub-string thereof, to find.
Return: The ISO-3166 two-letter code, or None if the countryName was unfound or ambiguous.
"""
logger.debug(f"Getting country code for {countryName}")
# workaround a common problem: "United States" is the Mapbox name for US, but the algorithm
# below will also find UM (United States Minor Outlying Islands) and fail because the
# query is ambiguous.
fixups = {
'united states': 'US'
}
name = countryName.lower()
if fixedCode := fixups.get(name):
return fixedCode

country = None
for key in Geocoder.INDEX:
if name in key:
if country is not None:
# Ambiguous countryName
logger.error(f"Country {countryName} is ambiguous, found both {country} and {Geocoder.INDEX[key]}")
return None
country = Geocoder.INDEX[key]

if country is not None:
logger.debug(f"code for {countryName} is {country.alpha2}")
return country.alpha2
logger.error(f"Country {countryName} not found in iso3166.countries!")
return None


Expand Down
13 changes: 13 additions & 0 deletions geocoding/location-service/src/app/geocoder_suggester.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import logging
import sys

from src.app.geocoder import Geocoder

h = logging.StreamHandler(sys.stdout)
logger = logging.getLogger(__name__)
logger.addHandler(h)
logger.setLevel(logging.INFO)

class GeocodeSuggester:
"""Suggest geocoding by asking a cohort of geocoders to locate the query."""

Expand All @@ -17,14 +25,19 @@ def suggest(self, request):
"""Interpret the request and pass it to the geocoders until one of them
returns a geocode result."""
if 'q' not in request:
logger.debug('empty query')
return []
logger.debug(f"suggesting for query: {request['q']}")
opts = { }
if 'limitToResolution' in request:
opts['limitToResolution'] = [self.limitToValidResolution(x) for x in request['limitToResolution'].split(',')]
if 'limitToCountry' in request:
opts['limitToCountry'] = request['limitToCountry'].split(',')
logger.debug(f"opts: {opts}")
for g in self.geocoders:
suggestions = g.geocode(request['q'], opts)
if len(suggestions) > 0:
logger.debug("geocoder {g} suggests {suggestions}")
return suggestions
logger.debug(f"No suggestions for query {request['q']}")
return []
23 changes: 14 additions & 9 deletions geocoding/location-service/src/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@

geocoders = []

h = logging.StreamHandler(sys.stdout)
logger = logging.getLogger(__name__)
logger.addHandler(h)
logger.setLevel(logging.INFO)

if 'ENABLE_FAKE_GEOCODER' in environ:
faking_it = Blueprint('fake_geocoder', __name__)
fake_geocoder = FakeGeocoder()
Expand Down Expand Up @@ -61,7 +66,9 @@ def index() -> str:
def geocode():
query = request.args.get('q', type=str)
if not query:
logger.warning(f"No query supplied in request {request}")
return "No query supplied", 400
logger.info(f"geocoding query {query}")
options = {}
resolution = request.args.get('limitToResolution', '[]', type=str)
listOfResolutions = json.loads(resolution)
Expand All @@ -71,28 +78,26 @@ def geocode():
listOfCountries = json.loads(countries)
if len(listOfCountries) > 0:
options['limitToCountry'] = listOfCountries
logger.debug(f"options {options}")
for g in geocoders:
locations = g.geocode(query, options)
if len(locations) > 0:
logger.debug("responding with locations: {locations}")
return jsonify(locations)
logger.debug("empty response")
return jsonify([])


@app.route("/geocode/suggest")
def suggest_geocode():
try:
return jsonify(suggester.suggest(request.args))
logger.info(f"suggestion based on {request.args}")
r = jsonify(suggester.suggest(request.args))
logger.info(f"suggestions: {r.get_data()}")
return r
except ValueError:
return f"Bad limitToResolution value {request.args.get('limitToResolution', str)}", 422


def setup_logger():
h = logging.StreamHandler(sys.stdout)
rootLogger = logging.getLogger()
rootLogger.addHandler(h)
rootLogger.setLevel(logging.DEBUG)


if __name__ == '__main__':
setup_logger()
app.run(host='0.0.0.0', port=8080)
2 changes: 2 additions & 0 deletions geocoding/location-service/test/test_fake_geocoder.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os
import unittest

os.environ['ENABLE_FAKE_GEOCODER'] = 'YES'
from src.app import main


Expand Down
5 changes: 5 additions & 0 deletions geocoding/location-service/test/test_geocoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,8 @@ def test_cachesMapboxResponses(self, mapbox_patch):
})
mapbox_patch.assert_called_once()
assert feats == feats2

def test_canFindCodeForUnitedStates(self):
geocoder = Geocoder('api_token', FakeAdminsFetcher())
code = geocoder.getISO3166Code('United States')
assert code == 'US'