From 29a34abd0dd7b6379226c90548569c007761ab4b Mon Sep 17 00:00:00 2001 From: Guido Fioravantti Date: Tue, 3 Nov 2015 16:26:54 +0100 Subject: [PATCH] Initial commit for heremaps module --- lib/python/heremaps/__init__.py | 0 lib/python/heremaps/heremapsexceptions.py | 11 +++ lib/python/heremaps/heremapsgeocoder.py | 103 ++++++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 lib/python/heremaps/__init__.py create mode 100644 lib/python/heremaps/heremapsexceptions.py create mode 100644 lib/python/heremaps/heremapsgeocoder.py diff --git a/lib/python/heremaps/__init__.py b/lib/python/heremaps/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/lib/python/heremaps/heremapsexceptions.py b/lib/python/heremaps/heremapsexceptions.py new file mode 100644 index 0000000..cced7a6 --- /dev/null +++ b/lib/python/heremaps/heremapsexceptions.py @@ -0,0 +1,11 @@ +import json + +class BadGeocodingParams(Exception): + def __init__(self, value): + self.value = value + def __str__(self): + return repr('Bad geocoding params: ' + json.dumps(value)) + +class NoGeocodingParams(Exception): + def __str__(self): + return repr('No params for geocoding specified') diff --git a/lib/python/heremaps/heremapsgeocoder.py b/lib/python/heremaps/heremapsgeocoder.py new file mode 100644 index 0000000..eb5c931 --- /dev/null +++ b/lib/python/heremaps/heremapsgeocoder.py @@ -0,0 +1,103 @@ +import inspect +import json +import urllib + +from heremapsexceptions import BadGeocodingParams, NoGeocodingParams + +class Geocoder: + 'A Here Maps Geocoder wrapper for python' + + URL_GEOCODE_JSON = 'http://geocoder.cit.api.here.com/6.2/geocode.json' + MAX_RESULTS = 1 + + ADDRESS_PARAMS = [ + 'city', + 'country', + 'county', + 'district', + 'housenumber', + 'postalcode', + 'searchtext', + 'state', + 'street' + ] + + ADMITTED_PARAMS = [ + 'additionaldata', + 'app_id', + 'app_code', + 'bbox', + 'countryfocus', + 'gen', + 'jsonattributes', + 'jsoncallback', + 'language', + 'locationattributes', + 'locationid', + 'mapview', + 'maxresults', + 'pageinformation', + 'politicalview', + 'prox', + 'strictlanguagemode' + ] + ADDRESS_PARAMS + + app_id = '' + app_code = '' + + def __init__(self, app_id, app_code): + self.app_id = app_id + self.app_code = app_code + + def geocode(self, params): + if not set(params.keys()).issubset(set(self.ADDRESS_PARAMS)): + raise BadGeocodingParams(params) + + request_params = { + 'app_id' : self.app_id, + 'app_code' : self.app_code, + 'maxresults' : self.MAX_RESULTS, + 'gen' : '9' + } + request_params.update(params) + + encoded_request_params = urllib.urlencode(request_params) + + response = json.load( + urllib.urlopen(self.URL_GEOCODE_JSON + + '?' + + encoded_request_params)) + + return response + + def geocodeAddress(self, + searchtext=None, + city=None, + country=None, + county=None, + district=None, + housenumber=None, + postalcode=None, + state=None, + street=None): + frame = inspect.currentframe() + keys, _, _, values = inspect.getargvalues(frame) + + iterableKeys = iter(keys) + next(iterableKeys) + + params = {} + for key in iterableKeys: + if values[key]: params[key] = values[key] + + if not params: raise NoGeocodingParams() + + return self.geocode(params) + + def extractLngLatFromResponse(response): + location = response['Response']['View'][0]['Result'][0]['Location'] + + longitude = location['DisplayPosition']['Longitude'] + latitude = location['DisplayPosition']['Latitude'] + + return [longitude, latitude]