gemgis.raster.clip_by_bbox#

gemgis.raster.clip_by_bbox(raster: Union[rasterio.io.DatasetReader, numpy.ndarray], bbox: List[Union[int, float]], raster_extent: List[Union[int, float]] = None, save_clipped_raster: bool = False, path: str = 'raster_clipped.tif', overwrite_file: bool = False, create_directory: bool = False) numpy.ndarray#

Clipping a rasterio raster or np.ndarray by a given extent

Parameters
  • raster (Union[rasterio.io.DatasetReader, np.ndarray]) – Array or Rasterio object to be clipped

  • bbox (List[Union[int, float]]) – Bounding box of minx, maxx, miny, maxy values to clip the raster, e.g. bbox=[0, 972, 0, 1069]

  • raster_extent (List[Union[int, float]]) – List of float values defining the extent of the raster, default None, e.g. raster_extent=[0, 972, 0, 1069]

  • save_clipped_raster (bool) – Variable to save the raster after clipping. Options include: True or False, default set to False

  • path (str) – Path where the raster is saved, e.g. path='raster_clipped.tif'

  • overwrite_file (bool) – Variable to overwrite an already existing file. Options include: True or False, default set to False

  • create_directory (bool) – Variable to create a new directory of directory does not exist Options include: True or False, default set to False

Returns

raster_clipped – Clipped array after clipping

Return type

np.ndarray

New in version 1.0.x.

Example

>>> # Loading Libraries and File
>>> import gemgis as gg
>>> import rasterio
>>> raster = rasterio.open(fp='raster.tif')
>>> raster.read(1).shape
(275, 250)
>>> # Creating bounding box and defining raster extent
>>> bbox = [250, 500, 250, 500]
>>> raster_extent = [0, 972, 0, 1069]
>>> # Clipping raster by bounding box
>>> raster_clipped = gg.raster.clip_by_bbox(raster=raster, bbox=bbox, raster_extent=raster_extent)
>>> raster_clipped.shape
(65, 65)

See also

clip_by_polygon

Clipping raster by a Shapely Polygon