Book Image

Mastering Geospatial Analysis with Python

By : Silas Toms, Paul Crickard, Eric van Rees
Book Image

Mastering Geospatial Analysis with Python

By: Silas Toms, Paul Crickard, Eric van Rees

Overview of this book

Python comes with a host of open source libraries and tools that help you work on professional geoprocessing tasks without investing in expensive tools. This book will introduce Python developers, both new and experienced, to a variety of new code libraries that have been developed to perform geospatial analysis, statistical analysis, and data management. This book will use examples and code snippets that will help explain how Python 3 differs from Python 2, and how these new code libraries can be used to solve age-old problems in geospatial analysis. You will begin by understanding what geoprocessing is and explore the tools and libraries that Python 3 offers. You will then learn to use Python code libraries to read and write geospatial data. You will then learn to perform geospatial queries within databases and learn PyQGIS to automate analysis within the QGIS mapping suite. Moving forward, you will explore the newly released ArcGIS API for Python and ArcGIS Online to perform geospatial analysis and create ArcGIS Online web maps. Further, you will deep dive into Python Geospatial web frameworks and learn to create a geospatial REST API.
Table of Contents (23 chapters)
Title Page
Copyright and Credits
Packt Upsell
Geoprocessing with Geodatabases


Over time, Python has become the programming language of choice for spatial analysis, resulting in many packages that read, convert, analyze, and visualize spatial data. With so many packages available, it made sense to create a reference book for students and experienced professionals containing essential geospatial Python libraries for Python 3.

This book also comes at an exciting moment: new technology is transforming how people work with geospatial data – IoT, machine learning, and data science are areas where geospatial data is used constantly. This explains the inclusion of new Python libraries, such as CARTOframes and MapboxGL, and Jupyter is included as well, to explore these new trends. At the same time, web and cloud-based GIS is increasingly becoming the new standard. This is reflected in the chapters of the second part of this book, where interactive geospatial web maps and REST APIs are introduced.

These newer libraries are combined with a number of older ones that have become essential over the years, and are still very popular to this day, such as Shapely, Rasterio, and GeoPandas. Readers who are new to this field will be given a proper introduction to popular libraries, putting them into perspective and comparing their syntax through code examples using real-world data.

Finally, this books marks the transition from Python 2 to 3.x. All of the libraries covered in this book were written in Python 3.x so that the readers can access all of them using Jupyter Notebook, which is also the recommended Python coding environment for this book.

Who this book is for

This book is for anyone who works with location information and Python. Students, developers, and geospatial professionals can all use this reference book, as it covers GIS data management, analysis techniques, and code libraries built with Python 3.

What this book covers

Chapter 1, Package Installation and Management, explains how to install and manage the code libraries used in the book.

Chapter 2, Introduction to Geospatial Code Libraries, covers the major code libraries used to process and analyze geospatial data.

Chapter 3, Introduction to Geospatial Databases, introduces the geospatial databases used for data storage and analysis.

Chapter 4, Data Types, Storage, and Conversion, focuses on the many different data types (both vector and raster) that exist within GIS.

Chapter 5, Vector Data Analysis, covers Python libraries such as Shapely, OGR, and GeoPandas. which are used for analyzing and processing vector data.

Chapter 6, Raster Data Processing, explores using GDAL and Rasterio to process raster datasets in order to perform geospatial analysis.

Chapter 7, Geoprocessing with Geodatabases, shows the readers how to use Spatial SQL to perform geoprocessing with database tables containing a spatial column.

Chapter 8, Automating QGIS Analysis, teaches the readers how to use PyQGIS to automate analysis within the QGIS mapping suite.

Chapter 9, ArcGIS API for Python and ArcGIS Online, introduces the ArcGIS API for Python, which enables users to interact with Esri's cloud platform, ArcGIS Online, using Python 3.

Chapter 10, Geoprocessing with a GPU Database, covers using Python tools to interact with cloud-based data to search and process data.

Chapter 11, Flask and GeoAlchemy2, describes how to use the Flask Python web framework and the GeoAlchemy ORM to perform spatial data queries.

Chapter 12, GeoDjango, covers using the Django Python web framework and the GeoDjango ORM to perform spatial data queries.

Chapter 13, Geospatial REST API, teaches the readers how to create a REST API for geospatial data.

Chapter 14, Cloud Geodatabase Analysis and Visualization, introduces the readers to the CARTOframes Python package, enabling the integration of Carto maps, analysis, and data services into data science workflows.

Chapter 15, Automating Cloud Cartography, covers a new location data visualization library for Jupyter Notebooks.

Chapter 16, Python Geoprocessing with Hadoop, explains how to perform geospatial analysis using distributed servers.

To get the most out of this book

As this book covers Python, it is assumed that the reader has a basic understanding of the Python language, can install Python libraries, and knows how to write and run Python scripts. As for additional knowledge, the first six chapters can easily be understood without any previous knowledge of geospatial data analysis. However, later chapters assume some knowledge of spatial databases, big data platforms, data science, web APIs, and Python web frameworks.

Download the example code files

You can download the example code files for this book from your account at If you purchased this book elsewhere, you can visit and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register at
  2. Select the SUPPORT tab.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR/7-Zip for Windows
  • Zipeg/iZip/UnRarX for Mac
  • 7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here:

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "Select a folder and save the key, which will now have a .ppk file extension."

A block of code is set as follows:

cursor.execute("SELECT * from art_pieces")

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

from pymapd import connect
connection = connect(user="mapd", password= "{password}", 
     host="{}", dbname="mapd")
cursor = connection.cursor()
sql_statement = """SELECT name FROM county;"""

Any command-line input or output is written as follows:

conda install -c conda-forge geos

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "To generate the pair from the EC2 Dashboard, select Key Pairs from the NETWORK & SECURITY group in the left panel after scrolling down."


Warnings or important notes appear like this.


Tips and tricks appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit


Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit