Skip to content
Study / Practical

Python for GIS: scripting, GDAL and spatial statistics

Twenty parts, written for beginners. Parts 1 to 16 build their own small sample data, so every example runs on any computer with no downloads. Parts 17 to 20 are intermediate and use real Bengaluru data: PyQGIS, spatial statistics, a Landsat 8 scene, and a final project that brings pandas, GeoPandas and GDAL together.

22 partsPython 3pandasGeoPandasGDALrasterioSQLFree software

Python basics

6 parts
  1. 01
    Python basics: variables, numbers and textInstall Python, run your first lines, and learn variables, data types and string cleaning by describing a city the way a GIS attribute table does.PracticalBeginner
  2. 02
    Python collections: lists, tuples, sets, dictionariesStore many cities, coordinates and road classes at once. Learn when to use a list, tuple, set or dictionary, with GIS records as the examples.PracticalBeginner
  3. 03
    Python decisions and loopsClassify cities by population, validate coordinates and process many records with if, for, while and list comprehensions.PracticalBeginner
  4. 04
    Python functions and errorsWrite reusable functions for distance and classification, handle bad values with try and except, and test your code with assert.PracticalBeginner
  5. 05
    Python files, CSV, JSON, dates and modulesRead and write text, CSV and JSON files, build file paths with pathlib, work with dates, and put reusable code in your own module.PracticalBeginner
  6. 06
    Python mini project: city data cleanerCombine everything from Python basics into one script that cleans a messy list of city records, validates coordinates, logs rejected rows and writes CSV and JSON output ready for pandas and GeoPandas.PracticalBeginner

Pandas

3 parts
  1. 07
    Pandas first steps: read, filter, sortLoad a cities table into a pandas DataFrame, inspect it, find missing values, filter with conditions and sort. Every step is explained for a beginner.PracticalBeginner
  2. 08
    Pandas groupby, merge, reshape, classes and datesSummarise a table by group, join two tables on a key, reshape wide and long data, classify values into bins, and convert text to dates and numbers.PracticalBeginner
  3. 09
    Pandas text cleaning with .str methodsClean place names, road codes and addresses with pandas .str methods: strip, replace, split, contains, match, extract and findall, with a gentle introduction to regular expressions.PracticalBeginner

GeoPandas

2 parts
  1. 10
    GeoPandas: points, CRS, measuring and mapsTurn a table of latitude and longitude into a GeoDataFrame, set and change the CRS, measure distance and area correctly, draw a map and save the layer.PracticalBeginner
  2. 11
    GeoPandas: spatial join, buffer, overlay, dissolveCount points in polygons, find what lies near a road, measure how much of each district is flooded and merge districts into zones, using a small invented dataset you can run anywhere.PracticalBeginner

GDAL

2 parts
  1. 12
    GDAL rasters: inspect, reproject, clip, mosaic, calculateUse the GDAL Python bindings to read raster metadata, handle NoData, convert, reproject, clip, mosaic and run a raster calculation on a small terrain raster built in the first step.PracticalBeginner
  2. 13
    GDAL/OGR vectors: convert, filter, rasterize, polygonizeUse the OGR side of GDAL to inspect vector files, convert between formats, reproject, filter by attribute, turn polygons into a raster and a raster back into polygons. Each step comes with its command line twin.PracticalBeginner

SQL

2 parts
  1. 14
    SQL basics for GIS data (PostgreSQL)Learn SQL on a small cities table in PostgreSQL, using pgAdmin's Query Tool: create tables, select, filter, sort, classify, handle missing values, join and summarise. Copy each query and run it.PracticalBeginner2 screenshots
  2. 15
    Spatial SQL with PostGISAdd geometry to SQL with PostGIS. Store points and polygons, measure distances correctly, find what is within a distance, join by location, buffer, intersect and dissolve, and read the results into GeoPandas.PracticalIntermediate2 screenshots

Rasterio

1 part
  1. 16
    Rasterio: read, clip, reproject, calculateUse rasterio to open a raster, read its metadata and cells, handle NoData, clip it with a polygon, reproject it, calculate NDVI from two bands, take zonal statistics per district, and write new GeoTIFF files.PracticalBeginner

Python and GDAL

2 parts
  1. 17
    Python for GIS: PyQGIS, GeoPandas and GDALAutomate QGIS from the Python console, run Processing tools from a script, and use GeoPandas and GDAL for vector and raster work outside the QGIS interface.PracticalIntermediate
  2. 18
    Spatial statistics and simulationTest whether a pattern is clustered, measure how similar nearby values are, fit a variogram, and run a small urban growth simulation with a few lines of Python.PracticalAdvanced3 screenshots

Rasterio

1 part
  1. 19
    Rasterio on real data: Landsat NDVI and zonal statisticsOpen real Landsat 8 bands in rasterio, convert them to reflectance, mask clouds with the quality band, calculate NDVI, take zonal statistics for Bengaluru's five city corporations, and compare them with the share of parks and lakes.PracticalIntermediate

Final project

1 part
  1. 20
    Final project: a corporation scorecardBring pandas, GeoPandas and GDAL together in one real project: count hospitals, measure parks, lakes and roads, take zonal NDVI with GDAL, join everything into one scorecard, check it, and save it as a CSV, a GeoPackage, a GeoJSON and a map.PracticalIntermediate

Image classification

1 part
  1. 21
    LULC with Python: K-means and Random ForestTurn a six-band Sentinel-2 image of Bengaluru into a land cover map with Python. Group pixels with K-means, train a Random Forest on labelled samples, check the result against independent reference points, and measure the area of each class.PracticalIntermediate2 screenshots

Time series

1 part
  1. 22
    Raster time series with PythonStack twelve monthly NDVI rasters into one array, read the seasonal curve of a field and a town, handle a month with missing cells, and map when each place is greenest. A gentle first project with satellite time series, using NumPy, pandas and rasterio.PracticalBeginner