Skip to content

Stack multi bands raster - #922

Open
marinebcht wants to merge 13 commits into
GlacioHack:mainfrom
marinebcht:899_stack_rasters_rebase
Open

Stack multi bands raster#922
marinebcht wants to merge 13 commits into
GlacioHack:mainfrom
marinebcht:899_stack_rasters_rebase

Conversation

@marinebcht

@marinebcht marinebcht commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

This PR updates stack_rasters() to stack all bands from multiband rasters, instead of only the first band of each input raster.

import geoutils as gu
rgb = gu.Raster(gu.examples.get_path("everest_landsat_rgb")) 
b4 = gu.Raster(gu.examples.get_path("everest_landsat_b4"))
rgbnir = gu.raster.stack_rasters([rgb,b4]) 

rgbnir now contains [red, green, blue, b4] instead of [red, b4]

Dev info

What has changed :

  • removed the error raised nb bands > 1 removed
  • iterate over all bands of each input raster and append them to the output raster
  • removed progress param that was not used
  • updated merge_algorithm param in the merge_raster test to be used with several (3d image + 3d image) input bands and not only two (1d image + 1d image)

Note: temporary fix version of Affine < 3 to work with Rasterio #922 (comment)

Comment thread geoutils/raster/multiraster.py Outdated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

progress is never called

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

progress is an old parameter not used since several version

for raster in tqdm(rasters, disable=not progress):
=> deleted

Comment thread tests/test_raster/test_multiraster.py Outdated
@marinebcht
marinebcht marked this pull request as draft August 31, 2026 09:54
@marinebcht
marinebcht marked this pull request as ready for review August 31, 2026 14:46
Comment thread tests/test_raster/test_multiraster.py Outdated
@@ -291,7 +345,7 @@ def test_merge_rasters(self, rasters: Any) -> None: # type: ignore

# For merge algo: function not supporting the axis keyword argument but raising the right "axis" type error

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rhugonnet not so sure about this test

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know. This is in merge_rasters, not stack... Did it stop working?

@marinebcht

Copy link
Copy Markdown
Contributor Author

@rhugonnet @belletva I think it is done
Will do the merge_rasters in another PR :)

@rhugonnet rhugonnet left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good for me!
Error on merge tests: You could skip it until the next PR to simplify things 😉

@marinebcht

marinebcht commented Sep 10, 2026

Copy link
Copy Markdown
Contributor Author

Hiii
So, I had this CI error :

    @pytest.mark.parametrize("dtype", ["float32", "uint8", "int32"])
    def test_numpy_functions(self, dtype: str) -> None:
        """Test how rasters can be used as/with numpy arrays."""
        warnings.simplefilter("error")
    
        # Create an array of unique values starting at 0 and ending at 24
        array = np.arange(25, dtype=dtype).reshape((1, 5, 5))
        # Create an associated dummy transform
>       transform = rio.transform.from_origin(0, 5, 1, 1)

tests/test_raster/test_raster.py:2823: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
../../../miniconda3/envs/geoutils-dev/lib/python3.14/site-packages/rasterio/transform.py:178: in from_origin
    return Affine.translation(west, north) * Affine.scale(xsize, -ysize)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = Affine(1.0, 0.0, 0.0,
       0.0, 1.0, 5.0)
other = Affine(1.0, 0.0, 0.0,
       0.0, -1.0, 0.0)

    def __mul__(self, other):
        """Multiplication.
    
        Apply the transform using matrix multiplication, creating
        a resulting object of the same type.  A transform may be applied
        to another transform, a vector, vector array, or shape.
    
        Parameters
        ----------
        other : Affine or iterable of (vx, vy)
    
        Returns
        -------
        Affine or a tuple of two items
    
        .. deprecated:: 3.1.0
            Use `@` matmul instead of `*` mul operator for matrix multiplication.
        """
        # TODO: consider elevating to DeprecationWarning for 3.2
>       warnings.warn(
            "Use `@` matmul instead of `*` mul operator for matrix multiplication",
            PendingDeprecationWarning,
            stacklevel=2,
        )
E       PendingDeprecationWarning: Use `@` matmul instead of `*` mul operator for matrix multiplication

../../../miniconda3/envs/geoutils-dev/lib/python3.14/site-packages/affine/__init__.py:623: PendingDeprecationWarning

After some checks, I saw that Affine had released a new version and that in the documentation:

Versions before 3.0.0 only supported the * operator for matrix multiplication. To use these examples with previous releases, replace @ with *.

They may also be multiplied together to combine transformations.

Affine.translation(1.0, 5.0) @ Affine.rotation(45.0)
Affine(0.7071067811865476, -0.7071067811865475, 1.0,
0.7071067811865475, 0.7071067811865476, 5.0)

And latest verion of rasterio still use * : Affine.translation(west, north) * Affine.scale(xsize, -ysize)
Conclusion: I fixed affine < 3 (only used in tests)

@marinebcht

Copy link
Copy Markdown
Contributor Author

I can add Raster.merge() in this PR if you want or we can merge like this @belletva @rhugonnet

@rhugonnet

rhugonnet commented Sep 10, 2026

Copy link
Copy Markdown
Member

@marinebcht I fixed the CI warning in #879 following #924. You just have to merge with upstream.

Adding Raster.merge(): sure, minimal effort!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants