Skip to content

Commit

Permalink
[processing] add missed functionality from Interpolation plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Dec 20, 2016
1 parent c365439 commit 7dde862
Show file tree
Hide file tree
Showing 8 changed files with 484 additions and 418 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

"""
***************************************************************************
IdwInterpolationZValue.py
IdwInterpolation.py
---------------------
Date : October 2016
Copyright : (C) 2016 by Alexander Bruy
Expand All @@ -29,28 +29,28 @@

from qgis.PyQt.QtGui import QIcon

from qgis.core import QgsRectangle, QgsWkbTypes
from qgis.core import QgsRectangle
from qgis.analysis import (QgsInterpolator,
QgsIDWInterpolator,
QgsGridFileWriter
)
)

from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterSelection
from processing.core.parameters import ParameterNumber
from processing.core.parameters import ParameterExtent
from processing.core.parameters import (Parameter,
ParameterNumber,
ParameterExtent,
_splitParameterOptions
)
from processing.core.outputs import OutputRaster
from processing.tools import dataobjects

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


class IdwInterpolationZValue(GeoAlgorithm):
class IdwInterpolation(GeoAlgorithm):

INPUT_LAYER = 'INPUT_LAYER'
LAYER_TYPE = 'LAYER_TYPE'
INTERPOLATION_DATA = 'INTERPOLATION_DATA'
DISTANCE_COEFFICIENT = 'DISTANCE_COEFFICIENT'
COLUMNS = 'COLUMNS'
ROWS = 'ROWS'
Expand All @@ -63,20 +63,61 @@ def getIcon(self):
return QIcon(os.path.join(pluginPath, 'images', 'interpolation.png'))

def defineCharacteristics(self):
self.name, self.i18n_name = self.trAlgorithm('IDW interpolation (using Z-values)')
self.name, self.i18n_name = self.trAlgorithm('IDW interpolation')
self.group, self.i18n_group = self.trAlgorithm('Interpolation')

self.TYPES = [self.tr('Points'),
self.tr('Structure lines'),
self.tr('Break lines')
]

self.addParameter(ParameterVector(self.INPUT_LAYER,
self.tr('Vector layer')))
self.addParameter(ParameterSelection(self.LAYER_TYPE,
self.tr('Type'),
self.TYPES,
0))
class ParameterInterpolationData(Parameter):
default_metadata = {
'widget_wrapper': 'processing.algs.qgis.ui.InterpolationDataWidget.InterpolationDataWidgetWrapper'
}

def __init__(self, name='', description=''):
Parameter.__init__(self, name, description)

def setValue(self, value):
if value is None:
if not self.optional:
return False
self.value = None
return True

if value == '':
if not self.optional:
return False

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

def getValueAsCommandLineParameter(self):
return '"{}"'.format(self.value)

def getAsScriptCode(self):
param_type = ''
param_type += 'interpolation data '
return '##' + self.name + '=' + param_type

@classmethod
def fromScriptCode(self, line):
isOptional, name, definition = _splitParameterOptions(line)
descName = _createDescriptiveName(name)
parent = definition.lower().strip()[len('interpolation data') + 1:]
return ParameterInterpolationData(name, description, parent)

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

self.addParameter(ParameterInterpolationData(self.INTERPOLATION_DATA,
self.tr('Input layer(s)')))
self.addParameter(ParameterNumber(self.DISTANCE_COEFFICIENT,
self.tr('Distance coefficient P'),
0.0, 99.99, 2.0))
Expand All @@ -98,9 +139,7 @@ def defineCharacteristics(self):
self.tr('Interpolated')))

def processAlgorithm(self, progress):
layer = dataobjects.getObjectFromUri(
self.getParameterValue(self.INPUT_LAYER))
layerType = self.getParameterValue(self.LAYER_TYPE)
interpolationData = self.getParameterValue(self.INTERPOLATION_DATA)
coefficient = self.getParameterValue(self.DISTANCE_COEFFICIENT)
columns = self.getParameterValue(self.COLUMNS)
rows = self.getParameterValue(self.ROWS)
Expand All @@ -109,29 +148,32 @@ def processAlgorithm(self, progress):
extent = self.getParameterValue(self.EXTENT).split(',')
output = self.getOutputValue(self.OUTPUT_LAYER)

if not QgsWkbTypes.hasZ(layer.wkbType()):
if interpolationData is None:
raise GeoAlgorithmExecutionException(
self.tr('Geometries in input layer does not have Z coordinates.'))
self.tr('You need to specify at least one input layer.'))

xMin = float(extent[0])
xMax = float(extent[1])
yMin = float(extent[2])
yMax = float(extent[3])
bbox = QgsRectangle(xMin, yMin, xMax, yMax)

layerData = QgsInterpolator.LayerData()
layerData.vectorLayer = layer
layerData.zCoordInterpolation = True
layerData.interpolationAttribute = -1

if layerType == 0:
layerData.mInputType = QgsInterpolator.POINTS
elif layerType == 1:
layerData.mInputType = QgsInterpolator.STRUCTURE_LINES
else:
layerData.mInputType = QgsInterpolator.BREAK_LINES

interpolator = QgsIDWInterpolator([layerData])
layerData = []
for row in interpolationData.split(';'):
v = row.split(',')
data = QgsInterpolator.LayerData()
data.vectorLayer = dataobjects.getObjectFromUri(v[0])
data.zCoordInterpolation = bool(v[1])
data.interpolationAttribute = int(v[2])
if v[3] == '0':
data.mInputType = QgsInterpolator.POINTS
elif v[3] == '1':
data.mInputType = QgsInterpolator.STRUCTURE_LINES
else:
data.mInputType = QgsInterpolator.BREAK_LINES
layerData.append(data)

interpolator = QgsIDWInterpolator(layerData)
interpolator.setDistanceCoefficient(coefficient)

writer = QgsGridFileWriter(interpolator,
Expand Down
147 changes: 0 additions & 147 deletions python/plugins/processing/algs/qgis/IdwInterpolationAttribute.py

This file was deleted.

9 changes: 3 additions & 6 deletions python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,8 @@
from .Ruggedness import Ruggedness
from .Hillshade import Hillshade
from .Relief import Relief
from .IdwInterpolationZValue import IdwInterpolationZValue
from .IdwInterpolationAttribute import IdwInterpolationAttribute
from .TinInterpolationZValue import TinInterpolationZValue
from .TinInterpolationAttribute import TinInterpolationAttribute
from .IdwInterpolation import IdwInterpolation
from .TinInterpolation import TinInterpolation
from .ZonalStatisticsQgis import ZonalStatisticsQgis
from .RemoveNullGeometry import RemoveNullGeometry
from .ExtendLines import ExtendLines
Expand Down Expand Up @@ -248,8 +246,7 @@ def __init__(self):
SingleSidedBuffer(), PointsAlongGeometry(),
Aspect(), Slope(), Ruggedness(), Hillshade(),
Relief(), ZonalStatisticsQgis(),
IdwInterpolationZValue(), IdwInterpolationAttribute(),
TinInterpolationZValue(), TinInterpolationAttribute(),
IdwInterpolation(), TinInterpolation(),
RemoveNullGeometry(), ExtractByExpression(),
ExtendLines(), ExtractSpecificNodes(),
GeometryByExpression(), SnapGeometriesToLayer(),
Expand Down
Loading

0 comments on commit 7dde862

Please sign in to comment.