20 #include "cpl_string.h" 21 #include <QProgressDialog> 24 #if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1800 25 #define TO8F(x) (x).toUtf8().constData() 27 #define TO8F(x) QFile::encodeName( x ).constData() 31 : mInputFile( inputFile )
32 , mOutputFile( outputFile )
33 , mOutputFormat( outputFormat )
36 , mInputNodataValue( -1.0 )
37 , mOutputNodataValue( -1.0 )
63 GDALDatasetH inputDataset = openInputFile( xSize, ySize );
70 GDALDriverH outputDriver = openOutputDriver();
76 GDALDatasetH outputDataset = openOutputFile( inputDataset, outputDriver );
83 GDALRasterBandH rasterBand = GDALGetRasterBand( inputDataset, 1 );
86 GDALClose( inputDataset );
87 GDALClose( outputDataset );
92 GDALRasterBandH outputRasterBand = GDALGetRasterBand( outputDataset, 1 );
93 if ( !outputRasterBand )
95 GDALClose( inputDataset );
96 GDALClose( outputDataset );
100 GDALSetRasterNoDataValue( outputRasterBand, -9999 );
105 GDALClose( inputDataset );
106 GDALClose( outputDataset );
111 float* scanLine1 = (
float * ) CPLMalloc(
sizeof(
float ) * xSize );
112 float* scanLine2 = (
float * ) CPLMalloc(
sizeof(
float ) * xSize );
113 float* scanLine3 = (
float * ) CPLMalloc(
sizeof(
float ) * xSize );
115 float* resultLine = (
float * ) CPLMalloc(
sizeof(
float ) * xSize );
123 for (
int i = 0; i < ySize; ++i )
138 for (
int a = 0; a < xSize; ++a )
142 if ( GDALRasterIO( rasterBand, GF_Read, 0, 0, xSize, 1, scanLine2, xSize, 1, GDT_Float32, 0, 0 ) != CE_None )
150 CPLFree( scanLine1 );
151 scanLine1 = scanLine2;
152 scanLine2 = scanLine3;
153 scanLine3 = (
float * ) CPLMalloc(
sizeof(
float ) * xSize );
156 if ( i == ySize - 1 )
158 for (
int a = 0; a < xSize; ++a )
165 if ( GDALRasterIO( rasterBand, GF_Read, 0, i + 1, xSize, 1, scanLine3, xSize, 1, GDT_Float32, 0, 0 ) != CE_None )
171 for (
int j = 0; j < xSize; ++j )
178 else if ( j == xSize - 1 )
185 resultLine[j] =
processNineCellWindow( &scanLine1[j-1], &scanLine1[j], &scanLine1[j+1], &scanLine2[j-1], &scanLine2[j],
186 &scanLine2[j+1], &scanLine3[j-1], &scanLine3[j], &scanLine3[j+1] );
190 if ( GDALRasterIO( outputRasterBand, GF_Write, 0, i, xSize, 1, resultLine, xSize, 1, GDT_Float32, 0, 0 ) != CE_None )
201 CPLFree( resultLine );
202 CPLFree( scanLine1 );
203 CPLFree( scanLine2 );
204 CPLFree( scanLine3 );
206 GDALClose( inputDataset );
214 GDALClose( outputDataset );
219 GDALDatasetH QgsNineCellFilter::openInputFile(
int& nCellsX,
int& nCellsY )
224 nCellsX = GDALGetRasterXSize( inputDataset );
225 nCellsY = GDALGetRasterYSize( inputDataset );
228 if ( GDALGetRasterCount( inputDataset ) < 1 )
230 GDALClose( inputDataset );
237 GDALDriverH QgsNineCellFilter::openOutputDriver()
239 char **driverMetadata;
249 driverMetadata = GDALGetMetadata( outputDriver,
nullptr );
250 if ( !CSLFetchBoolean( driverMetadata, GDAL_DCAP_CREATE,
false ) )
265 int xSize = GDALGetRasterXSize( inputDataset );
266 int ySize = GDALGetRasterYSize( inputDataset );
269 char **papszOptions =
nullptr;
271 if ( !outputDataset )
273 return outputDataset;
277 double geotransform[6];
278 if ( GDALGetGeoTransform( inputDataset, geotransform ) != CE_None )
280 GDALClose( outputDataset );
283 GDALSetGeoTransform( outputDataset, geotransform );
297 const char* projection = GDALGetProjectionRef( inputDataset );
298 GDALSetProjection( outputDataset, projection );
300 return outputDataset;
virtual ~QgsNineCellFilter()
void setMaximum(int maximum)
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 setValue(int progress)
int processRaster(QProgressDialog *p)
Starts the calculation, reads from mInputFile and stores the result in mOutputFile.
QByteArray toLocal8Bit() const
float mOutputNodataValue
The nodata value of the output layer.
QgsNineCellFilter(const QString &inputFile, const QString &outputFile, const QString &outputFormat)
Constructor that takes input file, output file and output format (GDAL string)
double mZFactor
Scale factor for z-value if x-/y- units are different to z-units (111120 for degree->meters and 37040...
float mInputNodataValue
The nodata value of the input layer.