Mosaic Module¶
The mosaic module provides functionality for creating spatial mosaics from multiple overlapping raster images. This is essential when working with satellite imagery that spans multiple tiles or when combining data from different acquisition times.
Overview¶
Mosaicking is the process of combining multiple raster images into a single, seamless composite. The module handles:
- Spatial alignment: Reprojecting images to a common coordinate reference system
- No-data handling: Filling gaps in one image with data from overlapping images
- Masking: Custom masking functions to exclude invalid pixels (clouds, shadows, etc.)
- Memory efficiency: Window-based processing for large datasets
βββββββββββ βββββββββββ
β Image 1 β β Image 2 β Input: Multiple overlapping rasters
β ββββ β β ββββ β
β ββββ β β ββββ β
βββββββββββ βββββββββββ
\ /
\ /
β β
βββββββββββββ
β Mosaic β Output: Single seamless composite
β ββββββββ β
β ββββββββ β
βββββββββββββ
Quick Start¶
from georeader import mosaic
from georeader.rasterio_reader import RasterioReader
# Load multiple overlapping images
images = [
RasterioReader("image1.tif"),
RasterioReader("image2.tif"),
RasterioReader("image3.tif"),
]
# Create mosaic over the union of all footprints
result = mosaic.spatial_mosaic(images)
# Create mosaic over a specific polygon
from shapely.geometry import box
aoi = box(minx, miny, maxx, maxy)
result = mosaic.spatial_mosaic(images, polygon=aoi, crs_polygon="EPSG:4326")
Key Functions¶
spatial_mosaic¶
The main function for creating mosaics from a list of raster images.
Create a spatial mosaic by filling gaps with data from overlapping rasters.
Combines multiple rasters into a single output by iteratively filling nodata regions with valid data from subsequent rasters. The first raster is used as the base, and remaining rasters fill in only where the base has nodata values.
This function is similar to rasterio.merge.merge but with support for:
- Custom validity masks per raster
- Masking functions (e.g., cloud masks)
- Windowed processing for memory efficiency
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_list
|
Union[List[GeoData], List[Tuple[GeoData, GeoData]]]
|
Input rasters to mosaic. Can be:
Rasters are processed in order; first valid pixel wins. |
required |
polygon
|
Optional[Polygon]
|
Output extent as a shapely Polygon. If provided, mosaic is clipped to this polygon. CRS specified by crs_polygon. |
None
|
crs_polygon
|
Optional[str]
|
CRS of the polygon. If not provided, uses the CRS of the first raster. |
None
|
dst_transform
|
Optional[Affine]
|
Output transform. If not provided, computed from bounds or polygon. |
None
|
bounds
|
Optional[Tuple[float, float, float, float]]
|
Output extent as (minx, miny, maxx, maxy). Alternative to polygon. |
None
|
dst_crs
|
Optional[str]
|
Output CRS. If not provided, uses CRS of first raster. |
None
|
dtype_dst
|
Optional[str]
|
Output data type. If not provided, uses dtype of first raster. |
None
|
window_size
|
Optional[Tuple[int, int]]
|
Process in tiles of this size (height, width) for memory efficiency. Default None (process all at once). |
None
|
resampling
|
Resampling
|
Resampling method for reprojection. Default cubic_spline for continuous data. |
cubic_spline
|
masking_function
|
Optional[Callable[[GeoData], GeoData]]
|
Function that takes a GeoData and returns a boolean mask of INVALID pixels. Applied to each raster before mosaicking. |
None
|
dst_nodata
|
Optional[int]
|
Output nodata value. If not provided, uses fill_value_default of first raster. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
GeoTensor |
GeoTensor
|
Mosaic covering the specified extent. Nodata regions are filled by iterating through data_list until all pixels are valid or list is exhausted. |
Examples:
Basic mosaic of overlapping Sentinel-2 scenes:
>>> from georeader import mosaic
>>> from georeader.rasterio_reader import RasterioReader
>>>
>>> # Load overlapping scenes
>>> scene1 = RasterioReader("scene1.tif")
>>> scene2 = RasterioReader("scene2.tif")
>>> scene3 = RasterioReader("scene3.tif")
>>>
>>> # Create seamless mosaic
>>> result = mosaic.spatial_mosaic(
... [scene1, scene2, scene3],
... bounds=(-122.5, 37.0, -121.5, 38.0),
... dst_crs="EPSG:4326"
... )
Mosaic with cloud masks (tuple format):
>>> # Each tuple is (data, cloud_mask) where cloud_mask=True means cloudy
>>> result = mosaic.spatial_mosaic(
... [(scene1, cloud1), (scene2, cloud2), (scene3, cloud3)],
... bounds=(-122.5, 37.0, -121.5, 38.0),
... dst_crs="EPSG:4326"
... )
>>> # Cloud-covered pixels in scene1 are filled from scene2, etc.
Memory-efficient tiled processing:
>>> result = mosaic.spatial_mosaic(
... large_scene_list,
... bounds=extent,
... window_size=(1024, 1024) # Process in 1024x1024 tiles
... )
See Also
georeader.read.read_reproject: Underlying reprojection function. rasterio.merge.merge: Similar functionality in rasterio.
Note
- Processing order matters: earlier rasters have priority
- Use window_size for large outputs to avoid memory issues
- Set appropriate resampling for your data type (nearest for categorical)
Source code in georeader/mosaic.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 | |
Algorithm Details¶
The spatial_mosaic function uses an iterative fill algorithm:
- Initialize output: Load the first image, reprojecting to target CRS/bounds
- Track invalid pixels: Identify pixels with no-data values
- Iterative fill: For each subsequent image:
- Skip if footprint doesn't intersect remaining invalid regions
- Load only the overlapping region
- Fill invalid pixels with valid data from the new image
- Update the invalid pixel mask
- Early termination: Stop when all pixels are valid
Iteration 1 Iteration 2 Final Result
βββββββββββββ βββββββββββββ βββββββββββββ
β ββββ ββββ β + β ββββ ββββ β = β ββββ ββββ β
β ββββ ββββ β β ββββ ββββ β β ββββ ββββ β
βββββββββββββ βββββββββββββ βββββββββββββ
(from image 1) (from image 2) (complete)
ββββ = valid data ββββ = no-data
Working with Masks¶
The module supports custom masking functions to exclude invalid pixels beyond simple no-data values:
def cloud_mask_function(geotensor):
"""Custom mask function that returns True for invalid pixels."""
# Example: mask pixels where any band exceeds threshold
return geotensor.values.max(axis=0) > 10000
# Use with spatial_mosaic
result = mosaic.spatial_mosaic(
images,
masking_function=cloud_mask_function
)
# Or provide explicit masks as tuples
images_with_masks = [
(image1, mask1),
(image2, mask2),
]
result = mosaic.spatial_mosaic(images_with_masks)
Memory-Efficient Processing¶
For large mosaics, use the window_size parameter to process in tiles:
# Process in 512x512 windows
result = mosaic.spatial_mosaic(
images,
window_size=(512, 512),
polygon=large_aoi,
crs_polygon="EPSG:4326"
)
Resampling Methods¶
The resampling parameter controls interpolation during reprojection:
| Method | Use Case |
|---|---|
nearest |
Categorical data (land cover, masks) |
bilinear |
Continuous data, faster |
cubic |
Continuous data, smoother |
cubic_spline |
Default, best quality for most imagery |
import rasterio.warp
result = mosaic.spatial_mosaic(
images,
resampling=rasterio.warp.Resampling.bilinear
)
See Also¶
read.read_reproject: Underlying reprojection functionslices: Tiling utilities for windowed processing