pyrsgis.convert.csv_to_raster¶
- pyrsgis.convert.csv_to_raster(csvfile, ref_raster, cols=[], stacked=True, filename=None, dtype='default', compress=None, nodata=- 9999)[source]¶
Convert a CSV file to raster
- Parameters
- csvfilestring
CSV file name. Please provide full path if file is not located in the working directory.
- ref_rasterstring
A reference raster file for target cell size, extent, projection, etc.
- colslist
The list of column names of the CSV files that should be exported. Passing a blank list will export all the columns.
- stackedboolean
Whether to stack all bands in one file or export them as separate files.
- filenamestring
The name of the output GeoTIFF file. Please note that if the ‘stacked’ argument is set to negative, the column name will be added towards the end of the output file name.
- dtypestring
The data type of the output raster. This is same as the options in the pyrsgis.raster.export module. Options are: ‘byte’, ‘cfloat32’, ‘cfloat64’, ‘cint16’, ‘cint32’, ‘float’, ‘float32’, ‘float64’, ‘int’, ‘int16’, ‘int32’, ‘uint8’, ‘uint16’, ‘uint32’.
- compressstring
Compression type of the raster. This is same as the pyrsgis.raster.export function. Options are ‘LZW’, ‘DEFLATE’ and other options that GDAL offers.
- nodatasigned number
Value to treat as NoData in the out out raster.
Examples
Let’s assume that you convert a GeoTIFF file to CSV and perform some statistical analysis.
>>> from pyrsgis import convert >>> input_file = r'E:/path_to_your_file/raster_file.tif' >>> out_csvfile = input_file.replace('.tif', '.csv') >>> convert.raster_to_csv(input_file, filename=out_csvfile, negative=False)
…create new column(s) (eg. clustering classes, predictions from a stats/ML model). And then convert the CSV to TIF file.
>>> new_csvfile = r'E:/path_to_your_file/predicted_file.tif' >>> out_tiffile = new_csvfile.replace('.csv', '.tif') >>> convert.csv_to_raster(new_csvfile, ref_raster=input_file, filename=out_tiffile, compress='DEFLATE')
This will export a GeoTIFF file. If there are multiple columns in the CSV file, the arrays will be stacked and exported as multispectral file. One can explicitly selct the columns to be exported but you should know the name of the columns beforehand.
>>> convert.csv_to_raster(new_csvfile, ref_raster=input_file, filename=out_tiffile, cols=['Blue', 'Green', 'KMeans', 'RF_Class'], compress='DEFLATE')
If you want to export each of the columns as separate bands, set the
stacked
parameter toFalse
.>>> convert.csv_to_raster(new_csvfile, ref_raster=input_file, filename=out_tiffile, cols=['Blue', 'Green', 'KMeans', 'RF_Class'], stacked=False, compress='DEFLATE')