19 #include "cpl_string.h"
20 #include <QProgressDialog>
23 #if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1800
24 #define TO8F(x) (x).toUtf8().constData()
26 #define TO8F(x) QFile::encodeName( x ).constData()
30 : mInputFile( inputFile )
31 , mOutputFile( outputFile )
32 , mOutputFormat( outputFormat )
35 , mInputNodataValue( -1.0 )
36 , mOutputNodataValue( -1.0 )
42 QgsNineCellFilter::QgsNineCellFilter()
45 , mInputNodataValue( -1.0 )
46 , mOutputNodataValue( -1.0 )
62 GDALDatasetH inputDataset = openInputFile( xSize, ySize );
63 if ( inputDataset == NULL )
69 GDALDriverH outputDriver = openOutputDriver();
70 if ( outputDriver == 0 )
75 GDALDatasetH outputDataset = openOutputFile( inputDataset, outputDriver );
76 if ( outputDataset == NULL )
82 GDALRasterBandH rasterBand = GDALGetRasterBand( inputDataset, 1 );
83 if ( rasterBand == NULL )
85 GDALClose( inputDataset );
86 GDALClose( outputDataset );
91 GDALRasterBandH outputRasterBand = GDALGetRasterBand( outputDataset, 1 );
92 if ( outputRasterBand == NULL )
94 GDALClose( inputDataset );
95 GDALClose( outputDataset );
99 GDALSetRasterNoDataValue( outputRasterBand, -9999 );
104 GDALClose( inputDataset );
105 GDALClose( outputDataset );
110 float* scanLine1 = (
float * ) CPLMalloc(
sizeof(
float ) * xSize );
111 float* scanLine2 = (
float * ) CPLMalloc(
sizeof(
float ) * xSize );
112 float* scanLine3 = (
float * ) CPLMalloc(
sizeof(
float ) * xSize );
114 float* resultLine = (
float * ) CPLMalloc(
sizeof(
float ) * xSize );
118 p->setMaximum( ySize );
122 for (
int i = 0; i < ySize; ++i )
129 if ( p && p->wasCanceled() )
137 for (
int a = 0; a < xSize; ++a )
141 GDALRasterIO( rasterBand, GF_Read, 0, 0, xSize, 1, scanLine2, xSize, 1, GDT_Float32, 0, 0 );
146 CPLFree( scanLine1 );
147 scanLine1 = scanLine2;
148 scanLine2 = scanLine3;
149 scanLine3 = (
float * ) CPLMalloc(
sizeof(
float ) * xSize );
152 if ( i == ySize - 1 )
154 for (
int a = 0; a < xSize; ++a )
161 GDALRasterIO( rasterBand, GF_Read, 0, i + 1, xSize, 1, scanLine3, xSize, 1, GDT_Float32, 0, 0 );
164 for (
int j = 0; j < xSize; ++j )
171 else if ( j == xSize - 1 )
178 resultLine[j] =
processNineCellWindow( &scanLine1[j-1], &scanLine1[j], &scanLine1[j+1], &scanLine2[j-1], &scanLine2[j],
179 &scanLine2[j+1], &scanLine3[j-1], &scanLine3[j], &scanLine3[j+1] );
183 GDALRasterIO( outputRasterBand, GF_Write, 0, i, xSize, 1, resultLine, xSize, 1, GDT_Float32, 0, 0 );
188 p->setValue( ySize );
191 CPLFree( resultLine );
192 CPLFree( scanLine1 );
193 CPLFree( scanLine2 );
194 CPLFree( scanLine3 );
196 GDALClose( inputDataset );
198 if ( p && p->wasCanceled() )
204 GDALClose( outputDataset );
209 GDALDatasetH QgsNineCellFilter::openInputFile(
int& nCellsX,
int& nCellsY )
211 GDALDatasetH inputDataset = GDALOpen(
TO8F(
mInputFile ), GA_ReadOnly );
212 if ( inputDataset != NULL )
214 nCellsX = GDALGetRasterXSize( inputDataset );
215 nCellsY = GDALGetRasterYSize( inputDataset );
218 if ( GDALGetRasterCount( inputDataset ) < 1 )
220 GDALClose( inputDataset );
227 GDALDriverH QgsNineCellFilter::openOutputDriver()
229 char **driverMetadata;
232 GDALDriverH outputDriver = GDALGetDriverByName(
mOutputFormat.toLocal8Bit().data() );
234 if ( outputDriver == NULL )
239 driverMetadata = GDALGetMetadata( outputDriver, NULL );
240 if ( !CSLFetchBoolean( driverMetadata, GDAL_DCAP_CREATE,
false ) )
248 GDALDatasetH QgsNineCellFilter::openOutputFile( GDALDatasetH inputDataset, GDALDriverH outputDriver )
250 if ( inputDataset == NULL )
255 int xSize = GDALGetRasterXSize( inputDataset );
256 int ySize = GDALGetRasterYSize( inputDataset );;
259 char **papszOptions = NULL;
260 GDALDatasetH outputDataset = GDALCreate( outputDriver,
TO8F(
mOutputFile ), xSize, ySize, 1, GDT_Float32, papszOptions );
261 if ( outputDataset == NULL )
263 return outputDataset;
267 double geotransform[6];
268 if ( GDALGetGeoTransform( inputDataset, geotransform ) != CE_None )
270 GDALClose( outputDataset );
273 GDALSetGeoTransform( outputDataset, geotransform );
287 const char* projection = GDALGetProjectionRef( inputDataset );
288 GDALSetProjection( outputDataset, projection );
290 return outputDataset;