Skip to content

Commit 6d15c5e

Browse files
Merge pull request #61 from gregory-halverson-jpl/main
fixing multi-raster mosaics
2 parents fc0e79e + 3896b72 commit 6d15c5e

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "rasters"
7-
version = "1.15.1"
7+
version = "1.16.0"
88
description = "raster processing toolkit"
99
readme = "README.md"
1010
authors = [

rasters/mosaic.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66

77
if TYPE_CHECKING:
88
from .raster import Raster
9+
from .multi_raster import MultiRaster
910
from .raster_geometry import RasterGeometry
1011

1112
def mosaic(
12-
images: Iterator[Union[Raster, str]],
13-
geometry: RasterGeometry,
13+
images: Iterator[Union[Raster, MultiRaster, str]],
14+
geometry: RasterGeometry = None,
1415
resampling: str = "nearest") -> Raster:
1516
"""
1617
Creates a mosaic from a sequence of Raster images.
@@ -30,12 +31,25 @@ def mosaic(
3031
"""
3132
from .where import where # Assuming 'where' is a function in the same package
3233
from .raster import Raster # Import Raster here to avoid circular dependency
34+
from .multi_raster import MultiRaster # Import MultiRaster here to avoid circular dependency
3335

34-
mosaic = Raster(np.full(geometry.shape, np.nan), geometry=geometry) # Initialize with NaN values
36+
if len(images) == 0:
37+
raise ValueError("No images provided for mosaicking.")
38+
39+
if len(images[0].shape == 2):
40+
mosaic = Raster(np.full(geometry.shape, np.nan), geometry=geometry)
41+
elif len(images[0].shape == 3):
42+
mosaic = MultiRaster(np.full(geometry.shape, np.nan), geometry=geometry)
43+
else:
44+
raise ValueError("Unsupported image shape for mosaicking.")
45+
3546
dtype = None
3647
nodata = None
3748
metadata = None
3849

50+
if geometry is None:
51+
geometry = images[0].geometry
52+
3953
for image in images:
4054
if isinstance(image, str):
4155
image = Raster.open(image) # Open the image if it's a file path
@@ -50,6 +64,12 @@ def mosaic(
5064
mosaic = where(np.isnan(mosaic), image.to_geometry(geometry, resampling=resampling), mosaic)
5165

5266
mosaic = mosaic.astype(dtype) # Set the data type of the mosaic
53-
mosaic = Raster(mosaic, geometry=geometry, nodata=nodata, metadata=metadata) # Create the final Raster
67+
68+
if len(mosaic.shape) == 2:
69+
mosaic = Raster(mosaic, geometry=geometry, nodata=nodata, metadata=metadata) # Create the final Raster
70+
elif len(mosaic.shape) == 3:
71+
mosaic = MultiRaster(mosaic, geometry=geometry, nodata=nodata, metadata=metadata) # Create the final MultiRaster
72+
else:
73+
raise ValueError("Unsupported mosaic shape after processing.")
5474

5575
return mosaic

0 commit comments

Comments
 (0)