import rastereasy

Removing some bands

Two options

  • Return an image with removed bands (function remove_bands with default options)

  • Remove bands from image directly (function remove_bands with inplace=True option)

name_im='./data/demo/sentinel.tif'
image=rastereasy.Geoimage(name_im)
image.info()
- Size of the image:
   - Rows (height): 1000
   - Cols (width): 1000
   - Bands: 12
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (7.04099599, 38.39058840)
- Driver: GTiff
- Data type: int16
- Projection system: EPSG:32637
- 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}
help(image.remove_bands)
Help on method remove_bands in module rastereasy.rastereasy:

remove_bands(bands, inplace=False, reformat_names=False, dest_name=None) method of rastereasy.rastereasy.Geoimage instance
    Remove specified bands from the image.
    
    This method modifies the current image by removing the specified bands.
    The remaining bands can be renamed sequentially or retain their original names.
    
    Parameters
    ----------
    bands : str, list, int, or array-like
        The bands to remove from the image. Format depends on band naming:
        - If using named bands: band name(s) as string(s) (e.g., 'NIR', ['R', 'G', 'B'])
        - If using indexed bands: band index/indices as int(s) or string(s) (e.g., 3, ['1', '4', '7'])
    
    inplace : bool, default False
        If False, return a copy. Otherwise, do removing in place and return None.
    
    
    reformat_names : bool, optional
        Band naming behavior after removal:
        - If True: Rename remaining bands sequentially as "1", "2", "3", etc.
        - If False: Preserve original band names with their indices updated
        Default is False.
    
    dest_name : str, optional
        Path to save the modified image. If None, the image is not saved.
        Default is None.
    
    Returns
    -------
    Geoimage
        The image with specified bands removed or None if `inplace=True`
    
    Raises
    ------
    ValueError
        If any specified band doesn't exist in the image, or if removing all bands.
    
    Examples
    --------
    >>> # Remove a single band
    >>> original_bands = list(image.names.keys())
    >>> image_removed = image.remove_bands('B4')
    >>> print(f"Original: {original_bands}, After removal: {list(image_removed.names.keys())}")
    >>>
    >>> # Remove multiple bands and rename sequentially
    >>> image_removed = image.remove_bands(['B1', 'B2'], reformat_names=True)
    >>> print(f"After renaming: {list(image_removed = .names.keys())}")
    >>>
    >>> # Remove bands and save the result
    >>> image_removed = image.remove_bands(['SWIR1', 'SWIR2'], dest_name='visible_only.tif')
    >>>
    >>> # Remove a single band
    >>> original_bands = list(image.names.keys())
    >>> image.remove_bands('B4', inplace=True)
    >>> print(f"Original: {original_bands}, After removal: {list(image.names.keys())}")
    >>>
    >>> # Remove multiple bands and rename sequentially
    >>> image.remove_bands(['B1', 'B2'], reformat_names=True, inplace=True)
    >>> print(f"After renaming: {list(image.names.keys())}")
    >>>
    >>> # Remove bands and save the result
    >>> image.remove_bands(['SWIR1', 'SWIR2'], dest_name='visible_only.tif', inplace=True)
    
    Notes
    -----
    - If reformat_names=False (default), band names are preserved but indices are updated.
    - If reformat_names=True, bands are renamed sequentially (1, 2, 3, ...).

1) Return an image with removed bands (function remove_bands)

image_removed=image.remove_bands([10,8,9])
image_removed.info()
- Size of the image:
   - Rows (height): 1000
   - Cols (width): 1000
   - Bands: 9
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (7.04099599, 38.39058840)
- Driver: GTiff
- Data type: int16
- Projection system: EPSG:32637
- Nodata: -32768.0

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

As you can see, it can be interesting to reformat bands names (depending of application). In this case, use reformat_names=True

image_removed=image.remove_bands([10,8,9],reformat_names=True)
image_removed.info()
- Size of the image:
   - Rows (height): 1000
   - Cols (width): 1000
   - Bands: 9
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (7.04099599, 38.39058840)
- Driver: GTiff
- Data type: int16
- Projection system: EPSG:32637
- 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}

In this case, it can be easier to deal with real names

names = {"CO" : 1,"B": 2,"G":3,"R":4,"RE1":5,"RE2":6,"RE3":7,"NIR":8,"WA":9,"SWIR1":10,"SWIR2":11,"SWIR3":12}
image_names=rastereasy.Geoimage(name_im,names=names)
image_removed=image_names.remove_bands(['SWIR1','NIR','WA'])
image_removed.info()
- Size of the image:
   - Rows (height): 1000
   - Cols (width): 1000
   - Bands: 9
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (7.04099599, 38.39058840)
- Driver: GTiff
- Data type: int16
- Projection system: EPSG:32637
- Nodata: -32768.0

- Given names for spectral bands: 
   {'CO': 1, 'B': 2, 'G': 3, 'R': 4, 'RE1': 5, 'RE2': 6, 'RE3': 7, 'SWIR2': 8, 'SWIR3': 9}

2) Remove bands from image directly (option inplace=True)

image_names.info()
image_names.remove_bands(['SWIR1','NIR','WA'], inplace=True)
image_names.info()
- Size of the image:
   - Rows (height): 1000
   - Cols (width): 1000
   - Bands: 12
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (7.04099599, 38.39058840)
- Driver: GTiff
- Data type: int16
- Projection system: EPSG:32637
- Nodata: -32768.0

- Given names for spectral bands: 
   {'CO': 1, 'B': 2, 'G': 3, 'R': 4, 'RE1': 5, 'RE2': 6, 'RE3': 7, 'NIR': 8, 'WA': 9, 'SWIR1': 10, 'SWIR2': 11, 'SWIR3': 12}


- Size of the image:
   - Rows (height): 1000
   - Cols (width): 1000
   - Bands: 9
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (7.04099599, 38.39058840)
- Driver: GTiff
- Data type: int16
- Projection system: EPSG:32637
- Nodata: -32768.0

- Given names for spectral bands: 
   {'CO': 1, 'B': 2, 'G': 3, 'R': 4, 'RE1': 5, 'RE2': 6, 'RE3': 7, 'SWIR2': 8, 'SWIR3': 9}