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

Added TomTom Geocoding service #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions ConfigDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,17 @@ 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
#pyqtRemoveInputHook()
#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)


6 changes: 5 additions & 1 deletion GeoCoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def config(self):
geocoders = {
'Nominatim (Openstreetmap)' : 'Nominatim',
'Google' : 'GoogleV3',
'TomTom': 'TomTom',
}
# Get current index
try:
Expand All @@ -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<br>This plugin provides GeoCoding functions using webservices.<br>Author: Alessandro Pasotti (aka: elpaso)<br>Mail: <a href=\"mailto:info@itopen.it\">info@itopen.it</a><br>Web: <a href=\"http://www.itopen.it\">www.itopen.it</a><br>" + "<b>Do yo like this plugin? Please consider <a href=\"https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=TJHLD5DY4LAFQ\">donating</a></b>.")
Expand Down Expand Up @@ -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'))



Expand Down
14 changes: 14 additions & 0 deletions Ui_Config.ui
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@
<widget class="QLineEdit" name="googleKey"/>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>TomTom API Key (optional)</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="tomtomKey"/>
</item>
</layout>
</item>
<item>
<widget class="QCheckBox" name="debugCheckBox">
Expand Down
40 changes: 39 additions & 1 deletion geocoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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))