-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClimb_algorithm.py
375 lines (345 loc) · 14.6 KB
/
Climb_algorithm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# -*- coding: utf-8 -*-
"""
/***************************************************************************
Climb
A QGIS plugin
-------------------
begin : 2019-03-01
copyright : (C) 2019 by Håvard Tveite
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* 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__ = 'Håvard Tveite'
__date__ = '2019-03-01'
__copyright__ = '(C) 2019 by Håvard Tveite'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
import math
from PyQt5.QtCore import QCoreApplication, QVariant
from qgis.core import (QgsProcessing,
QgsFeatureSink,
QgsProcessingAlgorithm,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterFeatureSink,
QgsProcessingParameterRasterLayer,
QgsProcessingParameterBand,
QgsProcessingOutputNumber,
QgsWkbTypes,
QgsFields,
QgsField)
from qgis.utils import Qgis
import processing
class ClimbAlgorithm(QgsProcessingAlgorithm):
"""
Takes a line vector layer and calculates climb.
Returns the total climb of the lines and a line vector layer
that includes the climb for each line.
"""
# Constants used to refer to parameters and outputs. They will be
# used when calling the algorithm from another algorithm, or when
# calling from the QGIS console.
OUTPUT = 'OUTPUT'
INPUT = 'INPUT'
DEMFORZ = 'DEMFORZ'
BANDDEM = 'BANDDEM'
TOTALCLIMB = 'TOTALCLIMB'
TOTALDESCENT = 'TOTALDESCENT'
MINELEVATION = 'MINELEVATION'
MAXELEVATION = 'MAXELEVATION'
CLIMBATTRIBUTE = 'climb'
DESCENTATTRIBUTE = 'descent'
MINELEVATTRIBUTE = 'minelev'
MAXELEVATTRIBUTE = 'maxelev'
# Override checking of parameters
def checkParameterValues(self, parameters, context):
super().checkParameterValues(parameters, context)
source = self.parameterAsSource(parameters, self.INPUT, context)
# Check for Z values
hasZ = QgsWkbTypes.hasZ(source.wkbType())
if not hasZ:
if Qgis.QGIS_VERSION_INT < 30405:
return [False, 'The line layer has no Z values, ' +
'so a DEM is needed, but extracting Z ' +
'values from the DEM requires QGIS ' +
'3.4.5 or later. Your QGIS version is ' +
Qgis.QGIS_VERSION +
' - sorry about that!']
demraster = self.parameterAsRasterLayer(parameters,
self.DEMFORZ, context)
if not demraster:
return [False, 'The line layer has no Z values - ' +
'a DEM is needed']
else:
return [True, 'OK']
else:
return [True, 'OK']
def initAlgorithm(self, config):
"""
Here we define the inputs and output of the algorithm, along
with some other properties.
"""
# We add the input vector features source. Has to be of type
# line.
self.addParameter(
QgsProcessingParameterFeatureSource(
self.INPUT,
self.tr('Input (line) layer'),
[QgsProcessing.TypeVectorLine]
)
)
# DEM for extracting the z value
self.addParameter(
QgsProcessingParameterRasterLayer(
self.DEMFORZ,
self.tr('DEM (to get Z values)'),
optional=True
)
)
# Raster band containing the z value (DEM)
self.addParameter(
QgsProcessingParameterBand(
self.BANDDEM,
self.tr('Band containing the Z values)'),
None,
self.DEMFORZ
)
)
# We add a feature sink in which to store our processed features.
self.addParameter(
QgsProcessingParameterFeatureSink(
self.OUTPUT,
self.tr('Climb layer')
)
)
# Output number for total climb
self.addOutput(
QgsProcessingOutputNumber(
self.TOTALCLIMB,
self.tr('Total climb')
)
)
# Output number for total descent
self.addOutput(
QgsProcessingOutputNumber(
self.TOTALDESCENT,
self.tr('Total descent')
)
)
# Output number for minimum elevation
self.addOutput(
QgsProcessingOutputNumber(
self.MINELEVATION,
self.tr('Minimum elevation')
)
)
# Output number for maximum elevation
self.addOutput(
QgsProcessingOutputNumber(
self.MAXELEVATION,
self.tr('Maximum elevation')
)
)
def processAlgorithm(self, parameters, context, feedback):
"""
Here is where the processing itself takes place.
"""
# Get the feature source
source = self.parameterAsSource(parameters, self.INPUT, context)
# Get the number of features (for the progress bar)
fcount = source.featureCount()
# Check for Z values
hasZ = QgsWkbTypes.hasZ(source.wkbType())
# Get the DEM
demraster = self.parameterAsRasterLayer(parameters,
self.DEMFORZ,
context)
# Add fields to the output layer
# Add fields from the input layer
thefields = QgsFields()
climbindex = -1
descentindex = -1
fieldnumber = 0
# Skip fields with names that are equal to the generated ones
for field in source.fields():
if str(field.name()) == str(self.CLIMBATTRIBUTE):
feedback.pushInfo("Warning: existing " +
str(self.CLIMBATTRIBUTE) +
" attribute found and removed")
climbindex = fieldnumber
elif str(field.name()) == str(self.DESCENTATTRIBUTE):
feedback.pushInfo("Warning: existing " +
str(self.DESCENTATTRIBUTE) +
" attribute found and removed")
descentindex = fieldnumber
else:
thefields.append(field)
fieldnumber = fieldnumber + 1
# Create new fields for climb, descent, minimum elevation
# and maximum elevation
thefields.append(QgsField(self.CLIMBATTRIBUTE, QVariant.Double))
thefields.append(QgsField(self.DESCENTATTRIBUTE, QVariant.Double))
thefields.append(QgsField(self.MINELEVATTRIBUTE, QVariant.Double))
thefields.append(QgsField(self.MAXELEVATTRIBUTE, QVariant.Double))
# If a DEM is provided, use it to extract z values
if demraster:
# Get the raster band with the z value
demband = self.parameterAsString(parameters,
self.BANDDEM,
context)
feedback.pushInfo("Adding Z values from DEM using " +
"Drape (setzfromraster) ...")
# Add the z values
withz = processing.run("native:setzfromraster",
{"INPUT": parameters[self.INPUT],
"RASTER": demraster,
"BAND": demband,
"OUTPUT": "memory:"},
context=context,
feedback=feedback,
is_child_algorithm=True)["OUTPUT"]
feedback.pushInfo("Z values added.")
layerwithz = context.temporaryLayerStore().mapLayer(withz)
else:
layerwithz = source
# Retrieve the feature sink. The 'dest_id' variable is used
# to uniquely identify the feature sink, and must be included
# in the dictionary returned by the processAlgorithm
# function.
(sink, dest_id) = self.parameterAsSink(parameters,
self.OUTPUT,
context, thefields,
layerwithz.wkbType(),
source.sourceCrs())
# get features from source (with z values)
features = layerwithz.getFeatures()
totalclimb = 0
totaldescent = 0
minelevation = float('Infinity')
maxelevation = float('-Infinity')
for current, feature in enumerate(features):
# Stop the algorithm if cancelled
if feedback.isCanceled():
break
climb = 0
descent = 0
minelev = float('Infinity')
maxelev = float('-Infinity')
# In case of multigeometries we need to do the parts
for part in feature.geometry().constParts():
# Calculate the climb
first = True
zval = 0
for v in part.vertices():
zval = v.z()
# Check if we do not have a valid z value
if math.isnan(zval):
feedback.pushInfo("Missing Z value")
continue
if first:
prevz = zval
minelev = zval
maxelev = zval
first = False
else:
diff = zval - prevz
if diff > 0:
climb = climb + diff
else:
descent = descent - diff
if minelev > zval:
minelev = zval
if maxelev < zval:
maxelev = zval
prevz = zval
totalclimb = totalclimb + climb
totaldescent = totaldescent + descent
# Set the attribute values
attrs = feature.attributes()
outattrs = []
attrindex = 0
for attr in attrs:
# Skip attributes from the input layer that had names
# that were equal to the generated ones
if not (attrindex == climbindex or
attrindex == descentindex):
outattrs.append(attr)
attrindex = attrindex + 1
# feature.setAttributes(outattrs + [climb, descent])
feature.setAttributes(outattrs +
[climb, descent, minelev, maxelev])
# Add a feature to the sink
sink.addFeature(feature, QgsFeatureSink.FastInsert)
if minelevation > minelev:
minelevation = minelev
if maxelevation < maxelev:
maxelevation = maxelev
# Update the progress bar
if fcount > 0:
feedback.setProgress(int(100 * current / fcount))
# Return the results
return {self.OUTPUT: dest_id, self.TOTALCLIMB: totalclimb,
self.TOTALDESCENT: totaldescent,
self.MINELEVATION: minelevation,
self.MAXELEVATION: maxelevation}
def shortHelpString(self):
return("The total climb and descent along the line "
"geometries of the input line layer are calculated "
"using the Z values for the points making up "
"the lines.<br> "
"Z values can be provided by the line geometries or "
"a DEM (by using the <i>Drape (set z-value from "
"raster)</i> algorithm to assign Z values to the "
"points that make up the lines).<br> "
"If a DEM is specified, Z values will be taken from "
"the DEM and not the line layer.<br>"
"The output layer (OUTPUT) has extra fields "
"(<i>climb</i> and <i>descent</i>) "
"that shall contain the total climb "
"and the total descent for each line geometry, "
"and extra fields (<i>minelev</i> and <i>maxelev</i>) "
"that shall contain the minimum and maximum elevation "
"of each line geometry."
"If these fields exist in the input layer, the "
"original fields will be removed.<br>"
"The layer totals are returned in the TOTALCLIMB, "
"TOTALDESCENT, MINELEVATION and MAXELEVATION output "
"parameters.")
def name(self):
"""
Returns the algorithm name, used for identifying the
algorithm. This string must not be localised. Names should
contain lowercase alphanumeric characters only and no spaces
or other formatting characters.
"""
return 'climbalongline'
def displayName(self):
"""
Returns the translated algorithm name (for display).
"""
return self.tr("Climb along line")
def group(self):
"""
Returns the name of the group this algorithm belongs to.
This string should be localised.
"""
return self.tr('Vector analysis')
def groupId(self):
"""
Returns the unique ID of the group this algorithm belongs to.
This string must not be localised.
Group id should contain lowercase alphanumeric characters
only and no spaces or other formatting characters.
"""
return 'vectoranalysis'
def tr(self, string):
return QCoreApplication.translate('Processing', string)
def createInstance(self):
return ClimbAlgorithm()