gemgis.raster.merge_tiles#

gemgis.raster.merge_tiles(src_files: List[rasterio.io.DatasetReader], extent: List[Union[int, float]] = None, res: int = None, nodata: Union[float, int] = None, precision: int = None, indices: int = None, method: str = 'first') Tuple[numpy.ndarray, affine.Affine]#

Merging downloaded tiles to mosaic

Parameters
  • src_files (List[rasterio.io.DatasetReader]) – List of rasterio datasets to be merged

  • extent (List[Union[float, int]]) – Bounds of the output image (left, bottom, right, top). If not set, bounds are determined from bounds of input rasters, e.g. extent=[0, 972, 0, 1069], default is None

  • res (int) – Output resolution in units of coordinate reference system. If not set, the resolution of the first raster is used. If a single value is passed, output pixels will be square. e.g. res=50, default is None

  • nodata (Union[float, int]) – nodata value to use in output file. If not set, uses the nodata value in the first input raster, e.g. nodata=9999.0, default is None

  • precision (int) – Number of decimal points of precision when computing inverse transform, e.g. precision=2, default is None

  • indices (int) – Bands to read and merge, e.g. indices=1, default is None

  • method (str) – Method on how to merge the tiles, e.g. method='first', default is 'first'

Returns

  • mosaic (np.ndarray) – Array containing the merged tile data

  • transform (affine.Affine) – Affine Transform of the merged tiles

New in version 1.0.x.

Example

>>> # Loading Libraries
>>> import gemgis as gg
>>> # Creating filepath
>>> filepath = 'Documents/images/'
>>> # Creating list of filepaths
>>> filepaths = gg.raster.create_filepaths(dirpath=filepath, search_criteria='tile*.tif')
>>> filepaths
['Documents/images//tile_292000_294000_5626000_5628000.tif',
'Documents/images//tile_292000_294000_5628000_5630000.tif',
'Documents/images//tile_292000_294000_5630000_5632000.tif',
'Documents/images//tile_294000_296000_5626000_5628000.tif']
>>> # Creating list of loaded rasterio objects
>>> src_list = gg.raster.create_src_list(filepaths=filepaths)
>>> src_list
[<open DatasetReader name='Documents/images/tile_292000_294000_5626000_5628000.tif' mode='r'>,
<open DatasetReader name='Documents/images/tile_292000_294000_5628000_5630000.tif' mode='r'>,
<open DatasetReader name='Documents/images/tile_292000_294000_5630000_5632000.tif' mode='r'>,
<open DatasetReader name='Documents/images/tile_294000_296000_5626000_5628000.tif' mode='r'>,
>>> # Merging tiles
>>> mosaic, transform = gg.raster.merge_tiles(src_files=src_list)
>>> # Inspecting the mosaic data
>>> mosaic
array([[200.72, 200.73, 200.72, ..., 204.42, 204.45, 204.45],
[200.74, 200.74, 200.75, ..., 204.43, 204.44, 204.48]
[200.76, 200.76, 200.76, ..., 204.42, 204.48, 204.5 ],
...,
[329.15, 328.86, 328.74, ..., 242.45, 242.38, 242.28],
[329.29, 329.06, 328.87, ..., 242.45, 242.39, 242.31],
[329.47, 329.3 , 329.09, ..., 242.42, 242.37, 242.32]],
dtype=float32)
>>> # Inspecting the transform of the mosaic
>>> transform
Affine(1.0, 0.0, 292000.0,
0.0, -1.0, 5632000.0)