Band¶
This module contains the classes to handle bands - that is, single layer raster images, with georeference.
The Band class represents an object in the filesystem, where the raster is not read and only the handle and metadata are stored. The BandSample contains the in-memory raster data that can be operated.
Band¶
-
class
aeronet.dataset.raster.Band(fp)[source]¶ Bases:
aeronet.dataset.raster.geoobject.GeoObjectFilesystem object Band - Rasterio DatasetReader wrapper.
The Band provides access to a georeferenced raster file placed in the filesystem. On creation the Band opens the file for reading and stores all the necessary metadata and allows to read the raster data on request.
The majority of properties are inherited from rasterio DatasetReader.
Any file format supported by GDAL drivers can be read.
Parameters: fp – full path to the raster file -
crs¶ CRS- coordindate reference system of the band; in the file
-
transform¶ Transform matrix as the affine.Affine object. This transform maps pixel row/column coordinates to coordinates in the dataset’s coordinate reference system.
affine.identity is returned if if the file does not contain transform
-
nodata¶ Band nodata value, type depends on the image dtype; None if the nodata value is not specified
-
res¶ Spatial resolution (x_res, y_res) of the Band in X and Y directions of the georeferenced coordinate system, derived from tranaform. Normally is equal to (transform.a, - transform.e)
-
width¶ Width of the raster data object in pixels
-
height¶ Height of the raster data object in pixels
-
count¶ By design of the aeronetlib, should be always 1. A Band can be created from image of any channel count, but only the first band can be read. If you need to work with multi-channel image, use
aeronet.converters.split.split()to get the one-channel images.Returns: (int) number of the bands in the image.
-
shape¶ The raster dimension as a Tuple (height, width)
-
name¶ Name of the file associated with the Band, without extension and the directory path
-
bounds¶ Georeferenced bounds - bounding box in the CRS of the image, based on transform and shape
Returns: BoundingBox object: (left, bottom, right, top)
-
meta¶ The basic metadata of the associated rasterio DatasetReader
-
dtype¶ Numerical type of the data stored in raster, according to numpy.dtype
-
numpy()[source]¶ Read all the raster data into memory as a numpy array
Returns: numpy array containing the whole Band raster data
-
same(other)[source]¶ Compare if samples have same resolution, crs and shape.
This means that the samples represent the same territory (like different spectral channels of the same image) and can be processed together as collection.
Parameters: other – GeoObject to compare with Returns: True if the objects match in shape, crs, transform, False otherwise
-
sample(y, x, height, width, **kwargs)[source]¶ Read sample of the Band to memory.
The sample is defined by its size and position in the raster, without respect to the georeference. In case if the sample coordinates spread out of the image boundaries, the image is padded with nodata value.
Parameters: - x – pixel horizontal coordinate of left top corner of the sample
- y – pixel vertical coordinate of left top corner of the sample
- width – spatial dimension of sample in pixels
- height – spatial dimension of sample in pixels
Returns: a new
BandSamplecontaining the specified spatial subset of the band
-
resample(dst_res, fp=None, interpolation='nearest')[source]¶ Change spatial resolution of the band. It does not alter the existing file, and creates a new file either in the specified location or a temporary file
It is based on rasterio.warp.reproject, see for more variants of interpolation.
Parameters: - dst_res (Tuple[float, float]) – new resoluton, georeferenced pixel size for the new band
- fp (str) – a filename for the new resampled band. If none, a temporary file is created
- interpolation – interpolation type as in rasterio, nearest, bilinear, cubic, lanzsos or others
Returns: a new resampled Band.
-
reproject(dst_crs, fp=None, interpolation='nearest')[source]¶ Change coordinate system (projection) of the band. It does not alter the existing file, and creates a new file either in the specified location or a temporary file.
The band ground sampling distance is not changed, however the resolution may change due to the new coordinate system It is based on rasterio.warp.reproject, see for more variants of interpolation.
Parameters: - dst_crs – new CRS, may be in any form acceptable by rasterio, for example as EPSG code, string, CRS object; if dst_crs == utm, the appropriate UTM zone is used according to the center of the image
- fp (str) – a filename for the new resampled band. If none, a temporary file is created
- interpolation – interpolation type as in rasterio, nearest, bilinear, cubic, lanzsos or others
Returns: a new reprojected Band
-
reproject_to(other: aeronet.dataset.raster.geoobject.GeoObject, fp=None, interpolation='nearest')[source]¶ Reprojects and resamples the band to match exactly the other.
This function ensures that the raster size, crs and transform will be the same, allowing them to be merged into one BandCollection. If the intial raster exceeds the other in coverage, it will be cut, and if it is insufficient or displaced, it will be zero-padded.
It aims to overpass the rounding problem which may cause an image to be misaligned with itself after a different series of transforms.
If the images are far from each other, the warning will be shown, because the raster may be zero due to severe misalignment.
Parameters: - other (GeoObject) – the Band with the parameters to fit to
- fp (str) – a filename for the new resampled band. If none, a temporary file is created
- interpolation – interpolation type as in rasterio, nearest, bilinear, cubic, lanzsos or others.
Returns: a new reprojected and resampled Band
-
reproject_to_utm(fp=None, interpolation='nearest')[source]¶ Alias of
Band.reprojectmethod with automatic Band utm zone determining
-
generate_samples(width, height)[source]¶ A generator for sequential sampling of the whole band, used for the windowed reading of the raster. It allows to handle and process large files without reading them at once in the memory.
Parameters: - width (int) – dimension of sample in pixels and step along X axis
- height (int) – dimension of sample in pixels and step along Y axis
Yields: BandSample – sequential samples of the specified dimensions
-
profile¶ A joint representation of the main properties
Returns: { ‘crs’: crs, ‘nodata’: nodata, ‘transform’: transform } Return type: Dict
-
BandSample¶
-
class
aeronet.dataset.raster.BandSample(name, raster, crs, transform, nodata=0)[source]¶ Bases:
aeronet.dataset.raster.geoobject.GeoObjectA wrapper over numpy array representing an in-memory georeferenced raster image.
It implements all the interfaces of the GeoObject, and stores the raster data in memory
Parameters: - name (str) – a name of the sample, which is used as a defaule name for saving to file
- raster (np.array) – the raster data
- crs – geographical coordinate reference system, as
CRSor string representation - transform (Affine) – affine transform for the
- nodata – the pixels with this value in raster should be ignored
-
width¶ Width of the raster data object in pixels
-
height¶ Height of the raster data object in pixels
-
count¶ (int) number of channels/bands.
-
shape¶ The raster dimension as a Tuple (height, width)
-
dtype¶ Data type of the associated numpy array
-
res¶ Resolution (or ground sampling distance) along X and Y axes in units of the CRS. Tuple (x_resolution, y_resolution)
-
transform¶ Transform matrix as the affine.Affine object. This transform maps pixel row/column coordinates to coordinates in the dataset’s coordinate reference system.
-
crs¶ Geographic coordinate reference system of the object Returns a rasterio.CRS
-
nodata¶ The value that should be interpreted as ‘No data’. May be None or a value within dtype range
-
bounds¶ Georeferenced bounds - bounding box in the CRS of the image, based on transform and shape
Returns: BoundingBox object: (left, bottom, right, top)
-
name¶ name of the sample, is used as a base filename when saving to file
-
classmethod
from_file(fp)[source]¶ Reads the raster data directly from the file. File must have only one channel. If you need to read multi-channel file, use
aeronet.converters.split.split()firstParameters: fp – full path to the file Returns: a new BandSample object
-
same(other)[source]¶ Compare if samples have same resolution, crs and shape.
This means that the samples represent the same territory (like different spectral channels of the same image) and can be processed together as collection.
Parameters: other – GeoObject to compare with Returns: True if the objects match in shape, crs, transform, False otherwise
-
save(directory, ext='.tif', **kwargs)[source]¶ Saves the raster data to a new geotiff file; the filename is derived from this BandSample name. If file exists, it will be overwritten.
Parameters: - directory – folder to save the file
- ext – file extension; as now only GTiff driver is used, it should match tif, tiff, TIF or TIFF.
- kwargs – other keywords arguments to be passed to rasterio.open .
-
sample(y, x, height, width)[source]¶ Subsample of the Sample with specified dimensions and position within the raster:
Parameters: - x (int) – horizontal pixel coordinate of left top corner
- y (int) – vertical pixel coordinate of left top corner
- width (int) – spatial x-dimension of sample in pixels
- height (int) – spatial y-dimension of sample in pixels
Returns: a new BandSample object
-
reproject(dst_crs, interpolation='nearest')[source]¶ Change coordinate system (projection) of the band. It returns a new BandSample and does not alter the current object
It is based on rasterio.warp.reproject, see for more variants of interpolation.
Parameters: - dst_crs – new CRS, may be in any form acceptable by rasterio, for example as EPSG code, string, CRS object; if dst_crs == utm, the appropriate UTM zone is used according to the center of the image
- interpolation – interpolation type as in rasterio, nearest, bilinear, cubic, lanzsos or others
Returns: a new instance with changed CRS.
Return type:
-
reproject_to_utm(interpolation='nearest')[source]¶ Alias of
BandSample.reprojectmethod with automatic Band utm zone determining
-
resample(dst_res=None, dst_shape=None, interpolation='nearest')[source]¶ Change spatial resolution of the sample, resizing the raster according to the new resolution. dst_res should be specified, otherwise the destination transform will be equal to the source. If dst_shape is not specified, it is calculated from dst_res, but it can be specified to override it and get the desired output shape
It is based on rasterio.warp.reproject, see for more variants of interpolation.
Parameters: - dst_res (Tuple[float, float]) – new resoluton, georeferenced pixel size for the new band
- dst_shape – new shape of the resampled raster, can override calculated new shape
- interpolation – interpolation type as in rasterio, nearest, bilinear, cubic, lanzsos or others
Returns: a new resampled BandSample.
-
numpy()[source]¶ A numpy representation of the raster, without metadata
Returns: Sample’s raster data as a numpy array
-
generate_samples(width, height)[source]¶ A generator for sequential sampling of the whole sample, similar to Band, used for the windowed processing of the raster data.
Parameters: - width (int) – dimension of sample in pixels and step along X axis
- height (int) – dimension of sample in pixels and step along Y axis
Yields: BandSample – sequential samples of the specified dimensions
-
profile¶ A joint representation of the main properties
Returns: { ‘crs’: crs, ‘nodata’: nodata, ‘transform’: transform } Return type: Dict