Database Reference
In-Depth Information
$ psql -U me postgis_cookbookpsql (9.1.6,
server 9.1.8)Type "help" for help.
postgis_cookbook=# CREATE EXTENSION
plpythonu;
Note
Alternatively,youcouldaddPL/Pythonsupporttoyourdatabaseusingthe
cre-
atelang
shell command (this is the only way if you are using a PostgreSQL
Version 9.0 and lower):
$ createlang plpythonu postgis_cookbook
How to do it...
Carry out the following steps:
1. As the first test, open your favorite SQL client (
psql
or
pgAdmin
), and
writeaverybasicPL/PythonfunctionjustusingtheGoogleV3geocodingAPI
with
geopy
. The function will accept the address string as an input para-
meter and, after importing
geopy
, it will instantiate a
geopy
Google Geo-
coder, run the geocode process, and then return the point geometry using
the
ST_GeomFromText
function and the
geopy
output:
CREATE OR REPLACE FUNCTION
chp08.Geocode(address text)
RETURNS geometry(Point,4326)
AS $$
from geopy import geocoders
g = geocoders.GoogleV3()
place, (lat, lng) =
g.geocode(address)
plpy.info('Geocoded %s for the
address: %s' % (place, address))
plpy.info('Longitude is %s,