Mosaic#

Basic example of a mosaic.

Initialization of the notebook#

  • Imports

  • Loggers

  • Paths

import os

from cloudpathlib import AnyPath
from eosets.mosaic import Mosaic
from eoreader.bands import MNDWI, RED, SLOPE, CLOUDS
from eoreader.env_vars import DEM_PATH
# Create logger
import logging
from sertit import logs

logs.init_logger(logging.getLogger("eoreader"))
logs.init_logger(logging.getLogger("eosets"))
# Get the base paths
data_path = AnyPath(r"/home/ds2_db3/CI/eosets/DATA")
db_path = AnyPath(r"/home/ds2_db2/BASES_DE_DONNEES")
# Set a DEM in order to compte the slope over the mosaic
dem_sub_dir_path = ["GLOBAL", "COPDEM_30m", "COPDEM_30m.vrt"]
os.environ[DEM_PATH] = str(db_path.joinpath(*dem_sub_dir_path))
# Get a list of Sentinel-2 data with the same day
s2_paths = [
    data_path / "S2B_MSIL2A_20220228T102849_N0400_R108_T32TLT_20220228T134712.SAFE",
    data_path / "S2B_MSIL2A_20220228T102849_N0400_R108_T32ULU_20220228T134712.SAFE",
    data_path / "S2B_MSIL2A_20220228T102849_N0400_R108_T32ULV_20220228T134712.SAFE",
]

Creation of the mosaic#

You just have to give the paths to your Sentinel-2 producs. Their extent or their footprints should be contiguous, and they need to be acquired on the same day.

You can choose a mosaicing method. VRT advised as it is lightweight and faster.

# Then with compatible
mosaic = Mosaic(s2_paths, mosaic_method="VRT")

Stack#

Create a stack of 4 bands, mosaicing the 3 Sentinel-2 products.

# Stack with a resolution of 60m
stack = mosaic.stack(
    [MNDWI, RED, SLOPE, CLOUDS],
    pixel_size=60,
)
2026-01-06 16:43:09,012 - [DEBUG] - *** Loading ['MNDWI', 'RED', 'SLOPE', 'CLOUDS'] for 20220228T102849_S2_T32TLT_L2A_134712 ***
2026-01-06 16:43:09,057 - [DEBUG] - Loading bands ['RED', 'GREEN', 'SWIR_1']
2026-01-06 16:43:09,084 - [DEBUG] - Read RED
2026-01-06 16:43:09,156 - [DEBUG] - Manage nodata for band RED
2026-01-06 16:43:10,020 - [DEBUG] - Converting RED to reflectance (if needed)
2026-01-06 16:43:10,040 - [DEBUG] - Clip the reflectance array to 0 as minimum value (in some cases, reflectance can have higher value than 1)
2026-01-06 16:43:14,879 - [DEBUG] - Read GREEN
2026-01-06 16:43:14,935 - [DEBUG] - Manage nodata for band GREEN
2026-01-06 16:43:15,032 - [DEBUG] - Converting GREEN to reflectance (if needed)
2026-01-06 16:43:15,035 - [DEBUG] - Clip the reflectance array to 0 as minimum value (in some cases, reflectance can have higher value than 1)
2026-01-06 16:43:19,655 - [DEBUG] - Read SWIR_1
2026-01-06 16:43:19,716 - [DEBUG] - Manage nodata for band SWIR_1
2026-01-06 16:43:19,810 - [DEBUG] - Converting SWIR_1 to reflectance (if needed)
2026-01-06 16:43:19,813 - [DEBUG] - Clip the reflectance array to 0 as minimum value (in some cases, reflectance can have higher value than 1)
2026-01-06 16:43:21,623 - [DEBUG] - Loading indices ['MNDWI']
2026-01-06 16:43:27,980 - [DEBUG] - Loading DEM bands ['SLOPE']
2026-01-06 16:43:27,981 - [DEBUG] - Warping DEM for 20220228T102849_S2_T32TLT_L2A_134712
2026-01-06 16:43:27,983 - [DEBUG] - Using DEM: /home/ds2_db2/BASES_DE_DONNEES/GLOBAL/COPDEM_30m/COPDEM_30m.vrt
2026-01-06 16:43:31,466 - [DEBUG] - Computing slope for 20220228T102849_S2_T32TLT_L2A_134712
2026-01-06 16:43:40,485 - [DEBUG] - Loading Cloud bands ['CLOUDS']
2026-01-06 16:43:40,486 - [DEBUG] - Loading CLDPRB mask
2026-01-06 16:43:47,807 - [DEBUG] - *** Loading ['MNDWI', 'RED', 'SLOPE', 'CLOUDS'] for 20220228T102849_S2_T32ULU_L2A_134712 ***
2026-01-06 16:43:47,812 - [DEBUG] - Loading bands ['RED', 'GREEN', 'SWIR_1']
2026-01-06 16:43:47,844 - [DEBUG] - Read RED
2026-01-06 16:43:47,905 - [DEBUG] - Manage nodata for band RED
2026-01-06 16:43:48,034 - [DEBUG] - Converting RED to reflectance (if needed)
2026-01-06 16:43:48,056 - [DEBUG] - Clip the reflectance array to 0 as minimum value (in some cases, reflectance can have higher value than 1)
2026-01-06 16:43:52,675 - [DEBUG] - Read GREEN
2026-01-06 16:43:52,750 - [DEBUG] - Manage nodata for band GREEN
2026-01-06 16:43:52,858 - [DEBUG] - Converting GREEN to reflectance (if needed)
2026-01-06 16:43:52,860 - [DEBUG] - Clip the reflectance array to 0 as minimum value (in some cases, reflectance can have higher value than 1)
2026-01-06 16:43:57,337 - [DEBUG] - Read SWIR_1
2026-01-06 16:43:57,397 - [DEBUG] - Manage nodata for band SWIR_1
2026-01-06 16:43:57,490 - [DEBUG] - Converting SWIR_1 to reflectance (if needed)
2026-01-06 16:43:57,492 - [DEBUG] - Clip the reflectance array to 0 as minimum value (in some cases, reflectance can have higher value than 1)
2026-01-06 16:43:59,285 - [DEBUG] - Loading indices ['MNDWI']
2026-01-06 16:44:07,061 - [DEBUG] - Loading DEM bands ['SLOPE']
2026-01-06 16:44:07,062 - [DEBUG] - Warping DEM for 20220228T102849_S2_T32ULU_L2A_134712
2026-01-06 16:44:07,063 - [DEBUG] - Using DEM: /home/ds2_db2/BASES_DE_DONNEES/GLOBAL/COPDEM_30m/COPDEM_30m.vrt
2026-01-06 16:44:15,890 - [DEBUG] - Computing slope for 20220228T102849_S2_T32ULU_L2A_134712
2026-01-06 16:44:42,454 - [DEBUG] - Loading Cloud bands ['CLOUDS']
2026-01-06 16:44:42,456 - [DEBUG] - Loading CLDPRB mask
2026-01-06 16:44:53,169 - [DEBUG] - *** Loading ['MNDWI', 'RED', 'SLOPE', 'CLOUDS'] for 20220228T102849_S2_T32ULV_L2A_134712 ***
2026-01-06 16:44:53,174 - [DEBUG] - Loading bands ['RED', 'GREEN', 'SWIR_1']
2026-01-06 16:44:53,203 - [DEBUG] - Read RED
2026-01-06 16:44:53,291 - [DEBUG] - Manage nodata for band RED
2026-01-06 16:44:53,415 - [DEBUG] - Converting RED to reflectance (if needed)
2026-01-06 16:44:53,450 - [DEBUG] - Clip the reflectance array to 0 as minimum value (in some cases, reflectance can have higher value than 1)
2026-01-06 16:44:58,588 - [DEBUG] - Read GREEN
2026-01-06 16:44:58,648 - [DEBUG] - Manage nodata for band GREEN
2026-01-06 16:44:58,747 - [DEBUG] - Converting GREEN to reflectance (if needed)
2026-01-06 16:44:58,750 - [DEBUG] - Clip the reflectance array to 0 as minimum value (in some cases, reflectance can have higher value than 1)
2026-01-06 16:45:03,422 - [DEBUG] - Read SWIR_1
2026-01-06 16:45:03,481 - [DEBUG] - Manage nodata for band SWIR_1
2026-01-06 16:45:03,576 - [DEBUG] - Converting SWIR_1 to reflectance (if needed)
2026-01-06 16:45:03,578 - [DEBUG] - Clip the reflectance array to 0 as minimum value (in some cases, reflectance can have higher value than 1)
2026-01-06 16:45:05,232 - [DEBUG] - Loading indices ['MNDWI']
2026-01-06 16:45:11,188 - [DEBUG] - Loading DEM bands ['SLOPE']
2026-01-06 16:45:11,188 - [DEBUG] - Warping DEM for 20220228T102849_S2_T32ULV_L2A_134712
2026-01-06 16:45:11,190 - [DEBUG] - Using DEM: /home/ds2_db2/BASES_DE_DONNEES/GLOBAL/COPDEM_30m/COPDEM_30m.vrt
2026-01-06 16:45:14,199 - [DEBUG] - Computing slope for 20220228T102849_S2_T32ULV_L2A_134712
2026-01-06 16:45:22,740 - [DEBUG] - Loading Cloud bands ['CLOUDS']
2026-01-06 16:45:22,740 - [DEBUG] - Loading CLDPRB mask
2026-01-06 16:45:30,730 - [DEBUG] - Merging bands MNDWI
2026-01-06 16:45:30,875 - [DEBUG] - Merging bands RED
2026-01-06 16:45:30,990 - [DEBUG] - Merging bands SLOPE
0...10...20...30...40...50...60...70...80...90...100 - done.
0...10...20...30...40...50...60...70...80...90...100 - done.
2026-01-06 16:45:31,106 - [DEBUG] - Merging bands CLOUDS
2026-01-06 16:45:31,193 - [DEBUG] - Collocating bands
2026-01-06 16:45:31,201 - [DEBUG] - Stacking
0...10...20...30...40...50...60...70...80...90...100 - done.
0...10...20...30...40...50...60...70...80...90...100 - done.
# Scale the mosaic in order to print it
from sertit.display import scale
scaled = stack.copy(data=scale(stack.data))
# Plot the mosaic
import cartopy.crs as ccrs
crs = ccrs.UTM("32")
scaled[0:3, :, :].plot.imshow(
    robust=True,
    transform=crs,
    x="x",
    y="y",
    figsize=[10, 30],
    subplot_kws={'projection':crs}
)
<matplotlib.image.AxesImage at 0x7fa2db089290>
../_images/733bd820dfdee576bc4c103129ae3ad3b7c828a4c5f8de6b609ffec1b1250718.png