Skip to content

Commit

Permalink
Simplify interpolation API
Browse files Browse the repository at this point in the history
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 fe78611
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 52 deletions.
15 changes: 12 additions & 3 deletions python/analysis/interpolation/qgsgridfilewriter.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 1 addition & 13 deletions python/plugins/processing/algs/qgis/IdwInterpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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))
Expand All @@ -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)

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

writer.writeFile(feedback)
return {self.OUTPUT: output}
18 changes: 1 addition & 17 deletions python/plugins/processing/algs/qgis/TinInterpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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))
Expand All @@ -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()
Expand Down Expand Up @@ -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}
24 changes: 12 additions & 12 deletions python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -1542,7 +1540,9 @@ tests:
ROWS: 300
results:
OUTPUT:
hash: 87f40be6ec08f3fcbb5707762de71f6be35bb265c61f594335562a26
hash:
- 87f40be6ec08f3fcbb5707762de71f6be35bb265c61f594335562a26
- a31f0faf918ebe0902e5c9c5c8cf606d30f52eb4094bf24e4f8ac67a
type: rasterhash
#TRIANGULATION_FILE:
# name: expected/triangulation.gml
Expand All @@ -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:
Expand All @@ -1562,7 +1560,9 @@ tests:
ROWS: 300
results:
OUTPUT:
hash: 5e14dd0b879884b8b8da56c082947dad681feb4e9f1137f5cda126f8
hash:
- 5e14dd0b879884b8b8da56c082947dad681feb4e9f1137f5cda126f8
- b8389559d6a85e7a672d72254228473fae71af2d89e5a5dd73d1b0d7
type: rasterhash
#TRIANULATION_FILE:
# name: expected/triangulation.gml
Expand Down
6 changes: 3 additions & 3 deletions src/analysis/interpolation/qgsgridfilewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
18 changes: 14 additions & 4 deletions src/analysis/interpolation/qgsgridfilewriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit fe78611

Please sign in to comment.