-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature[ Processing Climb along lines algorithm
- ltr-3_40
- ltr-3_34
- ltr-3_28
- ltr-3_22
- ltr-3_16
- ltr-3_10
- final-3_40_2
- final-3_40_1
- final-3_40_0
- final-3_38_3
- final-3_38_2
- final-3_38_1
- final-3_38_0
- final-3_36_3
- final-3_36_2
- final-3_36_1
- final-3_36_0
- final-3_34_14
- final-3_34_13
- final-3_34_12
- final-3_34_11
- final-3_34_10
- final-3_34_9
- final-3_34_8
- final-3_34_7
- final-3_34_6
- final-3_34_5
- final-3_34_4
- final-3_34_3
- final-3_34_2
- final-3_34_1
- final-3_34_0
- final-3_32_3
- final-3_32_2
- final-3_32_1
- final-3_32_0
- final-3_30_3
- final-3_30_2
- final-3_30_1
- final-3_30_0
- final-3_28_15
- final-3_28_14
- final-3_28_13
- final-3_28_12
- final-3_28_11
- final-3_28_10
- final-3_28_9
- final-3_28_8
- final-3_28_7
- final-3_28_6
- final-3_28_5
- final-3_28_4
- final-3_28_3
- final-3_28_2
- final-3_28_1
- final-3_28_0
- final-3_26_3
- final-3_26_2
- final-3_26_1
- final-3_26_0
- final-3_24_3
- final-3_24_2
- final-3_24_1
- final-3_24_0
- final-3_22_16
- final-3_22_15
- final-3_22_14
- final-3_22_13
- final-3_22_12
- final-3_22_11
- final-3_22_10
- final-3_22_9
- final-3_22_8
- final-3_22_7
- final-3_22_6
- final-3_22_5
- final-3_22_4
- final-3_22_3
- final-3_22_2
- final-3_22_1
- final-3_22_0
- final-3_20_3
- final-3_20_2
- final-3_20_1
- final-3_20_0
- final-3_18_3
- final-3_18_2
- final-3_18_1
- final-3_18_0
- final-3_16_16
- final-3_16_15
- final-3_16_14
- final-3_16_13
- final-3_16_12
- final-3_16_11
- final-3_16_10
- final-3_16_9
- final-3_16_8
- final-3_16_7
- final-3_16_6
- final-3_16_5
- final-3_16_4
- final-3_16_3
- final-3_16_2
- final-3_16_1
- final-3_16_0
- final-3_14_16
- final-3_14_15
- final-3_14_1
- final-3_14_0
- final-3_12_3
- final-3_12_2
- final-3_12_1
- final-3_12_0
- final-3_10_14
- final-3_10_13
- final-3_10_12
- final-3_10_11
- final-3_10_10
- final-3_10_9
- final-3_10_8
- final-3_10_7
- final-3_10_6
- final-3_10_5
- final-3_10_4
- final-3_10_3
- final-3_10_2
- final-3_10_1
- final-3_10_0
Showing
15 changed files
with
269 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,243 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
/*************************************************************************** | ||
Climb | ||
A QGIS plugin | ||
------------------- | ||
begin : 2019-03-01 | ||
copyright : (C) 2019 by Håvard Tveite | ||
email : havard.tveite@nmbu.no | ||
***************************************************************************/ | ||
/*************************************************************************** | ||
* * | ||
* 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 os | ||
import math | ||
|
||
from qgis.PyQt.QtGui import QIcon | ||
from qgis.PyQt.QtCore import QVariant | ||
|
||
from qgis.core import (QgsProcessing, | ||
QgsFeatureSink, | ||
QgsProcessingAlgorithm, | ||
QgsProcessingParameterFeatureSource, | ||
QgsProcessingParameterFeatureSink, | ||
QgsProcessingOutputNumber, | ||
QgsWkbTypes, | ||
QgsFields, | ||
QgsField) | ||
|
||
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm | ||
|
||
|
||
|
||
class Climb(QgisAlgorithm): | ||
|
||
INPUT = 'INPUT' | ||
OUTPUT = 'OUTPUT' | ||
|
||
TOTALCLIMB = 'TOTALCLIMB' | ||
TOTALDESCENT = 'TOTALDESCENT' | ||
MINELEVATION = 'MINELEVATION' | ||
MAXELEVATION = 'MAXELEVATION' | ||
CLIMBATTRIBUTE = 'climb' | ||
DESCENTATTRIBUTE = 'descent' | ||
MINELEVATTRIBUTE = 'minelev' | ||
MAXELEVATTRIBUTE = 'maxelev' | ||
|
||
def name(self): | ||
return 'climbalongline' | ||
|
||
def displayName(self): | ||
return self.tr("Climb Along Line") | ||
|
||
def group(self): | ||
return self.tr('Vector analysis') | ||
|
||
def groupId(self): | ||
return 'vectoranalysis' | ||
|
||
def __init__(self): | ||
super().__init__() | ||
|
||
def initAlgorithm(self, config=None): | ||
self.addParameter( | ||
QgsProcessingParameterFeatureSource( | ||
self.INPUT, | ||
self.tr('Input (line) layer'), | ||
[QgsProcessing.TypeVectorLine] | ||
) | ||
) | ||
|
||
self.addParameter( | ||
QgsProcessingParameterFeatureSink( | ||
self.OUTPUT, | ||
self.tr('Climb layer') | ||
) | ||
) | ||
|
||
self.addOutput( | ||
QgsProcessingOutputNumber( | ||
self.TOTALCLIMB, | ||
self.tr('Total climb') | ||
) | ||
) | ||
|
||
self.addOutput( | ||
QgsProcessingOutputNumber( | ||
self.TOTALDESCENT, | ||
self.tr('Total descent') | ||
) | ||
) | ||
|
||
self.addOutput( | ||
QgsProcessingOutputNumber( | ||
self.MINELEVATION, | ||
self.tr('Minimum elevation') | ||
) | ||
) | ||
|
||
self.addOutput( | ||
QgsProcessingOutputNumber( | ||
self.MAXELEVATION, | ||
self.tr('Maximum elevation') | ||
) | ||
) | ||
|
||
def processAlgorithm(self, parameters, context, feedback): | ||
|
||
source = self.parameterAsSource( | ||
parameters, | ||
self.INPUT, | ||
context | ||
) | ||
|
||
fcount = source.featureCount() | ||
|
||
hasZ = QgsWkbTypes.hasZ(source.wkbType()) | ||
|
||
if not hasZ: | ||
feedback.reportError(self.tr('The layer has not Z values. Please use the Drape algorithm with a DEM layer to extact the Z value.')) | ||
return | ||
|
||
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 and descent | ||
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)) | ||
|
||
layerwithz = source | ||
|
||
(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() | ||
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
UTF-8 |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 added
BIN
+302 Bytes
python/plugins/processing/tests/testdata/expected/climb_layer_result.dbf
Binary file not shown.
1 change: 1 addition & 0 deletions
1
python/plugins/processing/tests/testdata/expected/climb_layer_result.prj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]] |
1 change: 1 addition & 0 deletions
1
python/plugins/processing/tests/testdata/expected/climb_layer_result.qpj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 added
BIN
+676 Bytes
python/plugins/processing/tests/testdata/expected/climb_layer_result.shp
Binary file not shown.
Binary file added
BIN
+108 Bytes
python/plugins/processing/tests/testdata/expected/climb_layer_result.shx
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters