An intro to GIS automation with python
In this article I introduce some simple scripts which can be useful for automating basic GIS tools like raster creation , statistical calculator , geometry based clipping, Setting up projection based on reference, resampling raster images e.t.c
In all below exercises I have mentioned raster which mainly refers to GeoTiff images (not any other png,jpeg,jpg e.t.c)
OSGEO-GDAL
GDAL is a software library package specifically for reading and writing geospatial data formats (both raster and vector). GDAL is an open-source C/C++ language based software but also available as python package (python wrapper like). GDAL is one of the core package available in QGIS software
shapefile Installation of GDAL
Below is installation of gdal in ubuntu with aptitiude package manager it must be similar and supported in other Unix distros Here I am only dealing with a sadding the PPA to your sources should automatically grab any necessary dependencies, including at least the relevant libgdal version
sudo add-apt-repository ppa:ubuntugis/ppa
updating the source packages
sudo apt-get update
Now you should be able to install the GDAL/OGR package.
sudo apt-get install gdal-bin
sudo apt-get install libgdal-dev
gdal_numpy_calcTo verify the installation, run
ogrinfo --version
exporting variables to the compiler
export CPLUS_INCLUDE_PATH=/usr/include/gdal
export C_INCLUDE_PATH=/usr/include/gdal
Now we can install the gdal using pip
pip install GDAL==<GDAL VERSION FROM OGRINFO>
1. GeoTiff creation from NumPy array
Simply an GeoTiff image is an array with spatial georeferencing and projection based on the location (and of course we can add additional attrributes). So if we have a spatial array we can create an raster (Geotiff) image using GDAL and Python
For this I m creating an array with random values and adding geotransformation to it
Using the above example you can convert any dataset (xarray,netCDF4) to an tif image
2. Changing projection and referencing based on the reference raster
Similar to above exercise we can reference and project an array based on another raster
3. Raster calculator
Raster calculator is one of the important tool in order to do spatial analysis over multiple raster datasets
There are two ways to do the raster calculation one using directly the gdal and with extracting the values as numpy array and let the numpy do the rest
Using gdal_calc.py
gdal_calc.py is program for raster calculation using gdal. Directly there is no function to do raster calculation inside gdal library but the gdal_calc.py is officially available along with the gdal software
So we can call the gdal_calc.py and can be used for raster calculation
Using numpy
Even using the gdal_calc.py is simple the more better way is using our friendly neighborhood NumPy. we can use the gdal_array for that
I used one of the tiff image as an reference. Remember ultimately this is an 2D numpy array calculation which means array must be on same size (I have used an clipped rasters of same dimensions)
4. Resampling the GeoTiff
Resampling an GeoTiff with GDAL python is relatively simple using the gdal.Translate function.
Using the rasterio and shapely packages
In case of uses like spatial plots (meteorological data , environmental data e.t.c) we can accomplish most of the task like using shape file with simple module shapely and for raster using rasterio in python
In meteorological data visualisation, using rasterio, shapely, cartopy, geopandas packages are more preferred since it is simple and area based calculation is not involved (for example: from projected cartopy or geopandas dataset it is almost impossible to compute area while it is possible with gdal projected one)
All of the above exercises can be done using rasterio and shapely but the GDAL is more advances and consist of a large number of tools which ultimately gives us all the tools available in ArcGIS or QGIS
Conclusion
In my first experience using ArcPy is very simple for automating the above tools but as everyone know the ArcPy needs server license to run while the GDAL is an opensource software and it is becoming easier to use as python packages are available
I have made the above scripts for automationg some of GIS tools for large set of IMD rainfall data and runoff datasets (tiff images)
The aim of this article is to give a jump start for developing GIS based tools using python. This above tools can be scripted itself in the QGIS but a standalone framework only need GDAL and Python which will handy for a server based automation.