QGIS API Documentation 3.99.0-Master (09f76ad7019)
Loading...
Searching...
No Matches
qgsalgorithmdensifygeometriesbycount.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmdensifygeometries.cpp
3 ---------------------
4 begin : October 2019
5 copyright : (C) 2019 by Clemens Raffler
6 email : clemens dot raffler 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
20
21#include <QString>
22
23using namespace Qt::StringLiterals;
24
26
27QString QgsDensifyGeometriesByCountAlgorithm::name() const
28{
29 return u"densifygeometries"_s;
30}
31
32QString QgsDensifyGeometriesByCountAlgorithm::displayName() const
33{
34 return QObject::tr( "Densify by count" );
35}
36
37QStringList QgsDensifyGeometriesByCountAlgorithm::tags() const
38{
39 return QObject::tr( "add,vertex,vertices,points,nodes" ).split( ',' );
40}
41
42QString QgsDensifyGeometriesByCountAlgorithm::group() const
43{
44 return QObject::tr( "Vector geometry" );
45}
46
47QString QgsDensifyGeometriesByCountAlgorithm::groupId() const
48{
49 return u"vectorgeometry"_s;
50}
51
52QString QgsDensifyGeometriesByCountAlgorithm::shortHelpString() const
53{
54 return QObject::tr( "This algorithm takes a polygon or line layer "
55 "and generates a new one in which the geometries "
56 "have a larger number of vertices than the "
57 "original one.\n\n If the geometries have z or m values "
58 "present then these will be linearly interpolated "
59 "at the added nodes.\n\n The number of new vertices to "
60 "add to each feature geometry is specified "
61 "as an input parameter." );
62}
63
64QString QgsDensifyGeometriesByCountAlgorithm::shortDescription() const
65{
66 return QObject::tr( "Creates a densified version of geometries by adding a fixed number of vertices to segments." );
67}
68
69QgsDensifyGeometriesByCountAlgorithm *QgsDensifyGeometriesByCountAlgorithm::createInstance() const
70{
71 return new QgsDensifyGeometriesByCountAlgorithm;
72}
73
74QList<int> QgsDensifyGeometriesByCountAlgorithm::inputLayerTypes() const
75{
76 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorLine ) << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon );
77}
78
79void QgsDensifyGeometriesByCountAlgorithm::initParameters( const QVariantMap &configuration )
80{
81 Q_UNUSED( configuration )
82 auto verticesCnt = std::make_unique<QgsProcessingParameterNumber>( u"VERTICES"_s, QObject::tr( "Number of vertices to add" ), Qgis::ProcessingNumberParameterType::Integer, 1, false, 1, 10000000 );
83 verticesCnt->setIsDynamic( true );
84 verticesCnt->setDynamicPropertyDefinition( QgsPropertyDefinition( u"VerticesCount"_s, QObject::tr( "Vertices count" ), QgsPropertyDefinition::IntegerPositive ) );
85 verticesCnt->setDynamicLayerParameterName( u"INPUT"_s );
86 addParameter( verticesCnt.release() );
87}
88
89QString QgsDensifyGeometriesByCountAlgorithm::outputName() const
90{
91 return QObject::tr( "Densified" );
92}
93
94bool QgsDensifyGeometriesByCountAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
95{
96 Q_UNUSED( feedback )
97 mVerticesCnt = parameterAsInt( parameters, u"VERTICES"_s, context );
98
99 mDynamicVerticesCnt = QgsProcessingParameters::isDynamic( parameters, u"VERTICES"_s );
100 if ( mDynamicVerticesCnt )
101 mVerticesCntProperty = parameters.value( u"VERTICES"_s ).value<QgsProperty>();
102
103 return true;
104}
105
106QgsFeatureList QgsDensifyGeometriesByCountAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
107{
108 Q_UNUSED( context )
109 Q_UNUSED( feedback )
110
111 QgsFeature densifiedFeature = feature;
112
113 if ( feature.hasGeometry() )
114 {
115 int verticesCnt = mVerticesCnt;
116 if ( mDynamicVerticesCnt )
117 verticesCnt = mVerticesCntProperty.valueAsInt( context.expressionContext(), verticesCnt );
118
119 if ( verticesCnt > 0 )
120 densifiedFeature.setGeometry( feature.geometry().densifyByCount( verticesCnt ) );
121 }
122
123 return QgsFeatureList() << densifiedFeature;
124}
125
126
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3607
@ VectorLine
Vector line layers.
Definition qgis.h:3606
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
QgsGeometry geometry
Definition qgsfeature.h:71
bool hasGeometry() const
Returns true if the feature has an associated geometry.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
QgsGeometry densifyByCount(int extraNodesPerSegment) const
Returns a copy of the geometry which has been densified by adding the specified number of extra nodes...
Contains information about the context in which a processing algorithm is executed.
QgsExpressionContext & expressionContext()
Returns the expression context.
Base class for providing feedback from a processing algorithm.
static bool isDynamic(const QVariantMap &parameters, const QString &name)
Returns true if the parameter with matching name is a dynamic parameter, and must be evaluated once f...
Definition for a property.
Definition qgsproperty.h:47
@ IntegerPositive
Positive integer values (including 0).
Definition qgsproperty.h:55
A store for object properties.
QList< QgsFeature > QgsFeatureList