Branch data Line data Source code
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 : : #ifndef QGSNINECELLFILTER_H 19 : : #define QGSNINECELLFILTER_H 20 : : 21 : : #include <QString> 22 : : #include "gdal.h" 23 : : #include "qgis_analysis.h" 24 : : #include "qgsogrutils.h" 25 : : 26 : : class QgsFeedback; 27 : : 28 : : /** 29 : : * \ingroup analysis 30 : : * \brief Base class for raster analysis methods that work with a 3x3 cell filter and calculate the value of each cell based on 31 : : the cell value and the eight neighbour cells. 32 : : * Common examples are slope and aspect calculation in DEMs. Subclasses only implement 33 : : * the method that calculates the new value from the nine values. Everything else (reading file, writing file) is done by this subclass 34 : : */ 35 : : class ANALYSIS_EXPORT QgsNineCellFilter 36 : : { 37 : : public: 38 : : //! Constructor that takes input file, output file and output format (GDAL string) 39 : : QgsNineCellFilter( const QString &inputFile, const QString &outputFile, const QString &outputFormat ); 40 : 0 : virtual ~QgsNineCellFilter() = default; 41 : : 42 : : /** 43 : : * Starts the calculation, reads from mInputFile and stores the result in mOutputFile 44 : : * \param feedback feedback object that receives update and that is checked for cancellation. 45 : : * \returns 0 in case of success 46 : : */ 47 : : int processRaster( QgsFeedback *feedback = nullptr ); 48 : : 49 : : double cellSizeX() const { return mCellSizeX; } 50 : 0 : void setCellSizeX( double size ) { mCellSizeX = size; } 51 : : double cellSizeY() const { return mCellSizeY; } 52 : 0 : void setCellSizeY( double size ) { mCellSizeY = size; } 53 : : 54 : : double zFactor() const { return mZFactor; } 55 : 0 : void setZFactor( double factor ) { mZFactor = factor; } 56 : : 57 : : double inputNodataValue() const { return mInputNodataValue; } 58 : 0 : void setInputNodataValue( double value ) { mInputNodataValue = value; } 59 : : double outputNodataValue() const { return mOutputNodataValue; } 60 : 0 : void setOutputNodataValue( double value ) { mOutputNodataValue = value; } 61 : : 62 : : /** 63 : : * Calculates output value from nine input values. The input values and the output 64 : : * value can be equal to the nodata value if not present or outside of the border. 65 : : * Must be implemented by subclasses. 66 : : * 67 : : * First index of the input cell is the row, second index is the column 68 : : * 69 : : * \param x11 surrounding cell top left 70 : : * \param x21 surrounding cell central left 71 : : * \param x31 surrounding cell bottom left 72 : : * \param x12 surrounding cell top central 73 : : * \param x22 the central cell for which the value will be calculated 74 : : * \param x32 surrounding cell bottom central 75 : : * \param x13 surrounding cell top right 76 : : * \param x23 surrounding cell central right 77 : : * \param x33 surrounding cell bottom right 78 : : * \return the calculated cell value for the central cell x22 79 : : */ 80 : : virtual float processNineCellWindow( float *x11, float *x21, float *x31, 81 : : float *x12, float *x22, float *x32, 82 : : float *x13, float *x23, float *x33 ) = 0; 83 : : 84 : : private: 85 : : //default constructor forbidden. We need input file, output file and format obligatory 86 : : QgsNineCellFilter() = delete; 87 : : 88 : : //! Opens the input file and returns the dataset handle and the number of pixels in x-/y- direction 89 : : gdal::dataset_unique_ptr openInputFile( int &nCellsX, int &nCellsY ); 90 : : 91 : : /** 92 : : * Opens the output driver and tests if it supports the creation of a new dataset 93 : : * \returns nullptr on error and the driver handle on success 94 : : */ 95 : : GDALDriverH openOutputDriver(); 96 : : 97 : : /** 98 : : * Opens the output file and sets the same geotransform and CRS as the input data 99 : : * \returns the output dataset or nullptr in case of error 100 : : */ 101 : : gdal::dataset_unique_ptr openOutputFile( GDALDatasetH inputDataset, GDALDriverH outputDriver ); 102 : : 103 : : /** 104 : : * \brief processRasterCPU executes the computation on the CPU 105 : : * \param feedback instance of QgsFeedback, to allow for progress monitoring and cancellation 106 : : * \return an opaque integer for error codes: 0 in case of success 107 : : */ 108 : : int processRasterCPU( QgsFeedback *feedback = nullptr ); 109 : : 110 : : #ifdef HAVE_OPENCL 111 : : 112 : : /** 113 : : * \brief processRasterGPU executes the computation on the GPU 114 : : * \param source path to the OpenCL source file 115 : : * \param feedback instance of QgsFeedback, to allow for progress monitoring and cancellation 116 : : * \return an opaque integer for error codes: 0 in case of success 117 : : */ 118 : : int processRasterGPU( const QString &source, QgsFeedback *feedback = nullptr ); 119 : : 120 : : /** 121 : : * \brief addExtraRasterParams allow derived classes to add parameters needed 122 : : * by OpenCL program 123 : : * \param params vector of parameters passed to OpenCL algorithm 124 : : */ 125 : : virtual void addExtraRasterParams( std::vector<float> ¶ms ) 126 : : { 127 : : Q_UNUSED( params ) 128 : : } 129 : : 130 : : virtual const QString openClProgramBaseName() const 131 : : { 132 : : return QString(); 133 : : } 134 : : 135 : : #endif 136 : : 137 : : protected: 138 : : 139 : : QString mInputFile; 140 : : QString mOutputFile; 141 : : QString mOutputFormat; 142 : : 143 : : double mCellSizeX = -1.0; 144 : : double mCellSizeY = -1.0; 145 : : //! The nodata value of the input layer 146 : : float mInputNodataValue = -1.0; 147 : : //! The nodata value of the output layer 148 : : float mOutputNodataValue = -1.0; 149 : : //! Scale factor for z-value if x-/y- units are different to z-units (111120 for degree->meters and 370400 for degree->feet) 150 : : double mZFactor = 1.0; 151 : : }; 152 : : 153 : : #endif // QGSNINECELLFILTER_H