9. Get Started#

Open In Colab

9.1. Introduction#

Now that we’ve covered the basics of Python programming, we will begin exploring geospatial data analysis and visualization using Python. This chapter introduces you to key geospatial libraries that form the foundation for working with spatial data in Python.

Geospatial data analysis is essential for various applications, including environmental monitoring, urban planning, and mapping. Python’s ecosystem offers a robust set of libraries for handling both vector and raster data, performing spatial analysis, and creating interactive maps. Some of the core libraries we will work with include:

  • GeoPandas: For handling vector data such as shapefiles, GeoJSON, and performing spatial operations.

  • Rasterio: To read, analyze, and write raster data (e.g., satellite imagery).

  • Xarray: For multidimensional array-based data, often used with climate and meteorological datasets.

  • Leafmap: Simplifies creating interactive maps with minimal code.

  • MapLibre: A tool for building interactive, customizable map visualizations using vector tiles.

  • WhiteboxTools: A suite of GIS tools for spatial analysis.

  • Geemap: Combines the power of Google Earth Engine with Python for large-scale geospatial analysis.

  • Segment-geospatial: An advanced tool for image segmentation in geospatial analysis.

  • HyperCoast: Used for coastal data modeling and analysis.

  • DuckDB: A fast, embeddable analytical database with powerful spatial query capabilities.

  • GDAL: One of the most widely used libraries for raster and vector data processing.

These libraries will be introduced progressively, and we’ll explore their capabilities through hands-on exercises.

9.2. Setting Up Your Python Environment#

To follow along with the examples and exercises in this book, you need to set up a Python environment with the required geospatial libraries. There are two main ways to set up your Python environment: using uv or conda.

9.2.1. uv#

uv is an extremely fast Python package and project manager, written in Rust. It is designed to be a drop-in replacement for pip.

9.2.1.1. Install uv#

You can install uv as follows:

# macOS and Linux:
curl -LsSf https://astral.sh/uv/install.sh | sh

# Windows:
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"

# pip
pip install uv

9.2.1.2. Install geospatial#

To install the geospatial package, which includes many of the libraries we’ll use in this book, run the following commands:

uv venv

# macOS and Linux:
source .venv/bin/activate

# Windows:
.venv\Scripts\activate

uv pip install geospatial
uv pip install --find-links https://girder.github.io/large_image_wheels gdal pyproj

9.2.2. pixi#

Pixi is a package management tool for installing libraries and applications in a reproducible way. It can install packages from the conda-forge channel, PyPI, and other sources. If you have trouble installing geospatial libraries like gdal using uv, you can try using pixi.

9.2.2.1. Install pixi#

You can install pixi as follows:

# macOS and Linux:
curl -fsSL https://pixi.sh/install.sh | bash

# Windows:
iwr -useb https://pixi.sh/install.ps1 | iex

Close and reopen your terminal or command prompt to make sure the pixi command is available.

9.2.2.2. Install gdal#

To install the gdal and jupyterlab packages,run the following commands:

pixi init
pixi add gdal jupyterlab

After installing the packages, you can open Jupyter Lab to start working with geospatial data:

pixi run jupyter lab

9.2.3. conda#

9.2.3.1. Install Miniconda#

Miniconda is a lightweight version of Anaconda and provides all the core functionality needed to manage environments. You can download and install it from the official Miniconda page: Miniconda Installation Guide.

9.2.3.2. Create a New Conda Environment#

Once Miniconda is installed, you can create a new environment specifically for geospatial programming. This isolates your geospatial tools from other Python projects, helping avoid version conflicts.

Run the following commands in your terminal or command prompt:

conda create -n geo python=3.11
conda activate geo

9.2.3.3. Install Geospatial Libraries#

To manage the installation of multiple geospatial libraries more efficiently, we’ll use mamba, a faster alternative to conda. Install it first, then proceed with the geospatial package, which includes many of the libraries we’ll use in this book:

conda install -c conda-forge mamba
mamba install -c conda-forge geospatail

9.3. Verifying Your Installation#

Once you’ve set up your environment, it’s important to verify that everything is working correctly. Let’s run a simple test using leafmap to ensure the installation is successful.

  1. Import the leafmap library:

import leafmap.foliumap as leafmap
  1. Create an interactive map using leafmap.Map() and display it:

m = leafmap.Map()
m

If everything is set up correctly, you should see an interactive map displayed in your notebook or Jupyter Lab environment. This confirms that your Python environment is ready for geospatial data analysis and visualization.

9.4. Summary#

By setting up the Python environment and testing it with a simple map, you have laid the groundwork for more advanced geospatial analysis. In the upcoming chapters, we will dive into specific libraries, their functions, and how they can be applied to real-world geospatial data projects. From basic vector and raster manipulations to creating dynamic visualizations, you’ll progressively gain skills to tackle complex geospatial tasks.

Make sure to revisit this setup guide if you encounter any issues with your environment, and don’t hesitate to reach out for troubleshooting help!