Introduction

There are several ways to write PostGIS programs, and in this chapter we will see a few of them. You will mainly use the Python language throughout this chapter. Python is a fantastic language with a plethora of GIS and scientific libraries that can be combined with PostGIS to write awesome geospatial applications.

If you are new to Python, you can quickly get productive with these excellent web resources:

You can combine Python with some excellent and popular libraries, such as:

The recipes in this chapter will cover some other useful geospatial Python libraries that are worthy of being looked at if you are developing a geospatial application. Under these Python libraries, the following libraries are included:

In the first recipe, you will write a program that uses Python and its utilities such as psycopg, requests, and simplejson to fetch weather data from the web and import it in PostGIS.

In the second recipe, we will drive you to use Python and the GDAL OGR Python bindings library to create a script for geocoding a list of place names using one of the GeoNames web services.

You will then write a Python function for PostGIS using the PL/Python language to query the http://openweathermap.org/ web services, already used in the first recipe, to calculate the weather for a PostGIS geometry from within a PostgreSQL function.

In the fourth recipe, you will create two PL/pgSQL PostGIS functions that will let you perform geocoding and reverse geocoding using the GeoNames datasets.

After this, there is a recipe in which you will use the OpenStreetMap street datasets imported in PostGIS to implement a very basic Python class in order to provide a geocode implementation to the class's consumer using PostGIS trigram support.

The sixth recipe will show you how to create a PL/Python function using the geopy library to geocode addresses using a web geocoding API such as Google Maps, Yahoo! Maps, Geocoder, GeoNames, and others.

In the last recipe of this chapter, you will create a Python script to import data from the netCDF format to PostGIS using the GDAL Python bindings.

Let's see some notes before starting with the recipes in this chapter.

If you are using Linux or macOS, follow these steps:

  1. Create a Python virtualenv (http://www.virtualenv.org/en/latest/) to keep a Python-isolated environment to be used for all the Python recipes in this book and activate it. Create it in a central directory, as you will need to use it for most of the Python recipes in this book:
      $ cd ~/virtualenvs
      $ virtualenv --no-site-packages postgis-cb-env
      $ source postgis-cb-env/bin/activate
  1. Once activated, you can install the Python libraries you will need for the recipes in this chapter:
      $ pip install simplejson
      $ pip install psycopg2
      $ pip install numpy
      $ pip install requests
      $ pip install gdal
      $ pip install geopy
  1. If you are new to the virtual environment and you are wondering where the libraries have been installed, you should find everything in the virtualenv directory in our development box. You can find the libraries using the following command:
      $ ls /home/capooti/virtualenv/postgis-cb-env/lib/
python2.7/site-packages

If you are wondering what is going on with the previous command lines, then virtualenv is a tool that will be used to create isolated Python environments, and you can find more information about this tool at http://www.virtualenv.org, while pip (http://www.pip-installer.org) is a package management system used to install and manage software packages written in Python.

If you are using Windows, follow these steps:

  1. The easiest way to get Python and all the libraries needed for the recipes in this chapter is to use OSGeo4W, a popular binary distribution of open source geospatial software for Windows. You can download it from http://trac.osgeo.org/osgeo4w/.
  2. In our Windows box the OSGeo4W shell, at the time of writing this book comes with Python 2.7, GDAL 2.2 Python bindings, simplejson, psycopg2, and numpy. You will only need to install geopy.
  3. The easiest way to install geopy and to eventually add more Python libraries to the OSGeo4W shell is to install setuptools and pip by following the instructions found at http://www.pip-installer.org/en/latest/installing.html. Open the OSGeo4W shell and just enter the following commands:
      > python ez_setup.py
      > python get-pip.py
      > pip install requests
      > pip install geopy