Skip to content

Commit

Permalink
Merge pull request #8759 from alexbruy/processing-interpolation
Browse files Browse the repository at this point in the history
[processing][needs-docs] Add cell size parameter to native interpolation algorithms (fix #18556, #20114)
  • Loading branch information
alexbruy authored Dec 30, 2018
2 parents 8186f23 + acd4cec commit 84d155e
Show file tree
Hide file tree
Showing 7 changed files with 549 additions and 270 deletions.
80 changes: 33 additions & 47 deletions python/plugins/processing/algs/qgis/IdwInterpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,62 +31,26 @@

from qgis.core import (QgsRectangle,
QgsProcessingUtils,
QgsProcessingParameterDefinition,
QgsProcessingParameterNumber,
QgsProcessingParameterExtent,
QgsProcessingParameterDefinition,
QgsProcessingParameterRasterDestination,
QgsProcessingException)
from qgis.analysis import (QgsInterpolator,
QgsIDWInterpolator,
QgsGridFileWriter)

from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.algs.qgis.ui.InterpolationWidgets import ParameterInterpolationData, ParameterPixelSize

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


class ParameterInterpolationData(QgsProcessingParameterDefinition):

def __init__(self, name='', description=''):
super().__init__(name, description)
self.setMetadata({
'widget_wrapper': 'processing.algs.qgis.ui.InterpolationDataWidget.InterpolationDataWidgetWrapper'
})

def type(self):
return 'idw_interpolation_data'

def clone(self):
return ParameterInterpolationData(self.name(), self.description())

@staticmethod
def parseValue(value):
if value is None:
return None

if value == '':
return None

if isinstance(value, str):
return value if value != '' else None
else:
return ParameterInterpolationData.dataToString(value)

@staticmethod
def dataToString(data):
s = ''
for c in data:
s += '{}::~::{}::~::{:d}::~::{:d};'.format(c[0],
c[1],
c[2],
c[3])
return s[:-1]


class IdwInterpolation(QgisAlgorithm):

INTERPOLATION_DATA = 'INTERPOLATION_DATA'
DISTANCE_COEFFICIENT = 'DISTANCE_COEFFICIENT'
PIXEL_SIZE = 'PIXEL_SIZE'
COLUMNS = 'COLUMNS'
ROWS = 'ROWS'
EXTENT = 'EXTENT'
Expand All @@ -111,15 +75,31 @@ def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterNumber(self.DISTANCE_COEFFICIENT,
self.tr('Distance coefficient P'), type=QgsProcessingParameterNumber.Double,
minValue=0.0, maxValue=99.99, defaultValue=2.0))
self.addParameter(QgsProcessingParameterNumber(self.COLUMNS,
self.tr('Number of columns'),
minValue=0, maxValue=10000000, defaultValue=300))
self.addParameter(QgsProcessingParameterNumber(self.ROWS,
self.tr('Number of rows'),
minValue=0, maxValue=10000000, defaultValue=300))
self.addParameter(QgsProcessingParameterExtent(self.EXTENT,
self.tr('Extent'),
optional=False))
pixel_size_param = ParameterPixelSize(self.PIXEL_SIZE,
self.tr('Output raster size'),
layersData=self.INTERPOLATION_DATA,
extent=self.EXTENT,
minValue=0.0,
default=0.1)
self.addParameter(pixel_size_param)

cols_param = QgsProcessingParameterNumber(self.COLUMNS,
self.tr('Number of columns'),
optional=True,
minValue=0, maxValue=10000000)
cols_param.setFlags(cols_param.flags() | QgsProcessingParameterDefinition.FlagHidden)
self.addParameter(cols_param)

rows_param = QgsProcessingParameterNumber(self.ROWS,
self.tr('Number of rows'),
optional=True,
minValue=0, maxValue=10000000)
rows_param.setFlags(rows_param.flags() | QgsProcessingParameterDefinition.FlagHidden)
self.addParameter(rows_param)

self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT,
self.tr('Interpolated')))

Expand All @@ -132,11 +112,17 @@ def displayName(self):
def processAlgorithm(self, parameters, context, feedback):
interpolationData = ParameterInterpolationData.parseValue(parameters[self.INTERPOLATION_DATA])
coefficient = self.parameterAsDouble(parameters, self.DISTANCE_COEFFICIENT, context)
columns = self.parameterAsInt(parameters, self.COLUMNS, context)
rows = self.parameterAsInt(parameters, self.ROWS, context)
bbox = self.parameterAsExtent(parameters, self.EXTENT, context)
pixel_size = self.parameterAsDouble(parameters, self.PIXEL_SIZE, context)
output = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)

columns = self.parameterAsInt(parameters, self.COLUMNS, context)
rows = self.parameterAsInt(parameters, self.ROWS, context)
if columns == 0:
columns = max(round(bbox.width() / pixel_size) + 1, 1)
if rows == 0:
rows = max(round(bbox.height() / pixel_size) + 1, 1)

if interpolationData is None:
raise QgsProcessingException(
self.tr('You need to specify at least one input layer.'))
Expand Down
80 changes: 33 additions & 47 deletions python/plugins/processing/algs/qgis/TinInterpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@

from qgis.core import (QgsProcessingUtils,
QgsProcessing,
QgsProcessingParameterDefinition,
QgsProcessingParameterEnum,
QgsProcessingParameterNumber,
QgsProcessingParameterExtent,
QgsProcessingParameterDefinition,
QgsProcessingParameterRasterDestination,
QgsWkbTypes,
QgsProcessingParameterFeatureSink,
Expand All @@ -45,51 +45,15 @@
QgsGridFileWriter)

from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.algs.qgis.ui.InterpolationWidgets import ParameterInterpolationData, ParameterPixelSize

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


class ParameterInterpolationData(QgsProcessingParameterDefinition):

def __init__(self, name='', description=''):
super().__init__(name, description)
self.setMetadata({
'widget_wrapper': 'processing.algs.qgis.ui.InterpolationDataWidget.InterpolationDataWidgetWrapper'
})

def type(self):
return 'tin_interpolation_data'

def clone(self):
return ParameterInterpolationData(self.name(), self.description())

@staticmethod
def parseValue(value):
if value is None:
return None

if value == '':
return None

if isinstance(value, str):
return value if value != '' else None
else:
return ParameterInterpolationData.dataToString(value)

@staticmethod
def dataToString(data):
s = ''
for c in data:
s += '{}::~:: {}::~:: {:d}::~:: {:d};'.format(c[0],
c[1],
c[2],
c[3])
return s[:-1]


class TinInterpolation(QgisAlgorithm):
INTERPOLATION_DATA = 'INTERPOLATION_DATA'
METHOD = 'METHOD'
PIXEL_SIZE = 'PIXEL_SIZE'
COLUMNS = 'COLUMNS'
ROWS = 'ROWS'
EXTENT = 'EXTENT'
Expand Down Expand Up @@ -119,15 +83,31 @@ def initAlgorithm(self, config=None):
self.tr('Interpolation method'),
options=self.METHODS,
defaultValue=0))
self.addParameter(QgsProcessingParameterNumber(self.COLUMNS,
self.tr('Number of columns'),
minValue=0, maxValue=10000000, defaultValue=300))
self.addParameter(QgsProcessingParameterNumber(self.ROWS,
self.tr('Number of rows'),
minValue=0, maxValue=10000000, defaultValue=300))
self.addParameter(QgsProcessingParameterExtent(self.EXTENT,
self.tr('Extent'),
optional=False))
pixel_size_param = ParameterPixelSize(self.PIXEL_SIZE,
self.tr('Output raster size'),
layersData=self.INTERPOLATION_DATA,
extent=self.EXTENT,
minValue=0.0,
default=0.1)
self.addParameter(pixel_size_param)

cols_param = QgsProcessingParameterNumber(self.COLUMNS,
self.tr('Number of columns'),
optional=True,
minValue=0, maxValue=10000000)
cols_param.setFlags(cols_param.flags() | QgsProcessingParameterDefinition.FlagHidden)
self.addParameter(cols_param)

rows_param = QgsProcessingParameterNumber(self.ROWS,
self.tr('Number of rows'),
optional=True,
minValue=0, maxValue=10000000)
rows_param.setFlags(rows_param.flags() | QgsProcessingParameterDefinition.FlagHidden)
self.addParameter(rows_param)

self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT,
self.tr('Interpolated')))

Expand All @@ -147,11 +127,17 @@ def displayName(self):
def processAlgorithm(self, parameters, context, feedback):
interpolationData = ParameterInterpolationData.parseValue(parameters[self.INTERPOLATION_DATA])
method = self.parameterAsEnum(parameters, self.METHOD, context)
columns = self.parameterAsInt(parameters, self.COLUMNS, context)
rows = self.parameterAsInt(parameters, self.ROWS, context)
bbox = self.parameterAsExtent(parameters, self.EXTENT, context)
pixel_size = self.parameterAsDouble(parameters, self.PIXEL_SIZE, context)
output = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)

columns = self.parameterAsInt(parameters, self.COLUMNS, context)
rows = self.parameterAsInt(parameters, self.ROWS, context)
if columns == 0:
columns = max(round(bbox.width() / pixel_size) + 1, 1)
if rows == 0:
rows = max(round(bbox.height() / pixel_size) + 1, 1)

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

0 comments on commit 84d155e

Please sign in to comment.