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]