Google geocoder working
This commit is contained in:
@@ -16,11 +16,6 @@ class NoGeocodingParams(Exception):
|
||||
return repr('No params for geocoding specified')
|
||||
|
||||
|
||||
class EmptyGeocoderResponse(Exception):
|
||||
def __str__(self):
|
||||
return repr('The request could not be geocoded')
|
||||
|
||||
|
||||
class MalformedResult(Exception):
|
||||
def __str__(self):
|
||||
return repr('Result structure is malformed')
|
||||
@@ -4,10 +4,10 @@
|
||||
import json
|
||||
import requests
|
||||
|
||||
from heremaps.heremapsexceptions import BadGeocodingParams
|
||||
from heremaps.heremapsexceptions import EmptyGeocoderResponse
|
||||
from heremaps.heremapsexceptions import NoGeocodingParams
|
||||
from heremaps.heremapsexceptions import MalformedResult
|
||||
from heremaps.exception import BadGeocodingParams
|
||||
from heremaps.exception import EmptyGeocoderResponse
|
||||
from heremaps.exception import NoGeocodingParams
|
||||
from heremaps.exception import MalformedResult
|
||||
|
||||
|
||||
class Geocoder:
|
||||
@@ -50,10 +50,6 @@ class Geocoder:
|
||||
'strictlanguagemode'
|
||||
] + ADDRESS_PARAMS
|
||||
|
||||
app_id = ''
|
||||
app_code = ''
|
||||
maxresults = ''
|
||||
|
||||
def __init__(self, app_id, app_code, maxresults=DEFAULT_MAXRESULTS,
|
||||
gen=DEFAULT_GEN, host=PRODUCTION_GEOCODE_JSON_URL):
|
||||
self.app_id = app_id
|
||||
@@ -62,18 +58,26 @@ class Geocoder:
|
||||
self.gen = gen
|
||||
self.host = host
|
||||
|
||||
def geocode(self, params):
|
||||
def geocode(self, **kwargs):
|
||||
params = {}
|
||||
for key, value in kwargs.iteritems():
|
||||
if value:
|
||||
params[key] = value
|
||||
if not params:
|
||||
raise NoGeocodingParams()
|
||||
return self.geocode(params)
|
||||
|
||||
def _execute_geocode(self, params):
|
||||
if not set(params.keys()).issubset(set(self.ADDRESS_PARAMS)):
|
||||
raise BadGeocodingParams(params)
|
||||
response = self.perform_request(params)
|
||||
try:
|
||||
response = self._perform_request(params)
|
||||
results = response['Response']['View'][0]['Result']
|
||||
return self._extract_lng_lat_from_result(results)
|
||||
except IndexError:
|
||||
raise EmptyGeocoderResponse()
|
||||
return []
|
||||
|
||||
return results
|
||||
|
||||
def perform_request(self, params):
|
||||
def _perform_request(self, params):
|
||||
request_params = {
|
||||
'app_id': self.app_id,
|
||||
'app_code': self.app_code,
|
||||
@@ -87,16 +91,7 @@ class Geocoder:
|
||||
else:
|
||||
response.raise_for_status()
|
||||
|
||||
def geocode_address(self, **kwargs):
|
||||
params = {}
|
||||
for key, value in kwargs.iteritems():
|
||||
if value:
|
||||
params[key] = value
|
||||
if not params:
|
||||
raise NoGeocodingParams()
|
||||
return self.geocode(params)
|
||||
|
||||
def extract_lng_lat_from_result(self, result):
|
||||
def _extract_lng_lat_from_result(self, result):
|
||||
try:
|
||||
location = result['Location']
|
||||
except KeyError:
|
||||
|
||||
Reference in New Issue
Block a user