import rastereasy

Create Geoimage from single tif bands

This is very useful for creating a stack from SENTINEL-2 images for example

help(rastereasy.files2stack)
Help on function files2stack in module rastereasy.rastereasy:

files2stack(imagefile_path, resolution=None, names='origin', dest_name=None, ext='jp2', history=False)
    Create a stacked Geoimage from multiple single-band images.
    
    This function creates a multi-band Geoimage by stacking individual images,
    either from a list of image paths or from all images in a directory.
    All input images should have 1 band each.
    
    Parameters
    ----------
    imagefile_path : str or list of str
        - If a list of strings: paths to image files to stack (e.g., ['image1.jp2', 'image2.jp2', ...])
        - If a string: path to a directory containing images with the specified extension
    resolution : float, optional
        Resolution to which all images will be resampled. If None, all images must
        have the same resolution already.
        Default is None.
    names : dict or str, optional
        How to name the spectral bands in the stack:
        - If a dict: Maps band names to indices (e.g., {'B': 1, 'G': 2, 'R': 3, ...})
        - If "origin" (default): Uses the original filenames as band names
        - If None: Assigns numeric names ('1', '2', '3', ...)
        Default is "origin".
    dest_name : str, optional
        Path to save the stacked image as a TIFF file.
        Default is None (no file saved).
    ext : str, optional
        File extension of images to load if imagefile_path is a directory.
        Default is 'jp2'.
    history : bool, optional
        Whether to enable history tracking for the output Geoimage.
        Default is False.
    
    Returns
    -------
    Geoimage
        A stacked Geoimage containing all the input images as bands.
    
    Examples
    --------
    >>> # Stack from a list of image files
    >>> list_images = ['band1.jp2', 'band2.jp2', 'band3.jp2']
    >>> stacked_image = files2stack(list_images)
    >>> stacked_image.save('stacked.tif')
    >>>
    >>> # Stack all jp2 files from a directory with resolution resampling
    >>> folder_path = './my_bands_folder'
    >>> stacked_image = files2stack(folder_path, resolution=10)
    >>> stacked_image.info()
    
    Notes
    -----
    This function is particularly useful for combining separate band files (common in
    satellite imagery) into a single multi-band image for analysis.

1) Organization of data

Here, images are in ./data/demo/sentinel/copacabana_ipanema_synthesis/202406/

dir_data = './data/demo/sentinel/copacabana_ipanema_synthesis/202406/'
import os
os.listdir(dir_data)
['B08.tif',
 'B09.tif',
 'B8A.tif',
 'B02.tif',
 'B03.tif',
 'B01.tif',
 'B04.tif',
 'B11.tif',
 'B05.tif',
 'B07.tif',
 'B06.tif',
 'B12.tif']

2) Creation of a stack with band names associated with file names

stack=rastereasy.files2stack(dir_data,ext='tif')
stack.info()
- Size of the image:
   - Rows (height): 369
   - Cols (width): 467
   - Bands: 12
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (-22.97848940, -43.19246031)
- Driver: GTiff
- Data type: int16
- Projection system: EPSG:32723
- Nodata: -32768.0

- Given names for spectral bands: 
   {'B01': 1, 'B02': 2, 'B03': 3, 'B04': 4, 'B05': 5, 'B06': 6, 'B07': 7, 'B08': 8, 'B09': 9, 'B11': 10, 'B12': 11, 'B8A': 12}

3) Creation of a stack with specified resolution

desired_resolution = 30
stack=rastereasy.files2stack(dir_data,ext='tif',resolution=desired_resolution)
stack.info()
- Size of the image:
   - Rows (height): 123
   - Cols (width): 155
   - Bands: 12
- Spatial resolution: 30.129032258064512  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (-22.97857857, -43.19236096)
- Driver: GTiff
- Data type: int16
- Projection system: EPSG:32723
- Nodata: -32768.0

- Given names for spectral bands: 
   {'B01': 1, 'B02': 2, 'B03': 3, 'B04': 4, 'B05': 5, 'B06': 6, 'B07': 7, 'B08': 8, 'B09': 9, 'B11': 10, 'B12': 11, 'B8A': 12}

4) Creation of a stack with reformated names

stack=rastereasy.files2stack(dir_data,ext='tif',names=None)
stack.info()
- Size of the image:
   - Rows (height): 369
   - Cols (width): 467
   - Bands: 12
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (-22.97848940, -43.19246031)
- Driver: GTiff
- Data type: int16
- Projection system: EPSG:32723
- Nodata: -32768.0

- Given names for spectral bands: 
   {'1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, '11': 11, '12': 12}