Skip to content
Permalink
Browse files

[processing] Allow data defined z value for "set z value" algorithm

Allows easy setting of z from an existing height attribute!
  • Loading branch information
nyalldawson committed Dec 2, 2017
1 parent b782fab commit 4b464f3d68b32644da0e5ff2f3e658ba929fb157
Showing with 26 additions and 4 deletions.
  1. +26 −4 python/plugins/processing/algs/qgis/SetZValue.py
@@ -29,7 +29,10 @@

from qgis.core import (QgsGeometry,
QgsWkbTypes,
QgsProcessingParameterNumber)
QgsPropertyDefinition,
QgsProcessingParameters,
QgsProcessingParameterNumber,
QgsProcessingFeatureSource)


from processing.algs.qgis.QgisAlgorithm import QgisFeatureBasedAlgorithm
@@ -47,6 +50,9 @@ def group(self):
def __init__(self):
super().__init__()
self.z_value = 0
self.dynamic_z = False
self.z_property = None
self.expression_context = None

def name(self):
return 'setzvalue'
@@ -61,14 +67,26 @@ def tags(self):
return self.tr('set,add,z,25d,3d,values').split(',')

def initParameters(self, config=None):
self.addParameter(QgsProcessingParameterNumber(self.Z_VALUE,
self.tr('Z Value'), QgsProcessingParameterNumber.Double, defaultValue=0.0))
z_param = QgsProcessingParameterNumber(self.Z_VALUE,
self.tr('Z Value'), QgsProcessingParameterNumber.Double, defaultValue=0.0)
z_param.setIsDynamic(True)
z_param.setDynamicLayerParameterName('INPUT')
z_param.setDynamicPropertyDefinition(QgsPropertyDefinition(self.Z_VALUE, self.tr("Z Value"), QgsPropertyDefinition.Double))
self.addParameter(z_param)

def outputWkbType(self, inputWkb):
return QgsWkbTypes.addZ(inputWkb)

def prepareAlgorithm(self, parameters, context, feedback):
self.z_value = self.parameterAsDouble(parameters, self.Z_VALUE, context)
self.dynamic_z = QgsProcessingParameters.isDynamic(parameters, self.Z_VALUE)
if self.dynamic_z:
self.z_property = parameters[self.Z_VALUE]
source = self.parameterAsSource(parameters, 'INPUT', context)
if not isinstance(source, QgsProcessingFeatureSource):
source = None
self.expression_context = self.createExpressionContext(parameters, context, source)
self.z_property.prepare(self.expression_context)
return True

def processFeature(self, feature, feedback):
@@ -79,7 +97,11 @@ def processFeature(self, feature, feedback):
# addZValue won't alter existing Z values, so drop them first
new_geom.dropZValue()

new_geom.addZValue(self.z_value)
z = self.z_value
if self.dynamic_z:
self.expression_context.setFeature(feature)
z, ok = self.z_property.valueAsDouble(self.expression_context, z)
new_geom.addZValue(z)

feature.setGeometry(QgsGeometry(new_geom))

0 comments on commit 4b464f3

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