Streamlit app aborts when attempting to execute map reprojection for the second time

A simple streamlit app was made for matching the resolution and projection of two raster layers. The app could be executed by clicking on the Run button for the first time, but it would be aborted when clicking on the second time.

Any suggestion? Thank you very much!

Sample TIFF files:

Here’s the code:

import os
import sys
import gc
import rioxarray as rxr
import streamlit as st
from rasterio.io import MemoryFile

@st.cache_data
def read_memory(memoryfile):
    with MemoryFile(memoryfile) as mf:
        with mf.open() as ds:
            data_array = rxr.open_rasterio(ds)
    return data_array.squeeze()

uploaded_files = st.file_uploader("TIF files", type=['tif'], accept_multiple_files=True)

if len(uploaded_files) > 0:

    filenames = [file.name for file in uploaded_files]

    img1_filename = st.selectbox("img1: ", options=filenames)
    img2_filename = st.selectbox("img2: ", options=filenames)

    img1_ind = filenames.index(img1_filename)
    img2_ind = filenames.index(img2_filename)

    img1 = read_memory(uploaded_files[img1_ind])
    img2 = read_memory(uploaded_files[img2_ind])
    
    collected_img = [i for i in ['img1', 'img2'] if i in globals()]
    st.code(collected_img)

    if img1 is not None and img2 is not None:

        if st.button("Run"):
            img3 = img1.rio.reproject_match(img2)

            st.code(f"img1: {img1.shape}, {img1.rio.crs}")
            st.code(f"img2: {img2.shape}, {img2.rio.crs}")
            st.code(f"img3: {img3.shape}, {img3.rio.crs}")

    gc.collect()

# streamlit run pycode\test.py

Here’s the error messages:

RasterioIOError: No such file or directory
Traceback:
File "D:\pyenv\lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 552, in _run_script
    exec(code, module.__dict__)
File "D:\pycode\test.py", line 36, in <module>
    img3 = img1.rio.reproject_match(img2)
File "D:\pyenv\lib\site-packages\rioxarray\raster_array.py", line 546, in reproject_match
    reprojected_data_array = self.reproject(
File "D:\pyenv\lib\site-packages\rioxarray\raster_array.py", line 451, in reproject
    source=self._obj.values,
File "D:\pyenv\lib\site-packages\xarray\core\dataarray.py", line 733, in values
    return self.variable.values
File "D:\pyenv\lib\site-packages\xarray\core\variable.py", line 614, in values
    return _as_array_or_item(self._data)
File "D:\pyenv\lib\site-packages\xarray\core\variable.py", line 314, in _as_array_or_item
    data = np.asarray(data)
File "D:\pyenv\lib\site-packages\xarray\core\indexing.py", line 693, in __array__
    return np.asarray(self.get_duck_array(), dtype=dtype)
File "D:\pyenv\lib\site-packages\xarray\core\indexing.py", line 696, in get_duck_array
    self._ensure_cached()
File "D:\pyenv\lib\site-packages\xarray\core\indexing.py", line 690, in _ensure_cached
    self.array = as_indexable(self.array.get_duck_array())
File "D:\pyenv\lib\site-packages\xarray\core\indexing.py", line 664, in get_duck_array
    return self.array.get_duck_array()
File "D:\pyenv\lib\site-packages\xarray\core\indexing.py", line 551, in get_duck_array
    array = self.array[self.key]
File "D:\pyenv\lib\site-packages\rioxarray\_io.py", line 453, in __getitem__
    return indexing.explicit_indexing_adapter(
File "D:\pyenv\lib\site-packages\xarray\core\indexing.py", line 858, in explicit_indexing_adapter
    result = raw_indexing_method(raw_key.tuple)
File "D:\pyenv\lib\site-packages\rioxarray\_io.py", line 428, in _getitem
    self.manager.acquire(needs_lock=False), self.vrt_params
File "D:\pyenv\lib\site-packages\xarray\backends\file_manager.py", line 193, in acquire
    file, _ = self._acquire_with_cache_info(needs_lock)
File "D:\pyenv\lib\site-packages\xarray\backends\file_manager.py", line 217, in _acquire_with_cache_info
    file = self._opener(*self._args, **kwargs)
File "D:\pyenv\lib\site-packages\rasterio\env.py", line 451, in wrapper
    return f(*args, **kwds)
File "D:\pyenv\lib\site-packages\rasterio\__init__.py", line 320, in open
    dataset = DatasetReader(path, driver=driver, sharing=sharing, **kwargs)
File "rasterio\_base.pyx", line 312, in rasterio._base.DatasetBase.__init__

Hey @p10911004,

Thanks for sharing this question. Just to confirm, you’re running this app on your local machine – it’s not deployed on Community Cloud?

Dear @Caroline ,

Thanks for your attention. Yes, the app was designed to run on local. I’ve tried to mount it on the cloud, but it seems to have the same problems.
https://app-forum-b7j3c3qwvbq.streamlit.app/

* Sample images (img1, img2) are provided on top of the post.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.