Skip to content

Commit a07ea33

Browse files
committedAug 18, 2017
Port IDW Interpolation alg to new API
1 parent 355cff1 commit a07ea33

File tree

3 files changed

+112
-130
lines changed

3 files changed

+112
-130
lines changed
 

‎python/plugins/processing/algs/qgis/IdwInterpolation.py

Lines changed: 75 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,58 @@
3131

3232
from qgis.core import (QgsRectangle,
3333
QgsProcessingUtils,
34-
QgsProcessingParameterDefinition)
34+
QgsProcessingParameterDefinition,
35+
QgsProcessingParameterNumber,
36+
QgsProcessingParameterExtent,
37+
QgsProcessingParameterRasterDestination,
38+
QgsProcessingException)
3539
from qgis.analysis import (QgsInterpolator,
3640
QgsIDWInterpolator,
37-
QgsGridFileWriter
38-
)
41+
QgsGridFileWriter)
3942

4043
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
41-
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
42-
from processing.core.parameters import (Parameter,
43-
ParameterNumber,
44-
ParameterExtent,
45-
_splitParameterOptions)
46-
from processing.core.outputs import OutputRaster
4744

4845
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
4946

5047

48+
class ParameterInterpolationData(QgsProcessingParameterDefinition):
49+
50+
def __init__(self, name='', description=''):
51+
super().__init__(name, description)
52+
self.setMetadata({
53+
'widget_wrapper': 'processing.algs.qgis.ui.InterpolationDataWidget.InterpolationDataWidgetWrapper'
54+
})
55+
56+
def type(self):
57+
return 'idw_interpolation_data'
58+
59+
def clone(self):
60+
return ParameterInterpolationData(self.name(), self.description())
61+
62+
@staticmethod
63+
def parseValue(value):
64+
if value is None:
65+
return None
66+
67+
if value == '':
68+
return None
69+
70+
if isinstance(value, str):
71+
return value if value != '' else None
72+
else:
73+
return ParameterInterpolationData.dataToString(value)
74+
75+
@staticmethod
76+
def dataToString(data):
77+
s = ''
78+
for c in data:
79+
s += '{}, {}, {:d}, {:d};'.format(c[0],
80+
c[1],
81+
c[2],
82+
c[3])
83+
return s[:-1]
84+
85+
5186
class IdwInterpolation(QgisAlgorithm):
5287

5388
INTERPOLATION_DATA = 'INTERPOLATION_DATA'
@@ -57,7 +92,7 @@ class IdwInterpolation(QgisAlgorithm):
5792
CELLSIZE_X = 'CELLSIZE_X'
5893
CELLSIZE_Y = 'CELLSIZE_Y'
5994
EXTENT = 'EXTENT'
60-
OUTPUT_LAYER = 'OUTPUT_LAYER'
95+
OUTPUT = 'OUTPUT'
6196

6297
def icon(self):
6398
return QIcon(os.path.join(pluginPath, 'images', 'interpolation.png'))
@@ -69,78 +104,29 @@ def __init__(self):
69104
super().__init__()
70105

71106
def initAlgorithm(self, config=None):
72-
class ParameterInterpolationData(Parameter):
73-
default_metadata = {
74-
'widget_wrapper': 'processing.algs.qgis.ui.InterpolationDataWidget.InterpolationDataWidgetWrapper'
75-
}
76-
77-
def __init__(self, name='', description=''):
78-
Parameter.__init__(self, name, description)
79-
80-
def setValue(self, value):
81-
if value is None:
82-
if not self.flags() & QgsProcessingParameterDefinition.FlagOptional:
83-
return False
84-
self.value = None
85-
return True
86-
87-
if value == '':
88-
if not self.flags() & QgsProcessingParameterDefinition.FlagOptional:
89-
return False
90-
91-
if isinstance(value, str):
92-
self.value = value if value != '' else None
93-
else:
94-
self.value = ParameterInterpolationData.dataToString(value)
95-
return True
96-
97-
def getValueAsCommandLineParameter(self):
98-
return '"{}"'.format(self.value)
99-
100-
def getAsScriptCode(self):
101-
param_type = ''
102-
param_type += 'interpolation data '
103-
return '##' + self.name + '=' + param_type
104-
105-
@classmethod
106-
def fromScriptCode(self, line):
107-
isOptional, name, definition = _splitParameterOptions(line)
108-
descName = QgsProcessingParameters.descriptionFromName(name)
109-
parent = definition.lower().strip()[len('interpolation data') + 1:]
110-
return ParameterInterpolationData(name, descName, parent)
111-
112-
@staticmethod
113-
def dataToString(data):
114-
s = ''
115-
for c in data:
116-
s += '{}, {}, {:d}, {:d};'.format(c[0],
117-
c[1],
118-
c[2],
119-
c[3])
120-
return s[:-1]
121107

122108
self.addParameter(ParameterInterpolationData(self.INTERPOLATION_DATA,
123109
self.tr('Input layer(s)')))
124-
self.addParameter(ParameterNumber(self.DISTANCE_COEFFICIENT,
125-
self.tr('Distance coefficient P'),
126-
0.0, 99.99, 2.0))
127-
self.addParameter(ParameterNumber(self.COLUMNS,
128-
self.tr('Number of columns'),
129-
0, 10000000, 300))
130-
self.addParameter(ParameterNumber(self.ROWS,
131-
self.tr('Number of rows'),
132-
0, 10000000, 300))
133-
self.addParameter(ParameterNumber(self.CELLSIZE_X,
134-
self.tr('Cell Size X'),
135-
0.0, 999999.000000, 0.0))
136-
self.addParameter(ParameterNumber(self.CELLSIZE_Y,
137-
self.tr('Cell Size Y'),
138-
0.0, 999999.000000, 0.0))
139-
self.addParameter(ParameterExtent(self.EXTENT,
140-
self.tr('Extent'),
141-
optional=False))
142-
self.addOutput(OutputRaster(self.OUTPUT_LAYER,
143-
self.tr('Interpolated')))
110+
self.addParameter(QgsProcessingParameterNumber(self.DISTANCE_COEFFICIENT,
111+
self.tr('Distance coefficient P'), type=QgsProcessingParameterNumber.Double,
112+
minValue=0.0, maxValue=99.99, defaultValue=2.0))
113+
self.addParameter(QgsProcessingParameterNumber(self.COLUMNS,
114+
self.tr('Number of columns'),
115+
minValue=0, maxValue=10000000, defaultValue=300))
116+
self.addParameter(QgsProcessingParameterNumber(self.ROWS,
117+
self.tr('Number of rows'),
118+
minValue=0, maxValue=10000000, defaultValue=300))
119+
self.addParameter(QgsProcessingParameterNumber(self.CELLSIZE_X,
120+
self.tr('Cell Size X'), type=QgsProcessingParameterNumber.Double,
121+
minValue=0.0, maxValue=999999.000000, defaultValue=0.0))
122+
self.addParameter(QgsProcessingParameterNumber(self.CELLSIZE_Y,
123+
self.tr('Cell Size Y'), type=QgsProcessingParameterNumber.Double,
124+
minValue=0.0, maxValue=999999.000000, defaultValue=0.0))
125+
self.addParameter(QgsProcessingParameterExtent(self.EXTENT,
126+
self.tr('Extent'),
127+
optional=False))
128+
self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT,
129+
self.tr('Interpolated')))
144130

145131
def name(self):
146132
return 'idwinterpolation'
@@ -149,25 +135,19 @@ def displayName(self):
149135
return self.tr('IDW interpolation')
150136

151137
def processAlgorithm(self, parameters, context, feedback):
152-
interpolationData = self.getParameterValue(self.INTERPOLATION_DATA)
153-
coefficient = self.getParameterValue(self.DISTANCE_COEFFICIENT)
154-
columns = self.getParameterValue(self.COLUMNS)
155-
rows = self.getParameterValue(self.ROWS)
156-
cellsizeX = self.getParameterValue(self.CELLSIZE_X)
157-
cellsizeY = self.getParameterValue(self.CELLSIZE_Y)
158-
extent = self.getParameterValue(self.EXTENT).split(',')
159-
output = self.getOutputValue(self.OUTPUT_LAYER)
138+
interpolationData = ParameterInterpolationData.parseValue(parameters[self.INTERPOLATION_DATA])
139+
coefficient = self.parameterAsDouble(parameters, self.DISTANCE_COEFFICIENT, context)
140+
columns = self.parameterAsInt(parameters, self.COLUMNS, context)
141+
rows = self.parameterAsInt(parameters, self.ROWS, context)
142+
cellsizeX = self.parameterAsDouble(parameters, self.CELLSIZE_X, context)
143+
cellsizeY = self.parameterAsDouble(parameters, self.CELLSIZE_Y, context)
144+
bbox = self.parameterAsExtent(parameters, self.EXTENT, context)
145+
output = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
160146

161147
if interpolationData is None:
162-
raise GeoAlgorithmExecutionException(
148+
raise QgsProcessingException(
163149
self.tr('You need to specify at least one input layer.'))
164150

165-
xMin = float(extent[0])
166-
xMax = float(extent[1])
167-
yMin = float(extent[2])
168-
yMax = float(extent[3])
169-
bbox = QgsRectangle(xMin, yMin, xMax, yMax)
170-
171151
layerData = []
172152
layers = []
173153
for row in interpolationData.split(';'):
@@ -201,3 +181,4 @@ def processAlgorithm(self, parameters, context, feedback):
201181
cellsizeY)
202182

203183
writer.writeFile()
184+
return {self.OUTPUT: output}

‎python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
from .HubDistancePoints import HubDistancePoints
8484
from .HubLines import HubLines
8585
from .HypsometricCurves import HypsometricCurves
86+
from .IdwInterpolation import IdwInterpolation
8687
from .ImportIntoPostGIS import ImportIntoPostGIS
8788
from .ImportIntoSpatialite import ImportIntoSpatialite
8889
from .Intersection import Intersection
@@ -168,7 +169,6 @@
168169
# from .FieldPyculator import FieldsPyculator
169170
# from .SelectByAttributeSum import SelectByAttributeSum
170171
# from .DefineProjection import DefineProjection
171-
# from .IdwInterpolation import IdwInterpolation
172172
# from .TinInterpolation import TinInterpolation
173173
# from .RasterCalculator import RasterCalculator
174174
# from .ExecuteSQL import ExecuteSQL
@@ -194,7 +194,7 @@ def getAlgs(self):
194194
# FieldsPyculator(),
195195
# FieldsMapper(), SelectByAttributeSum()
196196
# DefineProjection(),
197-
# IdwInterpolation(), TinInterpolation(),
197+
# TinInterpolation(),
198198
# RasterCalculator(),
199199
# ExecuteSQL(), FindProjection(),
200200
# ]
@@ -241,6 +241,7 @@ def getAlgs(self):
241241
HubDistancePoints(),
242242
HubLines(),
243243
HypsometricCurves(),
244+
IdwInterpolation(),
244245
ImportIntoPostGIS(),
245246
ImportIntoSpatialite(),
246247
Intersection(),

‎python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,40 +1494,40 @@ tests:
14941494
name: expected/multipoint_delaunay.gml
14951495
type: vector
14961496

1497-
# - algorithm: qgis:idwinterpolation
1498-
# name: IDW interpolation using attribute
1499-
# params:
1500-
# CELLSIZE_X: 0.02667
1501-
# CELLSIZE_Y: 0.02667
1502-
# COLUMNS: 300
1503-
# DISTANCE_COEFFICIENT: 2.0
1504-
# EXTENT: 0, 8, -5, 3
1505-
# INTERPOLATION_DATA:
1506-
# name: pointsz.gml,False,1,0
1507-
# type: interpolation
1508-
# ROWS: 300
1509-
# results:
1510-
# OUTPUT_LAYER:
1511-
# hash: 56d2671d50444f8571affba3f9e585830b82af5e380394178f521065
1512-
# type: rasterhash
1513-
#
1514-
# - algorithm: qgis:idwinterpolation
1515-
# name: IDW interpolation using Z value
1516-
# params:
1517-
# CELLSIZE_X: 0.02667
1518-
# CELLSIZE_Y: 0.02667
1519-
# COLUMNS: 300
1520-
# DISTANCE_COEFFICIENT: 2.0
1521-
# EXTENT: 0, 8, -5, 3
1522-
# INTERPOLATION_DATA:
1523-
# name: pointsz.gml,True,-1,0
1524-
# type: interpolation
1525-
# ROWS: 300
1526-
# results:
1527-
# OUTPUT_LAYER:
1528-
# hash: 56d2671d50444f8571affba3f9e585830b82af5e380394178f521065
1529-
# type: rasterhash
1530-
#
1497+
- algorithm: qgis:idwinterpolation
1498+
name: IDW interpolation using attribute
1499+
params:
1500+
CELLSIZE_X: 0.02667
1501+
CELLSIZE_Y: 0.02667
1502+
COLUMNS: 300
1503+
DISTANCE_COEFFICIENT: 2.0
1504+
EXTENT: 0, 8, -5, 3
1505+
INTERPOLATION_DATA:
1506+
name: pointsz.gml,False,1,0
1507+
type: interpolation
1508+
ROWS: 300
1509+
results:
1510+
OUTPUT:
1511+
hash: 56d2671d50444f8571affba3f9e585830b82af5e380394178f521065
1512+
type: rasterhash
1513+
1514+
- algorithm: qgis:idwinterpolation
1515+
name: IDW interpolation using Z value
1516+
params:
1517+
CELLSIZE_X: 0.02667
1518+
CELLSIZE_Y: 0.02667
1519+
COLUMNS: 300
1520+
DISTANCE_COEFFICIENT: 2.0
1521+
EXTENT: 0, 8, -5, 3
1522+
INTERPOLATION_DATA:
1523+
name: pointsz.gml,True,-1,0
1524+
type: interpolation
1525+
ROWS: 300
1526+
results:
1527+
OUTPUT:
1528+
hash: 56d2671d50444f8571affba3f9e585830b82af5e380394178f521065
1529+
type: rasterhash
1530+
15311531
# - algorithm: qgis:tininterpolation
15321532
# name: TIN interpolation using attribute
15331533
# params:

0 commit comments

Comments
 (0)
Please sign in to comment.