pyrsgis.raster.export

pyrsgis.raster.export(arr, ds, filename='pyrsgis_outFile.tif', dtype='default', bands='all', nodata=- 9999, compress=None)[source]

Export GeoTIFF file

This function exports the GeoTIF file using a data source object and an array containing cell values.

Parameters
arrarray

A numpy array containing the cell values of to-be exported raster. This can either be a 2D or a 3D array. Please note that the channel index should be in the beginning.

dsdatasource object

The datasource object of a target reference raster.

filenamestring

Output file name ending with ‘.tif’. This can include relative path or full path of the file.

dtypestring

The data type of the raster to be exported. It can take one of these values, ‘byte’, ‘cfloat32’, ‘cfloat64’, ‘cint16’, ‘cint32’, ‘float’, ‘float32’, ‘float64’, ‘int’, ‘int16’, ‘int32’, ‘uint8’, ‘uint16’, ‘uint32’ or ‘default’. The ‘default’ type value will take the data type from the given datasource object. It is advised to use a lower depth data value, this helps in reducing the file size on the disk.

bandsinteger, list, tuple or ‘all’

The band(s) you want to export. Please note that the band number here is actual position on band instead of Python’s default index number. That is, the first band should be referred as 1. The ‘bands’ parameter defaults to 1 and should only be tweaked when the data array to be exported is a 3D array. If not specified, only the first band of the 3D array will be exported.

nodatasigned integer

The value that you want to tret as NULL or NoData in you output raster.

compressstring

Compression type of your output raster. Options are ‘LZW’, ‘DEFLATE’ and other methods that GDAL offers. Compressing the data can save a lot of disk space without losing data. Some methods, for instance ‘LZW’ can reduce the size of the raster from more than a GB to less than 20 MB.

Examples

>>> from pyrsgis import raster
>>> input_file = r'E:/path_to_your_file/landsat8_multispectral.tif'
>>> ds, data_arr = raster.read(input_file)
>>> red_arr = data_arr[3, :, :]
>>> nir_arr = data_arr[4, :, :]
>>> ndvi_arr = (nir_arr - red_arr) / (nir_arr + red_arr)

Or directly in one go:

>>> ndvi_arr = (data_arr[4, :, :] - data_arr[3, :, :]) / (data_arr[4, :, :] + data_arr[3, :, :])

And then export:

>>> output_file = r'E:/path_to_your_file/landsat8_ndvi.tif'
>>> raster.export(ndvi_arr, ds, output_file, dtype='float32')

Note that the ‘dtype’ parameter here is explicitly defined as ‘float32’ since NDVI is a continuous data.