Installing geospatial Python packages on Windows can be challenging due to complex binary dependencies and the need for pre-compiled wheels. The rgeowheels package dramatically simplifies this process by providing convenient access to Christoph Gohlke’s curated collection of pre-compiled geospatial wheels, especially for GDAL and related libraries.
Note: This vignette demonstrates Windows-specific workflows. The code examples will only execute successfully on Windows systems, as rgeowheels provides pre-compiled wheels specifically for Windows architectures (win32, win_amd64, win_arm64).
This vignette demonstrates how to:
This workflow is particularly useful for:
The recommended approach is to create a dedicated Python virtual environment for your project. This isolates your project dependencies from system Python and other projects.
If you already have Python installed, you can create a virtual environment directly in R using reticulate:
library(reticulate)
# Create a virtual environment in your project directory
venv_path <- file.path(getwd(), ".venv")
virtualenv_create(venv_path, python = Sys.which("python"))
# Activate the virtual environment
use_virtualenv(venv_path, required = TRUE)Alternatively, create the venv from the command line:
Before installing wheels, it’s helpful to understand what Python
environments are available. The detect_python_envs()
function scans your system for virtual environments and conda
environments:
You can also check the Python version of a specific environment:
# Get the version of the currently active Python
current_version <- detect_python_version()
current_version
# Or check a specific Python binary (wrapped in try to handle venv if not present)
venv_python <- file.path(".venv", "Scripts", "python.exe") # Windows
if (file.exists(venv_python)) {
venv_version <- detect_python_version(venv_python)
venv_version
}Once you have a Python environment set up and activated, installing GDAL is straightforward:
The simplest approach is to use pyversion = "auto" to
automatically detect your Python version and match it to available
wheels:
library(rgeowheels)
library(reticulate)
# Ensure your virtual environment is active
use_virtualenv(".venv", required = TRUE)
# Install GDAL - automatically detects Python version
install_wheel("GDAL", version = "latest", pyversion = "auto")This will:
"Auto-selected Python 3.11 for GDAL"pipIf you want to use auto-detection but suppress the informational message (useful in scripts or CI), you have three options:
Option 1: Set an environment variable
Option 2: Set an R option
Option 3: Explicitly specify the Python version
If you need a specific version of GDAL for a specific Python version:
Before installing, you can see what versions are available:
# List all available wheels
assets <- list_rgeowheels_assets()
# Filter for GDAL
gdal_wheels <- assets[assets$package == "GDAL", ]
print(gdal_wheels[, c("package", "version", "pyversion", "architecture")])
# Or with a specific Python version
gdal_py311 <- assets[assets$package == "GDAL" & assets$pyversion == "3.11", ]Here’s a complete workflow combining reticulate venv creation with rgeowheels installation:
library(reticulate)
library(rgeowheels)
# 1. Create a virtual environment
venv_path <- file.path(getwd(), ".venv")
if (!dir.exists(venv_path)) {
virtualenv_create(venv_path, python = Sys.which("python"))
}
# 2. Activate it
use_virtualenv(venv_path, required = TRUE)
# 3. Detect available Python in this venv
detect_python_envs()
# 4. Install geospatial wheels
install_wheel("GDAL", version = "latest", pyversion = "auto")
install_wheel("rasterio", version = "latest", pyversion = "auto")
install_wheel("fiona", version = "latest", pyversion = "auto")
# 5. Verify installations
py_run_string("from osgeo import gdal; print(f'GDAL {gdal.__version__}')")
py_run_string("import rasterio; print(f'Rasterio {rasterio.__version__}')")
# 6. Use Python objects in R
py_run_string("
import rasterio
from rasterio.plot import show
# ... your geospatial Python code here
")If you see an error like:
could not find wheels for:
- 'GDAL' version 'latest' for Python '3.9' (win_amd64)
Available Python versions for 'GDAL' (win_amd64): 3.10, 3.11, 3.12
This means GDAL wheels aren’t available for Python 3.9. You have two options:
If detect_python_version() fails with “Python binary not
found”, ensure:
Sys.which("python")use_virtualenv(".venv")If you have multiple Python installations, you can explicitly set which one rgeowheels uses:
Here’s a sample abbreviated YAML configuration for using rgeowheels in GitHub Actions CI:
name: Test with GDAL
on: [push, pull_request]
jobs:
test:
runs-on: windows-latest
strategy:
matrix:
python-version: ['3.10', '3.11', '3.12']
r-version: ['4.2', '4.3']
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- uses: r-lib/actions/setup-r@v2
with:
r-version: ${{ matrix.r-version }}
- name: Install R packages
run: |
install.packages(c("reticulate", "rgeowheels"))
shell: Rscript {0}
- name: Install Python geospatial wheels
run: |
library(rgeowheels)
library(reticulate)
use_python(Sys.which("python"))
install_wheel("GDAL", pyversion = "auto")
install_wheel("rasterio", pyversion = "auto")
shell: Rscript {0}
- name: Run tests
run: |
Rscript -e 'tinytest::test_all()'The rgeowheels workflow provides significant advantages on Windows:
install_wheel()By combining rgeowheels with reticulate’s virtual environment management, you can create reproducible, isolated Python development environments on Windows with geospatial dependencies installed in seconds. This is especially valuable for:
For more information, see ?install_wheel,
?detect_python_version, and
?detect_python_envs.