2015-11-05 01:12:54 +08:00
|
|
|
#!/usr/local/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2015-11-03 23:26:54 +08:00
|
|
|
import json
|
|
|
|
import urllib
|
|
|
|
|
2015-11-05 01:23:08 +08:00
|
|
|
from heremaps.heremapsexceptions import BadGeocodingParams
|
|
|
|
from heremaps.heremapsexceptions import EmptyGeocoderResponse
|
|
|
|
from heremaps.heremapsexceptions import NoGeocodingParams
|
2015-11-05 18:12:10 +08:00
|
|
|
from heremaps.heremapsexceptions import MalformedResult
|
2015-11-03 23:26:54 +08:00
|
|
|
|
|
|
|
class Geocoder:
|
|
|
|
'A Here Maps Geocoder wrapper for python'
|
|
|
|
|
2015-11-05 01:12:54 +08:00
|
|
|
URL_GEOCODE_JSON = 'http://geocoder.api.here.com/6.2/geocode.json'
|
|
|
|
DEFAULT_MAXRESULTS = 1
|
2015-11-05 18:12:10 +08:00
|
|
|
DEFAULT_GEN = 9
|
2015-11-03 23:26:54 +08:00
|
|
|
|
|
|
|
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 = ''
|
2015-11-05 01:12:54 +08:00
|
|
|
maxresults = ''
|
2015-11-03 23:26:54 +08:00
|
|
|
|
2015-11-05 18:12:10 +08:00
|
|
|
def __init__(self, app_id, app_code, maxresults=DEFAULT_MAXRESULTS, gen=DEFAULT_GEN):
|
2015-11-04 01:01:19 +08:00
|
|
|
self.app_id = app_id
|
|
|
|
self.app_code = app_code
|
2015-11-05 01:12:54 +08:00
|
|
|
self.maxresults = maxresults
|
2015-11-05 18:12:10 +08:00
|
|
|
self.gen = gen
|
2015-11-03 23:26:54 +08:00
|
|
|
|
|
|
|
def geocode(self, params):
|
|
|
|
if not set(params.keys()).issubset(set(self.ADDRESS_PARAMS)):
|
|
|
|
raise BadGeocodingParams(params)
|
|
|
|
|
2015-11-05 18:12:10 +08:00
|
|
|
response = self.perform_request(params)
|
2015-11-05 01:12:54 +08:00
|
|
|
|
|
|
|
try:
|
|
|
|
results = response['Response']['View'][0]['Result']
|
|
|
|
except IndexError:
|
|
|
|
raise EmptyGeocoderResponse()
|
|
|
|
|
|
|
|
return results
|
|
|
|
|
2015-11-05 18:12:10 +08:00
|
|
|
def perform_request(self, params):
|
2015-11-03 23:26:54 +08:00
|
|
|
request_params = {
|
|
|
|
'app_id' : self.app_id,
|
|
|
|
'app_code' : self.app_code,
|
2015-11-05 01:12:54 +08:00
|
|
|
'maxresults' : self.maxresults,
|
2015-11-05 18:12:10 +08:00
|
|
|
'gen' : self.gen
|
2015-11-03 23:26:54 +08:00
|
|
|
}
|
|
|
|
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
|
|
|
|
|
2015-11-05 18:12:10 +08:00
|
|
|
def geocode_address(self, **kwargs):
|
2015-11-03 23:26:54 +08:00
|
|
|
params = {}
|
2015-11-04 18:41:06 +08:00
|
|
|
for key, value in kwargs.iteritems():
|
|
|
|
if value: params[key] = value
|
2015-11-03 23:26:54 +08:00
|
|
|
|
|
|
|
if not params: raise NoGeocodingParams()
|
|
|
|
|
|
|
|
return self.geocode(params)
|
|
|
|
|
2015-11-05 18:12:10 +08:00
|
|
|
def extract_lng_lat_from_result(self, result):
|
|
|
|
try:
|
|
|
|
location = result['Location']
|
|
|
|
except KeyError:
|
|
|
|
raise MalformedResult()
|
2015-11-03 23:26:54 +08:00
|
|
|
|
|
|
|
longitude = location['DisplayPosition']['Longitude']
|
|
|
|
latitude = location['DisplayPosition']['Latitude']
|
|
|
|
|
|
|
|
return [longitude, latitude]
|