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:
- The official Python tutorial at http://docs.python.org/2/tutorial/
- The popular Dive into Python book at http://www.diveintopython.net/
You can combine Python with some excellent and popular libraries, such as:
- Psycopg: This is the most complete and popular Python DB API implementation for PostgreSQL; see http://initd.org/psycopg/
- GDAL: Used to unchain the powerful GDAL library in your Python scripts; see http://www.gdal.org/gdal_tutorial.html
- requests: This is a handy Python standard library to manage HTTP stuff, such as opening URLs
- simplejson: This is a simple and fast JSON encoder/decoder
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:
- Shapely: This is a Python interface to the GEOS library for the manipulation and analysis of planar geometric objects: http://toblerity.github.io/shapely/
- Fiona: This is a very light OGR Python API, which can be used as an alternative to the OGR bindings used in this chapter to manage vector datasets: https://github.com/Toblerity/Fiona
- Rasterio: This a Pythonic GDAL Python API, which can be used as an alternative to the GDAL bindings used in this chapter in order to manage raster datasets: https://github.com/mapbox/rasterio
- pyproj: This is the Python interface to the PROJ.4 library: https://pypi.python.org/pypi/pyproj
- Rtree: This is a ctype Python wrapper to the libspatialindex library, providing several spatial indexing features that can be extremely useful for some kinds of geospatial development: http://toblerity.github.io/rtree/
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:
- 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
- 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
- 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:
- 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/.
- 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.
- 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