22QString QgsClimbAlgorithm::name()
const
24 return QStringLiteral(
"climbalongline" );
27QString QgsClimbAlgorithm::displayName()
const
29 return QObject::tr(
"Climb along line" );
32QStringList QgsClimbAlgorithm::tags()
const
34 return QObject::tr(
"line,climb,descent,elevation" ).split(
',' );
37QString QgsClimbAlgorithm::group()
const
39 return QObject::tr(
"Vector analysis" );
42QString QgsClimbAlgorithm::groupId()
const
44 return QStringLiteral(
"vectoranalysis" );
47QString QgsClimbAlgorithm::shortHelpString()
const
49 return QObject::tr(
"This algorithm calculates the total climb and descent along line geometries.\n\n"
50 "Input layer must have Z values present. If Z values are not available, the \"Drape\" (set Z "
51 "value from raster) algorithm may be used to add Z values from a DEM layer.\n\n"
52 "The output layer is a copy of the input layer with additional fields that contain the total "
53 "climb, total descent, the minimum elevation and the maximum elevation for each line geometry."
54 "If the input layer contains fields with the same names as these added fields, they will be "
55 "renamed (field names will be altered to \"name_2\", \"name_3\", etc, finding the first "
56 "non-duplicate name)." );
59QString QgsClimbAlgorithm::shortDescription()
const
61 return QObject::tr(
"Calculates the total climb and descent along line geometries with Z values." );
64QgsClimbAlgorithm *QgsClimbAlgorithm::createInstance()
const
66 return new QgsClimbAlgorithm();
69void QgsClimbAlgorithm::initAlgorithm(
const QVariantMap & )
82 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
90 throw QgsProcessingException( QObject::tr(
"The layer does not have Z values. If you have a DEM, use the Drape algorithm to extract Z values." ) );
93 QgsFields outputFields = source->fields();
95 newFields.
append(
QgsField( QStringLiteral(
"climb" ), QMetaType::Type::Double ) );
96 newFields.
append(
QgsField( QStringLiteral(
"descent" ), QMetaType::Type::Double ) );
97 newFields.
append(
QgsField( QStringLiteral(
"minelev" ), QMetaType::Type::Double ) );
98 newFields.
append(
QgsField( QStringLiteral(
"maxelev" ), QMetaType::Type::Double ) );
102 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, outputFields, source->wkbType(), source->sourceCrs() ) );
106 double totalClimb = 0;
107 double totalDescent = 0;
108 double minElevation = std::numeric_limits<double>::max();
109 double maxElevation = -std::numeric_limits<double>::max();
111 QStringList noGeometry;
112 QStringList noZValue;
115 double step = source->featureCount() > 0 ? 100.0 / source->featureCount() : 1;
128 noGeometry.append( QObject::tr(
"Feature: %1" ).arg( f.
id() ) );
134 double minElev = std::numeric_limits<double>::max();
135 double maxElev = -std::numeric_limits<double>::max();
143 double previousZ = 0;
145 int vertexNumber = 0;
149 if ( std::isnan( z ) )
151 noZValue.append( QObject::tr(
"Feature: %1, part: %2, point: %3" ).arg( f.
id(), partNumber, vertexNumber ) );
163 double diff = z - previousZ;
172 minElev = std::min( minElev, z );
173 maxElev = std::max( maxElev, z );
178 totalDescent += descent;
182 attrs << climb << descent << minElev << maxElev;
188 minElevation = std::min( minElevation, minElev );
189 maxElevation = std::max( maxElevation, maxElev );
197 if ( !noGeometry.empty() )
199 feedback->
pushInfo( QObject::tr(
"The following features do not have geometry: %1" ).arg( noGeometry.join( QStringLiteral(
", " ) ) ) );
201 if ( !noZValue.empty() )
203 feedback->
pushInfo( QObject::tr(
"The following points do not have Z value: %1" ).arg( noZValue.join( QStringLiteral(
", " ) ) ) );
207 results.insert( QStringLiteral(
"OUTPUT" ), dest );
208 results.insert( QStringLiteral(
"TOTALCLIMB" ), totalClimb );
209 results.insert( QStringLiteral(
"TOTALDESCENT" ), totalDescent );
210 results.insert( QStringLiteral(
"MINELEVATION" ), minElevation );
211 results.insert( QStringLiteral(
"MAXELEVATION" ), maxElevation );
@ VectorLine
Vector line layers.
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.
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...
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
bool hasGeometry() const
Returns true if the feature has an associated geometry.
bool isCanceled() const
Tells whether the operation has been canceled already.
void setProgress(double progress)
Sets the current progress for the feedback object.
Encapsulate a field in an attribute table or data source.
Container of fields for a vector layer.
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
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.
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 bool hasZ(Qgis::WkbType type)
Tests whether a WKB type contains the z-dimension.