[FEATURE] New algorithms to add Z/M values to existing geometries

Allows upgrading geometries to include these dimensions, or
overwriting any existing Z/M values with a new value.

Intended mostly as a test run for QgsProcessingFeatureBasedAlgorithm
This commit is contained in:
Nyall Dawson 2017-07-17 11:31:34 +10:00
parent b9f225905a
commit 340cf93f93
15 changed files with 213 additions and 0 deletions

View File

@ -503,6 +503,10 @@ qgis:selectbyexpression: >
qgis:selectbylocation: >
This algorithm creates a selection in a vector layer. The criteria for selecting features is based on the spatial relationship between each feature and the features in an additional layer.
qgis:setmvalue: >
This algorithm sets the M value for geometries in a layer.
If M values already exist in the layer, they will be overwritten with the new value. If no M values exist, the geometry will be upgraded to include M values and the specified value used as the initial M value for all geometries.
qgis:setstyleforrasterlayer: >
This algorithm sets the style of a raster layer. The style must be defined in a QML file.
@ -510,6 +514,11 @@ qgis:setstyleforrasterlayer: >
qgis:setstyleforvectorlayer: >
This algorithm sets the style of a vector layer. The style must be defined in a QML file.
qgis:setzvalue: >
This algorithm sets the Z value for geometries in a layer.
If Z values already exist in the layer, they will be overwritten with the new value. If no Z values exist, the geometry will be upgraded to include Z values and the specified value used as the initial Z value for all geometries.
qgis:simplifygeometries: >
This algorithm simplifies the geometries in a line or polygon layer. It creates a new layer with the same features as the ones in the input layer, but with geometries containing a lower number of vertices.

View File

@ -89,6 +89,8 @@ from .SelectByAttribute import SelectByAttribute
from .SelectByExpression import SelectByExpression
from .ServiceAreaFromLayer import ServiceAreaFromLayer
from .ServiceAreaFromPoint import ServiceAreaFromPoint
from .SetMValue import SetMValue
from .SetZValue import SetZValue
from .ShortestPathLayerToPoint import ShortestPathLayerToPoint
from .ShortestPathPointToLayer import ShortestPathPointToLayer
from .ShortestPathPointToPoint import ShortestPathPointToPoint
@ -276,6 +278,8 @@ class QGISAlgorithmProvider(QgsProcessingProvider):
SelectByExpression(),
ServiceAreaFromLayer(),
ServiceAreaFromPoint(),
SetMValue(),
SetZValue(),
ShortestPathLayerToPoint(),
ShortestPathPointToLayer(),
ShortestPathPointToPoint(),

View File

@ -0,0 +1,86 @@
# -*- coding: utf-8 -*-
"""
***************************************************************************
SetMValue.py
--------------
Date : July 2017
Copyright : (C) 2017 by Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""
__author__ = 'Nyall Dawson'
__date__ = 'July 2017'
__copyright__ = '(C) 2017, Nyall Dawson'
# This will get replaced with a git SHA1 when you do a git archive323
__revision__ = '$Format:%H$'
import os
from qgis.core import (QgsGeometry,
QgsWkbTypes,
QgsProcessingParameterNumber)
from processing.algs.qgis.QgisAlgorithm import QgisFeatureBasedAlgorithm
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
class SetMValue(QgisFeatureBasedAlgorithm):
M_VALUE = 'M_VALUE'
def group(self):
return self.tr('Vector geometry tools')
def __init__(self):
super().__init__()
self.m_value = 0
def name(self):
return 'setmvalue'
def displayName(self):
return self.tr('Set M Value')
def outputName(self):
return self.tr('M Added')
def tags(self):
return self.tr('set,add,m,measure,values').split(',')
def initParameters(self, config=None):
self.addParameter(QgsProcessingParameterNumber(self.M_VALUE,
self.tr('M Value'), QgsProcessingParameterNumber.Double, defaultValue=0.0))
def outputWkbType(self, inputWkb):
return QgsWkbTypes.addM(inputWkb)
def prepareAlgorithm(self, parameters, context, feedback):
self.m_value = self.parameterAsDouble(parameters, self.M_VALUE, context)
return True
def processFeature(self, feature, feedback):
input_geometry = feature.geometry()
if input_geometry:
new_geom = input_geometry.geometry().clone()
if QgsWkbTypes.hasM(new_geom.wkbType()):
# addMValue won't alter existing M values, so drop them first
new_geom.dropMValue()
new_geom.addMValue(self.m_value)
feature.setGeometry(QgsGeometry(new_geom))
return True

View File

@ -0,0 +1,86 @@
# -*- coding: utf-8 -*-
"""
***************************************************************************
SetZValue.py
--------------
Date : July 2017
Copyright : (C) 2017 by Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""
__author__ = 'Nyall Dawson'
__date__ = 'July 2017'
__copyright__ = '(C) 2017, Nyall Dawson'
# This will get replaced with a git SHA1 when you do a git archive323
__revision__ = '$Format:%H$'
import os
from qgis.core import (QgsGeometry,
QgsWkbTypes,
QgsProcessingParameterNumber)
from processing.algs.qgis.QgisAlgorithm import QgisFeatureBasedAlgorithm
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
class SetZValue(QgisFeatureBasedAlgorithm):
Z_VALUE = 'Z_VALUE'
def group(self):
return self.tr('Vector geometry tools')
def __init__(self):
super().__init__()
self.z_value = 0
def name(self):
return 'setzvalue'
def displayName(self):
return self.tr('Set Z Value')
def outputName(self):
return self.tr('Z Added')
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))
def outputWkbType(self, inputWkb):
return QgsWkbTypes.addZ(inputWkb)
def prepareAlgorithm(self, parameters, context, feedback):
self.z_value = self.parameterAsDouble(parameters, self.Z_VALUE, context)
return True
def processFeature(self, feature, feedback):
input_geometry = feature.geometry()
if input_geometry:
new_geom = input_geometry.geometry().clone()
if QgsWkbTypes.hasZ(new_geom.wkbType()):
# addZValue won't alter existing Z values, so drop them first
new_geom.dropZValue()
new_geom.addZValue(self.z_value)
feature.setGeometry(QgsGeometry(new_geom))
return True

Binary file not shown.

View File

@ -0,0 +1 @@
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]

View File

@ -0,0 +1 @@
GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1 @@
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]

View File

@ -0,0 +1 @@
GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]

Binary file not shown.

Binary file not shown.

View File

@ -522,6 +522,30 @@ tests:
name: expected/multiline_boundary.gml
type: vector
- algorithm: qgis:setmvalue
name: Set M Value
params:
INPUT:
name: points.gml
type: vector
M_VALUE: 7
results:
OUTPUT:
name: expected/set_m_value.shp
type: vector
- algorithm: qgis:setzvalue
name: Set Z Value
params:
INPUT:
name: points.gml
type: vector
Z_VALUE: 6
results:
OUTPUT:
name: expected/set_z_value.shp
type: vector
- algorithm: qgis:pointonsurface
name: Point on polygon surface
params: