Skip to content

Commit 340cf93

Browse files
committedJul 18, 2017
[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
1 parent b9f2259 commit 340cf93

15 files changed

+213
-0
lines changed
 

‎python/plugins/processing/algs/help/qgis.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,13 +503,22 @@ qgis:selectbyexpression: >
503503
qgis:selectbylocation: >
504504
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.
505505

506+
qgis:setmvalue: >
507+
This algorithm sets the M value for geometries in a layer.
508+
509+
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.
506510

507511
qgis:setstyleforrasterlayer: >
508512
This algorithm sets the style of a raster layer. The style must be defined in a QML file.
509513

510514
qgis:setstyleforvectorlayer: >
511515
This algorithm sets the style of a vector layer. The style must be defined in a QML file.
512516

517+
qgis:setzvalue: >
518+
This algorithm sets the Z value for geometries in a layer.
519+
520+
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.
521+
513522
qgis:simplifygeometries: >
514523
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.
515524

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@
8989
from .SelectByExpression import SelectByExpression
9090
from .ServiceAreaFromLayer import ServiceAreaFromLayer
9191
from .ServiceAreaFromPoint import ServiceAreaFromPoint
92+
from .SetMValue import SetMValue
93+
from .SetZValue import SetZValue
9294
from .ShortestPathLayerToPoint import ShortestPathLayerToPoint
9395
from .ShortestPathPointToLayer import ShortestPathPointToLayer
9496
from .ShortestPathPointToPoint import ShortestPathPointToPoint
@@ -276,6 +278,8 @@ def getAlgs(self):
276278
SelectByExpression(),
277279
ServiceAreaFromLayer(),
278280
ServiceAreaFromPoint(),
281+
SetMValue(),
282+
SetZValue(),
279283
ShortestPathLayerToPoint(),
280284
ShortestPathPointToLayer(),
281285
ShortestPathPointToPoint(),
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
SetMValue.py
6+
--------------
7+
Date : July 2017
8+
Copyright : (C) 2017 by Nyall Dawson
9+
Email : nyall dot dawson at gmail dot com
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
20+
__author__ = 'Nyall Dawson'
21+
__date__ = 'July 2017'
22+
__copyright__ = '(C) 2017, Nyall Dawson'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive323
25+
26+
__revision__ = '$Format:%H$'
27+
28+
import os
29+
30+
from qgis.core import (QgsGeometry,
31+
QgsWkbTypes,
32+
QgsProcessingParameterNumber)
33+
34+
35+
from processing.algs.qgis.QgisAlgorithm import QgisFeatureBasedAlgorithm
36+
37+
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
38+
39+
40+
class SetMValue(QgisFeatureBasedAlgorithm):
41+
42+
M_VALUE = 'M_VALUE'
43+
44+
def group(self):
45+
return self.tr('Vector geometry tools')
46+
47+
def __init__(self):
48+
super().__init__()
49+
self.m_value = 0
50+
51+
def name(self):
52+
return 'setmvalue'
53+
54+
def displayName(self):
55+
return self.tr('Set M Value')
56+
57+
def outputName(self):
58+
return self.tr('M Added')
59+
60+
def tags(self):
61+
return self.tr('set,add,m,measure,values').split(',')
62+
63+
def initParameters(self, config=None):
64+
self.addParameter(QgsProcessingParameterNumber(self.M_VALUE,
65+
self.tr('M Value'), QgsProcessingParameterNumber.Double, defaultValue=0.0))
66+
67+
def outputWkbType(self, inputWkb):
68+
return QgsWkbTypes.addM(inputWkb)
69+
70+
def prepareAlgorithm(self, parameters, context, feedback):
71+
self.m_value = self.parameterAsDouble(parameters, self.M_VALUE, context)
72+
return True
73+
74+
def processFeature(self, feature, feedback):
75+
input_geometry = feature.geometry()
76+
if input_geometry:
77+
new_geom = input_geometry.geometry().clone()
78+
if QgsWkbTypes.hasM(new_geom.wkbType()):
79+
# addMValue won't alter existing M values, so drop them first
80+
new_geom.dropMValue()
81+
82+
new_geom.addMValue(self.m_value)
83+
84+
feature.setGeometry(QgsGeometry(new_geom))
85+
86+
return True
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
SetZValue.py
6+
--------------
7+
Date : July 2017
8+
Copyright : (C) 2017 by Nyall Dawson
9+
Email : nyall dot dawson at gmail dot com
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
20+
__author__ = 'Nyall Dawson'
21+
__date__ = 'July 2017'
22+
__copyright__ = '(C) 2017, Nyall Dawson'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive323
25+
26+
__revision__ = '$Format:%H$'
27+
28+
import os
29+
30+
from qgis.core import (QgsGeometry,
31+
QgsWkbTypes,
32+
QgsProcessingParameterNumber)
33+
34+
35+
from processing.algs.qgis.QgisAlgorithm import QgisFeatureBasedAlgorithm
36+
37+
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
38+
39+
40+
class SetZValue(QgisFeatureBasedAlgorithm):
41+
42+
Z_VALUE = 'Z_VALUE'
43+
44+
def group(self):
45+
return self.tr('Vector geometry tools')
46+
47+
def __init__(self):
48+
super().__init__()
49+
self.z_value = 0
50+
51+
def name(self):
52+
return 'setzvalue'
53+
54+
def displayName(self):
55+
return self.tr('Set Z Value')
56+
57+
def outputName(self):
58+
return self.tr('Z Added')
59+
60+
def tags(self):
61+
return self.tr('set,add,z,25d,3d,values').split(',')
62+
63+
def initParameters(self, config=None):
64+
self.addParameter(QgsProcessingParameterNumber(self.Z_VALUE,
65+
self.tr('Z Value'), QgsProcessingParameterNumber.Double, defaultValue=0.0))
66+
67+
def outputWkbType(self, inputWkb):
68+
return QgsWkbTypes.addZ(inputWkb)
69+
70+
def prepareAlgorithm(self, parameters, context, feedback):
71+
self.z_value = self.parameterAsDouble(parameters, self.Z_VALUE, context)
72+
return True
73+
74+
def processFeature(self, feature, feedback):
75+
input_geometry = feature.geometry()
76+
if input_geometry:
77+
new_geom = input_geometry.geometry().clone()
78+
if QgsWkbTypes.hasZ(new_geom.wkbType()):
79+
# addZValue won't alter existing Z values, so drop them first
80+
new_geom.dropZValue()
81+
82+
new_geom.addZValue(self.z_value)
83+
84+
feature.setGeometry(QgsGeometry(new_geom))
85+
86+
return True
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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.

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,30 @@ tests:
522522
name: expected/multiline_boundary.gml
523523
type: vector
524524

525+
- algorithm: qgis:setmvalue
526+
name: Set M Value
527+
params:
528+
INPUT:
529+
name: points.gml
530+
type: vector
531+
M_VALUE: 7
532+
results:
533+
OUTPUT:
534+
name: expected/set_m_value.shp
535+
type: vector
536+
537+
- algorithm: qgis:setzvalue
538+
name: Set Z Value
539+
params:
540+
INPUT:
541+
name: points.gml
542+
type: vector
543+
Z_VALUE: 6
544+
results:
545+
OUTPUT:
546+
name: expected/set_z_value.shp
547+
type: vector
548+
525549
- algorithm: qgis:pointonsurface
526550
name: Point on polygon surface
527551
params:

0 commit comments

Comments
 (0)
Please sign in to comment.