diff --git a/ConfigDialog.py b/ConfigDialog.py index 3c4f727..2973a28 100644 --- a/ConfigDialog.py +++ b/ConfigDialog.py @@ -43,6 +43,8 @@ def __init__(self, caller): # stupid qvariant return a tuple... zoom_scale = caller.get_config('ZoomScale', 0) + google_key = caller.get_config('googleKey', '') + tomtom_key = caller.get_config('tomtomKey', '') # Use pdb for debugging #import pdb ## These lines allow you to set a breakpoint in the app @@ -50,6 +52,8 @@ def __init__(self, caller): #pdb.set_trace() self.ZoomScale.setValue(int(zoom_scale)) + self.googleKey.setText(google_key) + self.tomtomKey.setText(tomtom_key) self.debugCheckBox.setChecked(caller.get_config('writeDebug', '') == True) diff --git a/GeoCoding.py b/GeoCoding.py index 24840d4..4648ac7 100644 --- a/GeoCoding.py +++ b/GeoCoding.py @@ -113,6 +113,7 @@ def config(self): geocoders = { 'Nominatim (Openstreetmap)' : 'Nominatim', 'Google' : 'GoogleV3', + 'TomTom': 'TomTom', } # Get current index try: @@ -132,6 +133,7 @@ def config(self): self.set_config('ZoomScale', dlg.ZoomScale.text()) self.set_config('writeDebug', dlg.debugCheckBox.isChecked()) self.set_config('googleKey', dlg.googleKey.text()) + self.set_config('tomtomKey', dlg.tomtomKey.text()) def about(self): infoString = QCoreApplication.translate('GeoCoding', "Python GeoCoding Plugin
This plugin provides GeoCoding functions using webservices.
Author: Alessandro Pasotti (aka: elpaso)
Mail: info@itopen.it
Web: www.itopen.it
" + "Do yo like this plugin? Please consider donating.") @@ -253,8 +255,10 @@ def get_geocoder_instance(self): if geocoder_class == 'Nominatim': return OsmGeoCoder() - else: + elif geocoder_class == 'GoogleV3': return GoogleGeoCoder(self.get_config('googleKey')) + else: + return TomTomGeoCoder(self.get_config('tomtomKey')) diff --git a/Ui_Config.ui b/Ui_Config.ui index 459a563..9c98f9d 100644 --- a/Ui_Config.ui +++ b/Ui_Config.ui @@ -85,6 +85,20 @@ + + + + + + + TomTom API Key (optional) + + + + + + + diff --git a/geocoders.py b/geocoders.py index 3549657..e356936 100644 --- a/geocoders.py +++ b/geocoders.py @@ -62,11 +62,12 @@ class GoogleGeoCoder(): def __init__(self, api_key=None): self.api_key = api_key + url = self.url def geocode(self, address): if self.api_key is not None and self.api_key.replace(' ', '') != '': - url += self.url + '&key=' + self.api_key + url = self.url + '&key=' + self.api_key else: url = self.url @@ -91,4 +92,41 @@ def reverse(self, lon, lat): except Exception as e: raise GeoCodeException(str(e)) +class TomTomGeoCoder(): + + url = 'https://api.tomtom.com/search/2/geocode/{address}.json?' + reverse_url = 'https://api.tomtom.com/search/2/reverseGeocode/{lat},{lon}.json?' + + def __init__(self, tomtomKey=None): + self.api_key = tomtomKey + url = self.url + + def geocode(self, address): + + if self.api_key is not None and self.api_key.replace(' ', '') != '': + url = self.url + '&key=' + self.api_key + else: + url = self.url + + try: + url = url.format(**{'address': address.decode('utf8')}) + logMessage(url) + results = json.loads(NAM.request(url, blocking=True)[1].decode('utf8'))['results'] + return [(rec['address']['freeformAddress'] + ' (' + rec['type'] + ')', (rec['position']['lon'], rec['position']['lat'])) for rec in results] + except Exception as e: + raise GeoCodeException(str(e)) + + def reverse(self, lon, lat): + if self.api_key is not None and self.api_key.replace(' ', '') != '': + url = self.reverse_url + '&key=' + self.api_key + else: + url = self.reverse_url + try: + url = url.format(**{'lon': lon, 'lat': lat}) + logMessage(url) + results = json.loads(NAM.request(url, blocking=True)[1].decode('utf8'))['addresses'] + return [(rec['address']['freeformAddress'], (rec['position'])) for rec in results] + except Exception as e: + raise GeoCodeException(str(e)) +