How it works...

You wrote a PL/Python function to geocode an address. For this purpose, you used the geopy Python library, which lets you query several geocoding APIs in the same manner.

Using geopy, you need to instantiate a geocoder object with a given API and query it to get the results, such as a place name and a couple of coordinates. You can use the plpy module utilities to run a query on the database using the PostGIS ST_GeomFromText function, and log informative messages and warnings for the user.

If the geocoding process fails, you return a NULL geometry to the user with a warning message, using a try..except Python block.

Get PostGIS Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.