Preface

Geocomputation with Python (geocompy) is motivated by the need for an introductory resource for working with geographic data with the most popular programming language in the world. A unique selling point of the book is its cohesive and joined-up coverage of both vector and raster geographic data models and consistent learning curve. We aim to minimize surprises, with each section and chapter building on the previous. If you’re just starting out with Python for working with geographic data, this book is an excellent place to start.

There are many resources on Python on β€˜GeoPython’ but none that fill this need for an introductory resource that provides strong foundations for future work. We want to avoid reinventing the wheel and provide something that fills an β€˜ecological niche’ in the wider free and open-source software for geospatial (FOSS4G) ecosystem. Key features include:

  1. Doing basic operations well
  2. Integration of vector and raster datasets operations
  3. Clear explanation of each line of code in the book to minimize surprises
  4. Provision of lucid example datasets and meaningful operations to illustrate the applied nature of geographic research

This book complements and adds value to other projects in the ecosystem, as highlighted in the following comparison between Geocomputation with Python and related GeoPython books:

Another unique feature of the book is that it is part of a wider community. Geocomputation with Python is a sister project of Geocomputation with R6(Lovelace, Nowosad, and Muenchow 2019), a book on geographic data analysis, visualization, and modeling using the R programming language that has 60+ contributors and an active community, not least in the associated Discord group7. Links with the vibrant β€˜R-spatial’ community, and other communities such as GeoRust and JuliaGeo, lead to many opportunities for mutual benefit across open-source ecosystems.

Prerequisites

We assume that the reader is:

  • familiar with the Python language,
  • is capable of running Python code and install Python packages, and
  • is familiar with the numpy and pandas packages for working with data in Python.

From that starting point on, the book introduces the topic of working with spatial data in Python, through dedicated third-party packagesβ€”most importantly geopandas and rasterio.

We also assume familiarity with theoretical concepts of geographic data and GIS, such as coordinate systems, projections, spatial layer file formats, etc., which is necessary for understanding the reasoning of the examples.

Code and sample data

To run the code examples, you can download8 the ZIP file of the GitHub repository. In the ZIP file, the ipynb directory contains the source files of the chapters in Jupyter Notebook format, the data directory contains the sample data files, and the output directory contains the files created in code examples (some of which are also used as inputs in other code sections). Place them together as follows to run the code:

β”œβ”€β”€ data
β”‚   β”œβ”€β”€ aut.tif
β”‚   β”œβ”€β”€ ch.tif
β”‚   β”œβ”€β”€ coffee_data.csv
β”‚   β”œβ”€β”€ cycle_hire.gpkg
β”‚   β”œβ”€β”€ cycle_hire_osm.gpkg
β”‚   β”œβ”€β”€ cycle_hire_xy.csv
β”‚   β”œβ”€β”€ dem.tif
β”‚   β”œβ”€β”€ landsat.tif
β”‚   β”œβ”€β”€ nlcd.tif
β”‚   β”œβ”€β”€ nz_elev.tif
β”‚   β”œβ”€β”€ nz.gpkg
β”‚   β”œβ”€β”€ nz_height.gpkg
β”‚   β”œβ”€β”€ seine.gpkg
β”‚   β”œβ”€β”€ srtm.tif
β”‚   β”œβ”€β”€ us_states.gpkg
β”‚   β”œβ”€β”€ world.gpkg
β”‚   β”œβ”€β”€ world_wkt.csv
β”‚   β”œβ”€β”€ zion.gpkg
β”‚   └── zion_points.gpkg
β”œβ”€β”€ output
β”‚   β”œβ”€β”€ cycle_hire_xy.csv
β”‚   β”œβ”€β”€ dem_agg5.tif
β”‚   β”œβ”€β”€ dem_contour.gpkg
β”‚   β”œβ”€β”€ dem_resample_maximum.tif
β”‚   β”œβ”€β”€ dem_resample_nearest.tif
β”‚   β”œβ”€β”€ elev.tif
β”‚   β”œβ”€β”€ grain.tif
β”‚   β”œβ”€β”€ map.html
β”‚   β”œβ”€β”€ ne_10m_airports.cpg
β”‚   β”œβ”€β”€ ne_10m_airports.dbf
β”‚   β”œβ”€β”€ ne_10m_airports.prj
β”‚   β”œβ”€β”€ ne_10m_airports.README.html
β”‚   β”œβ”€β”€ ne_10m_airports.shp
β”‚   β”œβ”€β”€ ne_10m_airports.shx
β”‚   β”œβ”€β”€ ne_10m_airports.VERSION.txt
β”‚   β”œβ”€β”€ ne_10m_airports.zip
β”‚   β”œβ”€β”€ nlcd_4326_2.tif
β”‚   β”œβ”€β”€ nlcd_4326.tif
β”‚   β”œβ”€β”€ nlcd_modified_crs.tif
β”‚   β”œβ”€β”€ plot_geopandas.jpg
β”‚   β”œβ”€β”€ plot_rasterio2.svg
β”‚   β”œβ”€β”€ plot_rasterio.jpg
β”‚   β”œβ”€β”€ r3.tif
β”‚   β”œβ”€β”€ r_nodata_float.tif
β”‚   β”œβ”€β”€ r_nodata_int.tif
β”‚   β”œβ”€β”€ r.tif
β”‚   β”œβ”€β”€ srtm_32612_aspect.tif
β”‚   β”œβ”€β”€ srtm_32612_slope.tif
β”‚   β”œβ”€β”€ srtm_32612.tif
β”‚   β”œβ”€β”€ srtm_masked_cropped.tif
β”‚   β”œβ”€β”€ srtm_masked.tif
β”‚   β”œβ”€β”€ w_many_features.gpkg
β”‚   β”œβ”€β”€ w_many_layers.gpkg
β”‚   └── world.gpkg
β”œβ”€β”€ 01-spatial-data.ipynb
β”œβ”€β”€ 02-attribute-operations.ipynb
β”œβ”€β”€ 03-spatial-operations.ipynb
β”œβ”€β”€ 04-geometry-operations.ipynb
β”œβ”€β”€ 05-raster-vector.ipynb
β”œβ”€β”€ 06-reproj.ipynb
β”œβ”€β”€ 07-read-write.ipynb
└── 08-mapping.ipynb

Software

Python version used when rendering the book:

3.11.4 (main, Aug 16 2023, 05:31:52) [GCC 10.2.1 20210110]

Versions of the main packages used in the book:

numpy==2.0.1
pandas==2.2.2
shapely==2.0.5
geopandas==1.0.1
rasterio==1.3.10
matplotlib==3.9.0
rasterstats==0.19.0

Acknowledgements

We acknowledge Robin Lovelace, Jakub Nowosad, and Jannes Muenchowβ€”authors of Geocomputation with R (Robin and Jakub also author the present book), a book on the same topic for a different programming language (R). The structure, topics, and most of the theoretical discussions were adapted from that earlier publication.

We thank the authors of the Python language, and the authors of the numpy, pandas, shapely, geopandas, and rasterio packages which are used extensively in the book, for building these wonderful tools.

We acknowledge GitHub users Will Deakin, Sean Gillies, Josh Cole, and Jt Miclat (at the time of writing; full list on GitHub9) for their contributions during the open-source development of the book.


  1. https://www.packtpub.com/product/learning-geospatial-analysis-with-python/9781783281138β†©οΈŽ

  2. https://www.manning.com/books/geoprocessing-with-pythonβ†©οΈŽ

  3. https://pythongis.orgβ†©οΈŽ

  4. https://geographicdata.science/book/intro.htmlβ†©οΈŽ

  5. https://www.oreilly.com/library/view/python-for-geospatial/9781098104788/β†©οΈŽ

  6. https://r.geocompx.org/β†©οΈŽ

  7. https://discord.gg/PMztXYgNxpβ†©οΈŽ

  8. https://github.com/geocompx/geocompy/zipball/masterβ†©οΈŽ

  9. https://github.com/geocompx/geocompy/graphs/contributorsβ†©οΈŽ