Create App using GDAL

Hello:

I am working on creating an app located here,

which will read in some files with a .tif extension from my space. These are geospatial files which require the rioxarray package in python (Rioxarray :: Anaconda.org), which in turn requires something called gdal (https://anaconda.org/conda-forge/gdal).

I have a requirements file which includes the following:

leafmap==0.29.1
matplotlib==3.8.2
numpy==1.26.2
pandas==2.1.3
rasterio==1.3.9
rioxarray==0.15.0
scikit_learn==1.2.2
streamlit==1.28.2

According to this question Deploy in Streamlit cloud with GDAL, I also need a packages.txt file with libgdal-dev which I have also included, but even so when building my app it fails with the following:

File "/home/user/.local/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 534, in _run_script
    exec(code, module.__dict__)
File "/home/user/app/app.py", line 220, in <module>
    m.add_raster(selected_file, bands=[6], layer_name = 'dNBR', colormap='viridis')
File "/home/user/.local/lib/python3.10/site-packages/leafmap/foliumap.py", line 499, in add_raster
    tile_layer, tile_client = get_local_tile_layer(
File "/home/user/.local/lib/python3.10/site-packages/leafmap/common.py", line 2818, in get_local_tile_layer
    from osgeo import gdal

Hi @sm-potter

Could you also try creating an environment.yml file to get gdal installed on Community Cloud as mentioned in the Docs (App dependencies - Streamlit Docs)

and for more details on creating the yml file, check out the following:

https://conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#creating-an-environment-file-manually

IMHO you don’t need conda, this should do the job:

packages.txt

libgdal-dev

requirements.txt

pygdal==3.6.2.*

Be aware that the version of the binary package gdal and pygdal must match, see the documentation of the pygdal package! Therefore the above version of pygdal will change in the future or may be different on different linux OSes.


The pip install of pygdal seems to check the existing gdal version on the system. If you try to install pygdal on top of an gdal version that does not match, you will get:

GDALConfigError: Version mismatch 3.6.2 != 3.7.0

Someone has thought ahead :+1: :+1: :+1:


You could also check the versions in your application, for example:

import subprocess
from osgeo import gdal

pygdal_version = gdal.__version__
gdal_version = subprocess.check_output(['gdal-config','--version']).decode('utf-8').strip()

print(f'pygdal version: {pygdal_version}')
print(f'gdal version: {gdal_version}')

if pygdal_version == gdal_version:
    print('GDAL versions match')
else:
    print('GDAL versions do not match')
1 Like