pyrsgis.convert.raster_to_csv

pyrsgis.convert.raster_to_csv(path, filename='pyrsgis_rastertocsv.csv', negative=True, remove=[], badrows=True)[source]

Convert raster to a tabular CSV file

This function converts a single or multiband raster or rasters present in a given directory to a CSV file. Each row in the output CSV file represents a cell and columns represent band(s) of the input raster(s).

Parameters
pathstring

Path to a file or a directory containing raster file(s).

filenamestring

Output CSV file name, with or without path.

negativeboolean

Whether to retain negative values or not. If False, all negative values will be forced to zero in the output CSV. This maybe useful in some cases as NoData cells in raster files are often negative.

removelist

A list of values that you want to remove from the exported table. If a list is passed, all the values of the list will be converted to zero in the raster before transforming to table. Please note that in the backend, this step happens before bad rows removal.

badrowsTrue

Whether to retain rows in the CSV where all cells have zero value. This can be helpful since raster layers masked using a non-rectangular polygon may have unnecessary NoData cells. In such cases, if all the bands have a cell value of zero and are not relevant, this parameter can help in reducing the size of the data. Please note that cells converted to zero by passing the ‘negative’ and ‘remove’ arguments will also be considered as bad cells.

Examples

>>> from pyrsgis import convert

If you want to convert a single raster file (single or multiple bands):

>>> input_file = r'E:/path_to_your_file/raster_file.tif'
>>> output_file = r'E:/path_to_your_file/tabular_file.csv'
>>> convert.raster_to_csv(input_file, filename=output_file)

If you want to convert all files in a directory, please ensure that all rasters in the directory have the same extent, cell size and geometry. The files in the directory can be a mix of single and multiband rasters.

>>> input_dir = r'E:/path_to_your_file/'
>>> output_file = r'E:/path_to_your_file/tabular_file.csv'
>>> convert.raster_to_csv(input_dir, filename=output_file)

If you want to remove negative values, simply pass the ‘negative’ argument to False:

>>> convert.raster_to_csv(input_dir, filename=output_file, negative=False)

If you want to remove specific values, use this:

>>> convert.raster_to_csv(input_dir, filename=output_file, remove=[10, 54, 127])

If you want to remove bad rows, use the following line:

>>> convert.raster_to_csv(input_dir, filename=output_file, badrows=False)