Database Reference
In-Depth Information
CREATE OR REPLACE FUNCTION
chp08.Geocode(address text, api text
DEFAULT 'google')
RETURNS geometry(Point,4326)
AS $$
from geopy import geocoders
plpy.info('Geocoing the given address
using the %s api' % (api))
if api.lower() == 'geonames':
g = geocoders.GeoNames()
elif api.lower() == 'geocoderdotus':
g = geocoders.GeocoderDotUS()
else: # if the user give a wrong api
name we use google
g = geocoders.GoogleV3()
try:
place, (lat, lng) =
g.geocode(address)
plpy.info('Geocoded %s for the
address: %s' % (place, address))
plpy.info('Longitude is %s,
Latitude is %s.' % (lng, lat))
result = plpy.execute("SELECT
ST_GeomFromText('POINT(%s %s)', 4326) AS
point_geocoded" % (lng, lat))
geometry =
result[0]["point_geocoded"]
return geometry
except:
plpy.warning('There was an error
in the geocoding process, setting
geometry to Null.')
return None
$$ LANGUAGE plpythonu;
4. Testthenewversionofyourfunctionwithoutspecifyingtheparameterforthe
API. In such a case, it should default to the Google API:
Search WWH ::




Custom Search