This action installs the OSGeo/gdal Python package, so the workflow can access it.
Simply running pip install gdal
usually does not help, as pypi.org does not include
any C extensions. This make the CI workflows where GDAL is required quite complicated, as the binaries must be build.
The action is intended to encapsulate all of these steps.
See action.yml, also the build-wheel.yml workflow as usage example
steps:
- uses: actions/setup-python@v5
- uses: trundev/setup-gdal@main
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- run: python -m osgeo_utils.samples.gdalinfo --formats
- run: python -c "from osgeo import gdal; print(gdal.__name__)"
Note: Under
macOS
, due to some issues withDYLD_LIBRARY_PATH
, the default shell (bash -e
) may not work. It is recomended to useshell: pwsh
orshell: python
(inlined python code).
All the input parameters are optional
gdal-version
- The GDAL version to be installed. This is actually a git-tag (or any reference) from OSGeo/gdal. For Windows runners anduse-conda=false
, it must match the version to be installed bypip
.rebuild-cache
- Whentrue
the internal cache for specific configuration (if exists) will be discarded and a fresh one will be createdbase-dir
- The base directory under$GITHUB_WORKSPACE
to perform the buildcache-key-prefix
- Extra string to be added to the cache-key. To be used as a workaround, if the cache is incompatible between different versions of the same OS (likemacOS-11
andmacOS-12
).use-conda
- Use conda, instead of GISInternals/system packagesextra-packages
- Install extra (optional) packages to be used by GDAL build processGITHUB_TOKEN
- GitHub token to allow installing from a NuGet package. When requested package is available, this skips the build steps, just like when using actions cache.
Default inputs example
steps:
- uses: trundev/setup-gdal@main
with:
gdal-version: 'v3.9.3'
rebuild-cache: 'false'
base-dir: 'GDAL~'
cache-key-prefix: ''
use-conda: 'true'
extra-packages: ''
GITHUB_TOKEN: ''
wheel-path
- Path to the pip-wheel generated by build process, which includes the binary modules and tools. This is the only file kept by the internal cacheosgeo-path
- Path to theosgeo
folder under Pythonimportlib
structureutils-path
- Path to theosgeo_utils
folder under Pythonimportlib
structure. There are installed many of the GDAL programs
GDAL_DRIVER_PATH
- Path to GDALplugins
, if availableGDAL_DATA
- Path to the various GDAL data files, see GDAL general optionsPROJ_DATA
- Path to the proj-data package, if availablePATH
- [Windows only] The variable is updated withosgeo-path
to allow loading ofgdal.dll
LD_LIBRARY_PATH
- [Linux & Mac] The variable is updated withosgeo-path
to allow loading oflibgdal.so
DYLD_LIBRARY_PATH
- [Mac only] The variable is updated withosgeo-path
to allow loading oflibgdal.dylib
Use of outputs example
steps:
- uses: trundev/setup-gdal@main
id: gdal
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- run: echo "wheel-path: ${{ steps.gdal.outputs.wheel-path }}"
- run: echo "osgeo-path: ${{ steps.gdal.outputs.osgeo-path }}"
- run: echo "utils-path: ${{ steps.gdal.outputs.utils-path }}"
The action checks-out the required GDAL version, then builds it for the specific Github runner environment. This includes installation of necessary extra packages. The build process generates a complete Python wheel package for the specific runnable. It includes even the binaries from the GDAL programs and optionally the GDAL driver plugins and PROJ package.
An actions cache is used to improve execution time. The cache keeps only the generated wheel-file, which is intended to includes everything needed. In case of a cache hit, the installed packages might be reduced only to the ones mandatory for loading of the Python modules.
Besides the cache, the action can try to install a GDAL python wheel from one of prebuild NuGet packages,
available here.
This requires rebuild-cache
to be false
(default) and GITHUB_TOKEN
to be set to a GitHub token with
"read:packages" access (usually ${{ secrets.GITHUB_TOKEN }}
).
Note that, because of the reduction of installed packages, some GDAL modules may NOT work after a cached wheel was used, even if they were working at first run.
For example
osgeo.gdal_array
needsnumpy
, but it is only installed in order to build the binaries. In all cases, such modules should be explicitly installed by separate workflow step, likepip install numpy
.
For a purpose of investigation of any issues with this Python wheel-file, it can be obtained as an artifact from the build-wheel.yml workflow, see Build GDAL python wheel. The "conda" builds are mostly self-contained and usually do not require extra installations, except the Windows one which may require Microsoft ODBC Driver 17.
Activated by
use-conda: 'true'
The setup process uses setup-miniconda
action to install conda and some minimal set of the required external packages. Then, performs
a full cmake
build of the sources from GDAL repository, the commit
is selected by gdal-version
.
Finally, a wheel is build from the generated result via standard setup.py bdist_wheel
procedure.
The extra-packages
input parameter can be used to install additional external packages, before the cmake
build. This allows adding support for more geospatial data formats.
Activated by
use-conda: 'false'
Preparation differs for Windows vs. Linux and MacOS runner OS:
-
Windows: the prebuild development kit from https://www.gisinternals.com/ is used. With the help of this SDK, just
pip wheel gdal
is enough to create wheel including the.pyd
extensions. The rest of GDAL related DLLs are added later.This process is much faster, than the full-build approach, but lacks GDAL version / runner OS flexibility.
-
Linux / MacOS: The required external packages are installed via
apt-get
orbrew
, then continue just like "Setup using conda packages".The
extra-packages
input can be used, to install additionalapt-get
orbrew
packages, similar to the conda build.
After the wheel is created, by pip wheel gdal
or setup.py bdist_wheel
, it still does not include the
dependent GDAL .dll
-s, .so
-s or .dylib
-s. This is fixed by repacking the wheel, by adding these
dependencies. This step also adds (for convenience only):
- GDAL programs under
osgeo_utils/apps
- The extra GDAL plugins under
osgeo_utils/plugins
(env-varGDAL_DRIVER_PATH
) - Various GDAL data files under
osgeo_utils/gdal-data
(env-varGDAL_DATA
) - The proj-data package under
osgeo_utils/proj
(env-varPROJ_DATA
)
Code from Reproject a Geometry
steps:
- uses: trundev/setup-gdal@main
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- shell: python
run: |
from osgeo import ogr
from osgeo import osr
source = osr.SpatialReference()
source.ImportFromEPSG(2927)
target = osr.SpatialReference()
target.ImportFromEPSG(4326)
transform = osr.CoordinateTransformation(source, target)
point = ogr.CreateGeometryFromWkt("POINT (1120351.57 741921.42)")
point.Transform(transform)
print(point.ExportToWkt())