QGIS API Documentation  2.99.0-Master (23ddace)
qgsninecellfilter.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsninecellfilter.h - description
3  -------------------
4  begin : August 6th, 2009
5  copyright : (C) 2009 by Marco Hugentobler
6  email : marco dot hugentobler at karto dot baug dot ethz dot ch
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgsninecellfilter.h"
19 #include "qgslogger.h"
20 #include "cpl_string.h"
21 #include <QProgressDialog>
22 #include <QFile>
23 
24 QgsNineCellFilter::QgsNineCellFilter( const QString &inputFile, const QString &outputFile, const QString &outputFormat )
25  : mInputFile( inputFile )
26  , mOutputFile( outputFile )
27  , mOutputFormat( outputFormat )
28  , mCellSizeX( -1.0 )
29  , mCellSizeY( -1.0 )
30  , mInputNodataValue( -1.0 )
31  , mOutputNodataValue( -1.0 )
32  , mZFactor( 1.0 )
33 {
34 
35 }
36 
38  : mCellSizeX( -1.0 )
39  , mCellSizeY( -1.0 )
40  , mInputNodataValue( -1.0 )
41  , mOutputNodataValue( -1.0 )
42  , mZFactor( 1.0 )
43 {
44 }
45 
46 int QgsNineCellFilter::processRaster( QProgressDialog *p )
47 {
48  GDALAllRegister();
49 
50  //open input file
51  int xSize, ySize;
52  GDALDatasetH inputDataset = openInputFile( xSize, ySize );
53  if ( !inputDataset )
54  {
55  return 1; //opening of input file failed
56  }
57 
58  //output driver
59  GDALDriverH outputDriver = openOutputDriver();
60  if ( !outputDriver )
61  {
62  return 2;
63  }
64 
65  GDALDatasetH outputDataset = openOutputFile( inputDataset, outputDriver );
66  if ( !outputDataset )
67  {
68  return 3; //create operation on output file failed
69  }
70 
71  //open first raster band for reading (operation is only for single band raster)
72  GDALRasterBandH rasterBand = GDALGetRasterBand( inputDataset, 1 );
73  if ( !rasterBand )
74  {
75  GDALClose( inputDataset );
76  GDALClose( outputDataset );
77  return 4;
78  }
79  mInputNodataValue = GDALGetRasterNoDataValue( rasterBand, nullptr );
80 
81  GDALRasterBandH outputRasterBand = GDALGetRasterBand( outputDataset, 1 );
82  if ( !outputRasterBand )
83  {
84  GDALClose( inputDataset );
85  GDALClose( outputDataset );
86  return 5;
87  }
88  //try to set -9999 as nodata value
89  GDALSetRasterNoDataValue( outputRasterBand, -9999 );
90  mOutputNodataValue = GDALGetRasterNoDataValue( outputRasterBand, nullptr );
91 
92  if ( ySize < 3 ) //we require at least three rows (should be true for most datasets)
93  {
94  GDALClose( inputDataset );
95  GDALClose( outputDataset );
96  return 6;
97  }
98 
99  //keep only three scanlines in memory at a time
100  float *scanLine1 = ( float * ) CPLMalloc( sizeof( float ) * xSize );
101  float *scanLine2 = ( float * ) CPLMalloc( sizeof( float ) * xSize );
102  float *scanLine3 = ( float * ) CPLMalloc( sizeof( float ) * xSize );
103 
104  float *resultLine = ( float * ) CPLMalloc( sizeof( float ) * xSize );
105 
106  if ( p )
107  {
108  p->setMaximum( ySize );
109  }
110 
111  //values outside the layer extent (if the 3x3 window is on the border) are sent to the processing method as (input) nodata values
112  for ( int i = 0; i < ySize; ++i )
113  {
114  if ( p )
115  {
116  p->setValue( i );
117  }
118 
119  if ( p && p->wasCanceled() )
120  {
121  break;
122  }
123 
124  if ( i == 0 )
125  {
126  //fill scanline 1 with (input) nodata for the values above the first row and feed scanline2 with the first row
127  for ( int a = 0; a < xSize; ++a )
128  {
129  scanLine1[a] = mInputNodataValue;
130  }
131  if ( GDALRasterIO( rasterBand, GF_Read, 0, 0, xSize, 1, scanLine2, xSize, 1, GDT_Float32, 0, 0 ) != CE_None )
132  {
133  QgsDebugMsg( "Raster IO Error" );
134  }
135  }
136  else
137  {
138  //normally fetch only scanLine3 and release scanline 1 if we move forward one row
139  CPLFree( scanLine1 );
140  scanLine1 = scanLine2;
141  scanLine2 = scanLine3;
142  scanLine3 = ( float * ) CPLMalloc( sizeof( float ) * xSize );
143  }
144 
145  if ( i == ySize - 1 ) //fill the row below the bottom with nodata values
146  {
147  for ( int a = 0; a < xSize; ++a )
148  {
149  scanLine3[a] = mInputNodataValue;
150  }
151  }
152  else
153  {
154  if ( GDALRasterIO( rasterBand, GF_Read, 0, i + 1, xSize, 1, scanLine3, xSize, 1, GDT_Float32, 0, 0 ) != CE_None )
155  {
156  QgsDebugMsg( "Raster IO Error" );
157  }
158  }
159 
160  for ( int j = 0; j < xSize; ++j )
161  {
162  if ( j == 0 )
163  {
164  resultLine[j] = processNineCellWindow( &mInputNodataValue, &scanLine1[j], &scanLine1[j + 1], &mInputNodataValue, &scanLine2[j],
165  &scanLine2[j + 1], &mInputNodataValue, &scanLine3[j], &scanLine3[j + 1] );
166  }
167  else if ( j == xSize - 1 )
168  {
169  resultLine[j] = processNineCellWindow( &scanLine1[j - 1], &scanLine1[j], &mInputNodataValue, &scanLine2[j - 1], &scanLine2[j],
170  &mInputNodataValue, &scanLine3[j - 1], &scanLine3[j], &mInputNodataValue );
171  }
172  else
173  {
174  resultLine[j] = processNineCellWindow( &scanLine1[j - 1], &scanLine1[j], &scanLine1[j + 1], &scanLine2[j - 1], &scanLine2[j],
175  &scanLine2[j + 1], &scanLine3[j - 1], &scanLine3[j], &scanLine3[j + 1] );
176  }
177  }
178 
179  if ( GDALRasterIO( outputRasterBand, GF_Write, 0, i, xSize, 1, resultLine, xSize, 1, GDT_Float32, 0, 0 ) != CE_None )
180  {
181  QgsDebugMsg( "Raster IO Error" );
182  }
183  }
184 
185  if ( p )
186  {
187  p->setValue( ySize );
188  }
189 
190  CPLFree( resultLine );
191  CPLFree( scanLine1 );
192  CPLFree( scanLine2 );
193  CPLFree( scanLine3 );
194 
195  GDALClose( inputDataset );
196 
197  if ( p && p->wasCanceled() )
198  {
199  //delete the dataset without closing (because it is faster)
200  GDALDeleteDataset( outputDriver, mOutputFile.toUtf8().constData() );
201  return 7;
202  }
203  GDALClose( outputDataset );
204 
205  return 0;
206 }
207 
208 GDALDatasetH QgsNineCellFilter::openInputFile( int &nCellsX, int &nCellsY )
209 {
210  GDALDatasetH inputDataset = GDALOpen( mInputFile.toUtf8().constData(), GA_ReadOnly );
211  if ( inputDataset )
212  {
213  nCellsX = GDALGetRasterXSize( inputDataset );
214  nCellsY = GDALGetRasterYSize( inputDataset );
215 
216  //we need at least one band
217  if ( GDALGetRasterCount( inputDataset ) < 1 )
218  {
219  GDALClose( inputDataset );
220  return nullptr;
221  }
222  }
223  return inputDataset;
224 }
225 
226 GDALDriverH QgsNineCellFilter::openOutputDriver()
227 {
228  char **driverMetadata = nullptr;
229 
230  //open driver
231  GDALDriverH outputDriver = GDALGetDriverByName( mOutputFormat.toLocal8Bit().data() );
232 
233  if ( !outputDriver )
234  {
235  return outputDriver; //return nullptr, driver does not exist
236  }
237 
238  driverMetadata = GDALGetMetadata( outputDriver, nullptr );
239  if ( !CSLFetchBoolean( driverMetadata, GDAL_DCAP_CREATE, false ) )
240  {
241  return nullptr; //driver exist, but it does not support the create operation
242  }
243 
244  return outputDriver;
245 }
246 
247 GDALDatasetH QgsNineCellFilter::openOutputFile( GDALDatasetH inputDataset, GDALDriverH outputDriver )
248 {
249  if ( !inputDataset )
250  {
251  return nullptr;
252  }
253 
254  int xSize = GDALGetRasterXSize( inputDataset );
255  int ySize = GDALGetRasterYSize( inputDataset );
256 
257  //open output file
258  char **papszOptions = nullptr;
259  GDALDatasetH outputDataset = GDALCreate( outputDriver, mOutputFile.toUtf8().constData(), xSize, ySize, 1, GDT_Float32, papszOptions );
260  if ( !outputDataset )
261  {
262  return outputDataset;
263  }
264 
265  //get geotransform from inputDataset
266  double geotransform[6];
267  if ( GDALGetGeoTransform( inputDataset, geotransform ) != CE_None )
268  {
269  GDALClose( outputDataset );
270  return nullptr;
271  }
272  GDALSetGeoTransform( outputDataset, geotransform );
273 
274  //make sure mCellSizeX and mCellSizeY are always > 0
275  mCellSizeX = geotransform[1];
276  if ( mCellSizeX < 0 )
277  {
279  }
280  mCellSizeY = geotransform[5];
281  if ( mCellSizeY < 0 )
282  {
284  }
285 
286  const char *projection = GDALGetProjectionRef( inputDataset );
287  GDALSetProjection( outputDataset, projection );
288 
289  return outputDataset;
290 }
291 
#define QgsDebugMsg(str)
Definition: qgslogger.h:37
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.
int processRaster(QProgressDialog *p)
Starts the calculation, reads from mInputFile and stores the result in mOutputFile.
void * GDALDatasetH
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.