Skip to content
Permalink
Browse files

Simplify interpolation API

Don't require both pixel size and row/col count in constructor,
since we can calculate the pixel size from the extent and row/column
count.
  • Loading branch information
nyalldawson committed Nov 3, 2017
1 parent b36dd12 commit fe78611cf54887cc4f1770b7035811555c2bdc9f
@@ -17,14 +17,23 @@ class QgsGridFileWriter
#include "qgsgridfilewriter.h"
%End
public:
QgsGridFileWriter( QgsInterpolator *i, const QString &outputPath, const QgsRectangle &extent, int nCols, int nRows, double cellSizeX, double cellSizeY );

QgsGridFileWriter( QgsInterpolator *interpolator, const QString &outputPath, const QgsRectangle &extent, int nCols, int nRows );
%Docstring
Constructor for QgsGridFileWriter, for the specified ``interpolator``.

The ``outputPath`` argument is used to set the output file path.

The ``extent`` and ``nCols``, ``nRows`` arguments dictate the extent and size of the output raster.
%End

int writeFile( QgsFeedback *feedback = 0 );
%Docstring
Writes the grid file.
\param feedback optional feedback object for progress reports and cancelation support
:return: 0 in case of success*

An optional ``feedback`` object can be set for progress reports and cancelation support

:return: 0 in case of success
:rtype: int
%End

@@ -89,8 +89,6 @@ class IdwInterpolation(QgisAlgorithm):
DISTANCE_COEFFICIENT = 'DISTANCE_COEFFICIENT'
COLUMNS = 'COLUMNS'
ROWS = 'ROWS'
CELLSIZE_X = 'CELLSIZE_X'
CELLSIZE_Y = 'CELLSIZE_Y'
EXTENT = 'EXTENT'
OUTPUT = 'OUTPUT'

@@ -116,12 +114,6 @@ def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterNumber(self.ROWS,
self.tr('Number of rows'),
minValue=0, maxValue=10000000, defaultValue=300))
self.addParameter(QgsProcessingParameterNumber(self.CELLSIZE_X,
self.tr('Cell Size X'), type=QgsProcessingParameterNumber.Double,
minValue=0.0, maxValue=999999.000000, defaultValue=0.0))
self.addParameter(QgsProcessingParameterNumber(self.CELLSIZE_Y,
self.tr('Cell Size Y'), type=QgsProcessingParameterNumber.Double,
minValue=0.0, maxValue=999999.000000, defaultValue=0.0))
self.addParameter(QgsProcessingParameterExtent(self.EXTENT,
self.tr('Extent'),
optional=False))
@@ -139,8 +131,6 @@ def processAlgorithm(self, parameters, context, feedback):
coefficient = self.parameterAsDouble(parameters, self.DISTANCE_COEFFICIENT, context)
columns = self.parameterAsInt(parameters, self.COLUMNS, context)
rows = self.parameterAsInt(parameters, self.ROWS, context)
cellsizeX = self.parameterAsDouble(parameters, self.CELLSIZE_X, context)
cellsizeY = self.parameterAsDouble(parameters, self.CELLSIZE_Y, context)
bbox = self.parameterAsExtent(parameters, self.EXTENT, context)
output = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)

@@ -176,9 +166,7 @@ def processAlgorithm(self, parameters, context, feedback):
output,
bbox,
columns,
rows,
cellsizeX,
cellsizeY)
rows)

writer.writeFile(feedback)
return {self.OUTPUT: output}
@@ -92,8 +92,6 @@ class TinInterpolation(QgisAlgorithm):
METHOD = 'METHOD'
COLUMNS = 'COLUMNS'
ROWS = 'ROWS'
CELLSIZE_X = 'CELLSIZE_X'
CELLSIZE_Y = 'CELLSIZE_Y'
EXTENT = 'EXTENT'
OUTPUT = 'OUTPUT'
TRIANGULATION = 'TRIANGULATION'
@@ -124,12 +122,6 @@ def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterNumber(self.ROWS,
self.tr('Number of rows'),
minValue=0, maxValue=10000000, defaultValue=300))
self.addParameter(QgsProcessingParameterNumber(self.CELLSIZE_X,
self.tr('Cell size X'), type=QgsProcessingParameterNumber.Double,
minValue=0.0, maxValue=999999.000000, defaultValue=0.0))
self.addParameter(QgsProcessingParameterNumber(self.CELLSIZE_Y,
self.tr('Cell size Y'), type=QgsProcessingParameterNumber.Double,
minValue=0.0, maxValue=999999.000000, defaultValue=0.0))
self.addParameter(QgsProcessingParameterExtent(self.EXTENT,
self.tr('Extent'),
optional=False))
@@ -154,19 +146,13 @@ def processAlgorithm(self, parameters, context, feedback):
method = self.parameterAsEnum(parameters, self.METHOD, context)
columns = self.parameterAsInt(parameters, self.COLUMNS, context)
rows = self.parameterAsInt(parameters, self.ROWS, context)
cellsizeX = self.parameterAsDouble(parameters, self.CELLSIZE_X, context)
cellsizeY = self.parameterAsDouble(parameters, self.CELLSIZE_Y, context)
bbox = self.parameterAsExtent(parameters, self.EXTENT, context)
output = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)

if interpolationData is None:
raise QgsProcessingException(
self.tr('You need to specify at least one input layer.'))

if cellsizeX == 0.0 or cellsizeY == 0.0:
raise QgsProcessingException(
self.tr('Cellsize should be greater than 0.'))

layerData = []
layers = []
crs = QgsCoordinateReferenceSystem()
@@ -207,9 +193,7 @@ def processAlgorithm(self, parameters, context, feedback):
output,
bbox,
columns,
rows,
cellsizeX,
cellsizeY)
rows)

writer.writeFile(feedback)
return {self.OUTPUT: output, self.TRIANGULATION: triangulation_dest_id}
@@ -1497,8 +1497,6 @@ tests:
- algorithm: qgis:idwinterpolation
name: IDW interpolation using attribute
params:
CELLSIZE_X: 0.02667
CELLSIZE_Y: 0.02667
COLUMNS: 300
DISTANCE_COEFFICIENT: 2.0
EXTENT: 0, 8, -5, 3
@@ -1508,14 +1506,14 @@ tests:
ROWS: 300
results:
OUTPUT:
hash: 56d2671d50444f8571affba3f9e585830b82af5e380394178f521065
hash:
- 56d2671d50444f8571affba3f9e585830b82af5e380394178f521065
- 2ae62aca803e6864ac75e47b4357292b0637d811cb510ed19471f422
type: rasterhash

- algorithm: qgis:idwinterpolation
name: IDW interpolation using Z value
params:
CELLSIZE_X: 0.02667
CELLSIZE_Y: 0.02667
COLUMNS: 300
DISTANCE_COEFFICIENT: 2.0
EXTENT: 0, 8, -5, 3
@@ -1525,14 +1523,14 @@ tests:
ROWS: 300
results:
OUTPUT:
hash: 56d2671d50444f8571affba3f9e585830b82af5e380394178f521065
hash:
- 56d2671d50444f8571affba3f9e585830b82af5e380394178f521065
- 2ae62aca803e6864ac75e47b4357292b0637d811cb510ed19471f422
type: rasterhash

- algorithm: qgis:tininterpolation
name: TIN interpolation using attribute
params:
CELLSIZE_X: 0.02667
CELLSIZE_Y: 0.02667
COLUMNS: 300
EXTENT: 0, 8, -5, 3
INTERPOLATION_DATA:
@@ -1542,7 +1540,9 @@ tests:
ROWS: 300
results:
OUTPUT:
hash: 87f40be6ec08f3fcbb5707762de71f6be35bb265c61f594335562a26
hash:
- 87f40be6ec08f3fcbb5707762de71f6be35bb265c61f594335562a26
- a31f0faf918ebe0902e5c9c5c8cf606d30f52eb4094bf24e4f8ac67a
type: rasterhash
#TRIANGULATION_FILE:
# name: expected/triangulation.gml
@@ -1551,8 +1551,6 @@ tests:
- algorithm: qgis:tininterpolation
name: TIN interpolation using Z value
params:
CELLSIZE_X: 0.02667
CELLSIZE_Y: 0.02667
COLUMNS: 300
EXTENT: 0, 8, -5, 3
INTERPOLATION_DATA:
@@ -1562,7 +1560,9 @@ tests:
ROWS: 300
results:
OUTPUT:
hash: 5e14dd0b879884b8b8da56c082947dad681feb4e9f1137f5cda126f8
hash:
- 5e14dd0b879884b8b8da56c082947dad681feb4e9f1137f5cda126f8
- b8389559d6a85e7a672d72254228473fae71af2d89e5a5dd73d1b0d7
type: rasterhash
#TRIANULATION_FILE:
# name: expected/triangulation.gml
@@ -22,14 +22,14 @@
#include <QFile>
#include <QFileInfo>

QgsGridFileWriter::QgsGridFileWriter( QgsInterpolator *i, const QString &outputPath, const QgsRectangle &extent, int nCols, int nRows, double cellSizeX, double cellSizeY )
QgsGridFileWriter::QgsGridFileWriter( QgsInterpolator *i, const QString &outputPath, const QgsRectangle &extent, int nCols, int nRows )
: mInterpolator( i )
, mOutputFilePath( outputPath )
, mInterpolationExtent( extent )
, mNumColumns( nCols )
, mNumRows( nRows )
, mCellSizeX( cellSizeX )
, mCellSizeY( cellSizeY )
, mCellSizeX( extent.width() / nCols )
, mCellSizeY( extent.height() / nRows )
{}

int QgsGridFileWriter::writeFile( QgsFeedback *feedback )
@@ -33,13 +33,23 @@ class QgsFeedback;
class ANALYSIS_EXPORT QgsGridFileWriter
{
public:
QgsGridFileWriter( QgsInterpolator *i, const QString &outputPath, const QgsRectangle &extent, int nCols, int nRows, double cellSizeX, double cellSizeY );

/**
* Writes the grid file.
\param feedback optional feedback object for progress reports and cancelation support
\returns 0 in case of success*/
* Constructor for QgsGridFileWriter, for the specified \a interpolator.
*
* The \a outputPath argument is used to set the output file path.
*
* The \a extent and \a nCols, \a nRows arguments dictate the extent and size of the output raster.
*/
QgsGridFileWriter( QgsInterpolator *interpolator, const QString &outputPath, const QgsRectangle &extent, int nCols, int nRows );

/**
* Writes the grid file.
*
* An optional \a feedback object can be set for progress reports and cancelation support
*
* \returns 0 in case of success
*/
int writeFile( QgsFeedback *feedback = nullptr );

private:

0 comments on commit fe78611

Please sign in to comment.
You can’t perform that action at this time.