QGIS API Documentation 3.99.0-Master (357b655ed83)
Loading...
Searching...
No Matches
qgsalgorithmclimb.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmclimb.cpp
3 ---------------------
4 begin : February 2025
5 copyright : (C) 2025 by Alexander Bruy
6 email : alexander dot bruy at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#include "qgsalgorithmclimb.h"
19
20#include <QString>
21
22using namespace Qt::StringLiterals;
23
25
26QString QgsClimbAlgorithm::name() const
27{
28 return u"climbalongline"_s;
29}
30
31QString QgsClimbAlgorithm::displayName() const
32{
33 return QObject::tr( "Climb along line" );
34}
35
36QStringList QgsClimbAlgorithm::tags() const
37{
38 return QObject::tr( "line,climb,descent,elevation" ).split( ',' );
39}
40
41QString QgsClimbAlgorithm::group() const
42{
43 return QObject::tr( "Vector analysis" );
44}
45
46QString QgsClimbAlgorithm::groupId() const
47{
48 return u"vectoranalysis"_s;
49}
50
51QString QgsClimbAlgorithm::shortHelpString() const
52{
53 return QObject::tr( "This algorithm calculates the total climb and descent along line geometries.\n\n"
54 "Input layer must have Z values present. If Z values are not available, the \"Drape\" (set Z "
55 "value from raster) algorithm may be used to add Z values from a DEM layer.\n\n"
56 "The output layer is a copy of the input layer with additional fields that contain the total "
57 "climb, total descent, the minimum elevation and the maximum elevation for each line geometry."
58 "If the input layer contains fields with the same names as these added fields, they will be "
59 "renamed (field names will be altered to \"name_2\", \"name_3\", etc, finding the first "
60 "non-duplicate name)." );
61}
62
63QString QgsClimbAlgorithm::shortDescription() const
64{
65 return QObject::tr( "Calculates the total climb and descent along line geometries with Z values." );
66}
67
68QgsClimbAlgorithm *QgsClimbAlgorithm::createInstance() const
69{
70 return new QgsClimbAlgorithm();
71}
72
73void QgsClimbAlgorithm::initAlgorithm( const QVariantMap & )
74{
75 addParameter( new QgsProcessingParameterFeatureSource( u"INPUT"_s, QObject::tr( "Line layer" ), QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorLine ) ) );
76 addParameter( new QgsProcessingParameterFeatureSink( u"OUTPUT"_s, QObject::tr( "Climb layer" ) ) );
77 // TODO QGIS 5.0 harmonize output names with the rest of algorithms (use underscore to separate words)
78 addOutput( new QgsProcessingOutputNumber( u"TOTALCLIMB"_s, QObject::tr( "Total climb" ) ) );
79 addOutput( new QgsProcessingOutputNumber( u"TOTALDESCENT"_s, QObject::tr( "Total descent" ) ) );
80 addOutput( new QgsProcessingOutputNumber( u"MINELEVATION"_s, QObject::tr( "Minimum elevation" ) ) );
81 addOutput( new QgsProcessingOutputNumber( u"MAXELEVATION"_s, QObject::tr( "Maximum elevation" ) ) );
82}
83
84QVariantMap QgsClimbAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
85{
86 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u"INPUT"_s, context ) );
87 if ( !source )
88 {
89 throw QgsProcessingException( invalidSourceError( parameters, u"INPUT"_s ) );
90 }
91
92 if ( !QgsWkbTypes::hasZ( source->wkbType() ) )
93 {
94 throw QgsProcessingException( QObject::tr( "The layer does not have Z values. If you have a DEM, use the Drape algorithm to extract Z values." ) );
95 }
96
97 QgsFields outputFields = source->fields();
98 QgsFields newFields;
99 newFields.append( QgsField( u"climb"_s, QMetaType::Type::Double ) );
100 newFields.append( QgsField( u"descent"_s, QMetaType::Type::Double ) );
101 newFields.append( QgsField( u"minelev"_s, QMetaType::Type::Double ) );
102 newFields.append( QgsField( u"maxelev"_s, QMetaType::Type::Double ) );
103 outputFields = QgsProcessingUtils::combineFields( outputFields, newFields );
104
105 QString dest;
106 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u"OUTPUT"_s, context, dest, outputFields, source->wkbType(), source->sourceCrs() ) );
107 if ( !sink )
108 throw QgsProcessingException( invalidSinkError( parameters, u"OUTPUT"_s ) );
109
110 double totalClimb = 0;
111 double totalDescent = 0;
112 double minElevation = std::numeric_limits<double>::max();
113 double maxElevation = -std::numeric_limits<double>::max();
114
115 QStringList noGeometry;
116 QStringList noZValue;
117
118 QgsFeatureIterator it = source->getFeatures();
119 double step = source->featureCount() > 0 ? 100.0 / source->featureCount() : 1;
120 int i = 0;
121
122 QgsFeature f;
123 while ( it.nextFeature( f ) )
124 {
125 if ( feedback->isCanceled() )
126 {
127 break;
128 }
129
130 if ( !f.hasGeometry() )
131 {
132 noGeometry.append( QObject::tr( "Feature: %1" ).arg( f.id() ) );
133 continue;
134 }
135
136 double climb = 0;
137 double descent = 0;
138 double minElev = std::numeric_limits<double>::max();
139 double maxElev = -std::numeric_limits<double>::max();
140 // Handle multipart geometries
141 const QgsGeometry g = f.geometry();
142 int partNumber = 0;
143 for ( auto partIt = g.const_parts_begin(); partIt != g.const_parts_end(); ++partIt, ++partNumber )
144 {
145 bool first = true;
146 double z = 0;
147 double previousZ = 0;
148 const QgsAbstractGeometry *part = *partIt;
149 int vertexNumber = 0;
150 for ( auto it = part->vertices_begin(); it != part->vertices_end(); ++it, ++vertexNumber )
151 {
152 z = QgsPoint( *it ).z();
153 if ( std::isnan( z ) )
154 {
155 noZValue.append( QObject::tr( "Feature: %1, part: %2, point: %3" ).arg( f.id(), partNumber, vertexNumber ) );
156 continue;
157 }
158 if ( first )
159 {
160 previousZ = z;
161 minElev = z;
162 maxElev = z;
163 first = false;
164 }
165 else
166 {
167 double diff = z - previousZ;
168 if ( diff > 0 )
169 {
170 climb += diff;
171 }
172 else
173 {
174 descent -= diff;
175 }
176 minElev = std::min( minElev, z );
177 maxElev = std::max( maxElev, z );
178 }
179 previousZ = z;
180 }
181 totalClimb += climb;
182 totalDescent += descent;
183 }
184
185 QgsAttributes attrs = f.attributes();
186 attrs << climb << descent << minElev << maxElev;
187 f.setAttributes( attrs );
188 if ( !sink->addFeature( f, QgsFeatureSink::FastInsert ) )
189 {
190 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, u"OUTPUT"_s ) );
191 }
192 minElevation = std::min( minElevation, minElev );
193 maxElevation = std::max( maxElevation, maxElev );
194
195 feedback->setProgress( i * step );
196 i++;
197 }
198
199 sink->finalize();
200
201 if ( !noGeometry.empty() )
202 {
203 feedback->pushInfo( QObject::tr( "The following features do not have geometry: %1" ).arg( noGeometry.join( ", "_L1 ) ) );
204 }
205 if ( !noZValue.empty() )
206 {
207 feedback->pushInfo( QObject::tr( "The following points do not have Z value: %1" ).arg( noZValue.join( ", "_L1 ) ) );
208 }
209
210 QVariantMap results;
211 results.insert( u"OUTPUT"_s, dest );
212 results.insert( u"TOTALCLIMB"_s, totalClimb );
213 results.insert( u"TOTALDESCENT"_s, totalDescent );
214 results.insert( u"MINELEVATION"_s, minElevation );
215 results.insert( u"MAXELEVATION"_s, maxElevation );
216 return results;
217}
218
@ VectorLine
Vector line layers.
Definition qgis.h:3606
Abstract base class for all geometries.
vertex_iterator vertices_end() const
Returns STL-style iterator pointing to the imaginary vertex after the last vertex of the geometry.
vertex_iterator vertices_begin() const
Returns STL-style iterator pointing to the first vertex of the geometry.
A vector of attributes.
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
QgsAttributes attributes
Definition qgsfeature.h:69
QgsFeatureId id
Definition qgsfeature.h:68
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
QgsGeometry geometry
Definition qgsfeature.h:71
bool hasGeometry() const
Returns true if the feature has an associated geometry.
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:55
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:63
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:56
Container of fields for a vector layer.
Definition qgsfields.h:46
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
Definition qgsfields.cpp:76
A geometry is the spatial representation of a feature.
QgsAbstractGeometry::const_part_iterator const_parts_begin() const
Returns STL-style const iterator pointing to the first part of the geometry.
QgsAbstractGeometry::const_part_iterator const_parts_end() const
Returns STL-style iterator pointing to the imaginary part after the last part of the geometry.
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:53
double z
Definition qgspoint.h:58
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
A numeric output for processing algorithms.
A feature sink output for processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.
static QgsFields combineFields(const QgsFields &fieldsA, const QgsFields &fieldsB, const QString &fieldsBPrefix=QString())
Combines two field lists, avoiding duplicate field names (in a case-insensitive manner).
static Q_INVOKABLE bool hasZ(Qgis::WkbType type)
Tests whether a WKB type contains the z-dimension.