Google geocoder working
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
#!/usr/local/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
|
||||
class MalformedResult(Exception):
|
||||
def __str__(self):
|
||||
return repr('Malformed result. The API might have changed.')
|
||||
@@ -0,0 +1,49 @@
|
||||
#!/usr/local/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import googlemaps
|
||||
|
||||
from exceptions import MalformedResult
|
||||
|
||||
|
||||
class Geocoder:
|
||||
"""A Google Maps Geocoder wrapper for python"""
|
||||
|
||||
def __init__(self, client_id, client_secret):
|
||||
self.client_id = self._clean_client_id(client_id)
|
||||
self.client_secret = client_secret
|
||||
self.geocoder = googlemaps.Client(
|
||||
client_id=self.client_id, client_secret=self.client_secret)
|
||||
|
||||
def geocode_address(self, searchtext, city=None, state=None,
|
||||
country=None):
|
||||
opt_params = self._build_optional_parameters(city, state, country)
|
||||
results = self.geocoder.geocode(address=searchtext, components=opt_params)
|
||||
if results:
|
||||
return self._extract_lng_lat_from_result(results[0])
|
||||
else:
|
||||
return []
|
||||
|
||||
def _extract_lng_lat_from_result(self, result):
|
||||
try:
|
||||
location = result['geometry']['location']
|
||||
longitude = location['lng']
|
||||
latitude = location['lat']
|
||||
return [longitude, latitude]
|
||||
except KeyError:
|
||||
raise MalformedResult()
|
||||
|
||||
def _build_optional_parameters(self, city=None, state=None,
|
||||
country=None):
|
||||
optional_params = {}
|
||||
if city:
|
||||
optional_params['locality'] = city
|
||||
if state:
|
||||
optional_params['administrative_area'] = state
|
||||
if country:
|
||||
optional_params['country'] = country
|
||||
return optional_params
|
||||
|
||||
def _clean_client_id(self, client_id):
|
||||
# Consistency with how the client_id is saved in metadata
|
||||
return client_id.replace('client=', '')
|
||||
41
server/lib/python/google_geocoder/setup.py
Normal file
41
server/lib/python/google_geocoder/setup.py
Normal file
@@ -0,0 +1,41 @@
|
||||
"""
|
||||
A Google Maps wrapper for the CartoDB geocoder server PostgreSQL extension
|
||||
|
||||
See:
|
||||
https://github.com/CartoDB/geocoder-api
|
||||
"""
|
||||
|
||||
from setuptools import setup, find_packages
|
||||
|
||||
setup(
|
||||
name='google-geocoder',
|
||||
|
||||
version='0.0.1',
|
||||
|
||||
description='A Google Maps wrapper for the CartoDB geocoder server PostgreSQL extension',
|
||||
|
||||
url='https://github.com/CartoDB/geocoder-api',
|
||||
|
||||
author='Data Services Team - CartoDB',
|
||||
author_email='dataservices@cartodb.com',
|
||||
|
||||
license='MIT',
|
||||
|
||||
classifiers=[
|
||||
'Development Status :: 2 - Beta',
|
||||
'Intended Audience :: Mapping comunity',
|
||||
'Topic :: Maps :: Mapping Tools',
|
||||
'License :: OSI Approved :: MIT License',
|
||||
'Programming Language :: Python :: 2.7',
|
||||
],
|
||||
|
||||
keywords='maps api mapping tools',
|
||||
|
||||
packages=find_packages(exclude=['contrib', 'docs', 'tests']),
|
||||
|
||||
extras_require={
|
||||
'dev': ['unittest'],
|
||||
'test': ['unittest'],
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user