pyrsgis.raster.read

pyrsgis.raster.read(file, bands='all')[source]

Read raster file

The function reads the raster file and generates two object. The first one is a datasource object and the second is a numpy array that contains cell values of the bands.

Parameters
filedatasource object

Path to the input file.

bandsinteger, tuple, list or ‘all’

Bands to read. This can either be a specific band number you wish to read or a list or tuple of band numbers or ‘all’.

Returns
datasourcedatasource object

A data source object that contains the metadata of the raster file. Information such as the number of rows and columns, number of bands, data type of raster, projection, cellsize, geographic extent etc. are stored in this datasource object.

data_arraynumpy array

A 2D or 3D numpy array that contains the DN values of the raster file. If the input file is a single band raster, the function returns a 2D array. Whereas, if the input file is a multiband raster, this function returns a 3D array.

Examples

>>> from pyrsgis import raster
>>> input_file = r'E:/path_to_your_file/raster_file.tif'
>>> ds, data_arr = raster.read(input_file)

The ‘data_arr’ is a numpy array. The ‘ds’ is the datasource object that contains details about the raster file.

>>> print(ds.RasterXSize, ds.RasterYSize)

The output from the above line will be equal to the followng line:

>>> print(arr.shape)

Please note that if the input file is a multispectral raster, the ‘arr.shape’ command will result a tuple of size three, where the number of bands will be at the first index. For multispectral input files, the ‘arr.shape’ will be equal to the following line:

>>> print(ds.RasterCount, ds.RasterXSize, ds.RasterYSize)
Attributes
RasterCountinteger

The total number of bands in the input file.

RasterXSizeinteger

Height of the raster represented as the number of rows.

RasterYSizeinteger

Width of the raster represented as the number of columns.

Projectionprojection object

The projection of the input file.

GeoTransformgeotransform object

This is the Geotransform tuple of the input file. The tuple can be converted to list and updated to change various properties of the raster file.