20 #include "cpl_string.h" 26 : mInputFile( inputFile )
27 , mOutputFile( outputFile )
28 , mOutputFormat( outputFormat )
46 GDALDriverH outputDriver = openOutputDriver();
59 GDALRasterBandH rasterBand = GDALGetRasterBand( inputDataset.get(), 1 );
66 GDALRasterBandH outputRasterBand = GDALGetRasterBand( outputDataset.get(), 1 );
67 if ( !outputRasterBand )
72 GDALSetRasterNoDataValue( outputRasterBand, -9999 );
81 float *scanLine1 = (
float * ) CPLMalloc(
sizeof(
float ) * xSize );
82 float *scanLine2 = (
float * ) CPLMalloc(
sizeof(
float ) * xSize );
83 float *scanLine3 = (
float * ) CPLMalloc(
sizeof(
float ) * xSize );
85 float *resultLine = (
float * ) CPLMalloc(
sizeof(
float ) * xSize );
88 for (
int i = 0; i < ySize; ++i )
97 feedback->
setProgress( 100.0 * static_cast< double >( i ) / ySize );
103 for (
int a = 0; a < xSize; ++a )
107 if ( GDALRasterIO( rasterBand, GF_Read, 0, 0, xSize, 1, scanLine2, xSize, 1, GDT_Float32, 0, 0 ) != CE_None )
115 CPLFree( scanLine1 );
116 scanLine1 = scanLine2;
117 scanLine2 = scanLine3;
118 scanLine3 = (
float * ) CPLMalloc(
sizeof(
float ) * xSize );
121 if ( i == ySize - 1 )
123 for (
int a = 0; a < xSize; ++a )
130 if ( GDALRasterIO( rasterBand, GF_Read, 0, i + 1, xSize, 1, scanLine3, xSize, 1, GDT_Float32, 0, 0 ) != CE_None )
136 for (
int j = 0; j < xSize; ++j )
143 else if ( j == xSize - 1 )
150 resultLine[j] =
processNineCellWindow( &scanLine1[j - 1], &scanLine1[j], &scanLine1[j + 1], &scanLine2[j - 1], &scanLine2[j],
151 &scanLine2[j + 1], &scanLine3[j - 1], &scanLine3[j], &scanLine3[j + 1] );
155 if ( GDALRasterIO( outputRasterBand, GF_Write, 0, i, xSize, 1, resultLine, xSize, 1, GDT_Float32, 0, 0 ) != CE_None )
161 CPLFree( resultLine );
162 CPLFree( scanLine1 );
163 CPLFree( scanLine2 );
164 CPLFree( scanLine3 );
180 nCellsX = GDALGetRasterXSize( inputDataset.get() );
181 nCellsY = GDALGetRasterYSize( inputDataset.get() );
184 if ( GDALGetRasterCount( inputDataset.get() ) < 1 )
192 GDALDriverH QgsNineCellFilter::openOutputDriver()
194 char **driverMetadata =
nullptr;
197 GDALDriverH outputDriver = GDALGetDriverByName(
mOutputFormat.toLocal8Bit().data() );
204 driverMetadata = GDALGetMetadata( outputDriver,
nullptr );
205 if ( !CSLFetchBoolean( driverMetadata, GDAL_DCAP_CREATE,
false ) )
220 int xSize = GDALGetRasterXSize( inputDataset );
221 int ySize = GDALGetRasterYSize( inputDataset );
224 char **papszOptions =
nullptr;
226 if ( !outputDataset )
228 return outputDataset;
232 double geotransform[6];
233 if ( GDALGetGeoTransform( inputDataset, geotransform ) != CE_None )
237 GDALSetGeoTransform( outputDataset.get(), geotransform );
241 if ( mCellSizeX < 0 )
251 const char *projection = GDALGetProjectionRef( inputDataset );
252 GDALSetProjection( outputDataset.get(), projection );
254 return outputDataset;
void setProgress(double progress)
Sets the current progress for the feedback object.
int processRaster(QgsFeedback *feedback=nullptr)
Starts the calculation, reads from mInputFile and stores the result in mOutputFile.
Base class for feedback objects to be used for cancelation of something running in a worker thread...
virtual float processNineCellWindow(float *x11, float *x21, float *x31, float *x12, float *x22, float *x32, float *x13, float *x23, float *x33)=0
Calculates output value from nine input values.
void CORE_EXPORT fast_delete_and_close(dataset_unique_ptr &dataset, GDALDriverH driver, const QString &path)
Performs a fast close of an unwanted GDAL dataset handle by deleting the underlying data store...
float mOutputNodataValue
The nodata value of the output layer.
bool isCanceled() const
Tells whether the operation has been canceled already.
QgsNineCellFilter(const QString &inputFile, const QString &outputFile, const QString &outputFormat)
Constructor that takes input file, output file and output format (GDAL string)
float mInputNodataValue
The nodata value of the input layer.
std::unique_ptr< std::remove_pointer< GDALDatasetH >::type, GDALDatasetCloser > dataset_unique_ptr
Scoped GDAL dataset.