QGIS API Documentation 3.99.0-Master (357b655ed83)
Loading...
Searching...
No Matches
qgsalgorithmsubdivide.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmsubdivide.cpp
3 ---------------------
4 begin : April 2017
5 copyright : (C) 2017 by Nyall Dawson
6 email : nyall dot dawson 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
19
20#include <QString>
21
22using namespace Qt::StringLiterals;
23
25
26void QgsSubdivideAlgorithm::initParameters( const QVariantMap & )
27{
28 auto nodes = std::make_unique<QgsProcessingParameterNumber>( u"MAX_NODES"_s, QObject::tr( "Maximum nodes in parts" ), Qgis::ProcessingNumberParameterType::Integer, 256, false, 8, 100000 );
29 nodes->setIsDynamic( true );
30 nodes->setDynamicPropertyDefinition( QgsPropertyDefinition( u"MAX_NODES"_s, QObject::tr( "Maximum nodes in parts" ), QgsPropertyDefinition::Integer ) );
31 nodes->setDynamicLayerParameterName( u"INPUT"_s );
32
33 addParameter( nodes.release() );
34}
35
36QString QgsSubdivideAlgorithm::name() const
37{
38 return u"subdivide"_s;
39}
40
41QString QgsSubdivideAlgorithm::displayName() const
42{
43 return QObject::tr( "Subdivide" );
44}
45
46QStringList QgsSubdivideAlgorithm::tags() const
47{
48 return QObject::tr( "subdivide,segmentize,split,tessellate" ).split( ',' );
49}
50
51QString QgsSubdivideAlgorithm::group() const
52{
53 return QObject::tr( "Vector geometry" );
54}
55
56QString QgsSubdivideAlgorithm::groupId() const
57{
58 return u"vectorgeometry"_s;
59}
60
61QString QgsSubdivideAlgorithm::shortHelpString() const
62{
63 return QObject::tr( "This algorithm subdivides the geometry. The returned geometry will be a collection containing subdivided parts "
64 "from the original geometry, where no part has more than the specified maximum number of nodes.\n\n"
65 "This is useful for dividing a complex geometry into less complex parts, which are better able to be spatially "
66 "indexed and faster to perform further operations such as intersects on. The returned geometry parts may "
67 "not be valid and may contain self-intersections.\n\n"
68 "Curved geometries will be segmentized before subdivision." );
69}
70
71QString QgsSubdivideAlgorithm::shortDescription() const
72{
73 return QObject::tr( "Subdivides the geometry into parts that have less than a specified maximum number of nodes." );
74}
75
76QgsSubdivideAlgorithm *QgsSubdivideAlgorithm::createInstance() const
77{
78 return new QgsSubdivideAlgorithm();
79}
80
81QString QgsSubdivideAlgorithm::outputName() const
82{
83 return QObject::tr( "Subdivided" );
84}
85
86Qgis::WkbType QgsSubdivideAlgorithm::outputWkbType( Qgis::WkbType inputWkbType ) const
87{
88 return QgsWkbTypes::multiType( inputWkbType );
89}
90
91QgsFeatureList QgsSubdivideAlgorithm::processFeature( const QgsFeature &f, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
92{
93 QgsFeature feature = f;
94 if ( feature.hasGeometry() )
95 {
96 int maxNodes = mMaxNodes;
97 if ( mDynamicMaxNodes )
98 maxNodes = mMaxNodesProperty.valueAsDouble( context.expressionContext(), maxNodes );
99
100 feature.setGeometry( feature.geometry().subdivide( maxNodes ) );
101 if ( !feature.hasGeometry() )
102 {
103 feedback->reportError( QObject::tr( "Error calculating subdivision for feature %1" ).arg( feature.id() ) );
104 }
105 }
106 return QgsFeatureList() << feature;
107}
108
109bool QgsSubdivideAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
110{
111 mMaxNodes = parameterAsInt( parameters, u"MAX_NODES"_s, context );
112 mDynamicMaxNodes = QgsProcessingParameters::isDynamic( parameters, u"MAX_NODES"_s );
113 if ( mDynamicMaxNodes )
114 mMaxNodesProperty = parameters.value( u"MAX_NODES"_s ).value<QgsProperty>();
115
116 return true;
117}
118
119
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:280
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
QgsFeatureId id
Definition qgsfeature.h:68
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 subdivide(int maxNodes=256, const QgsGeometryParameters &parameters=QgsGeometryParameters()) const
Subdivides the geometry.
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.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
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
@ Integer
Integer value (including negative values).
Definition qgsproperty.h:54
A store for object properties.
static Qgis::WkbType multiType(Qgis::WkbType type)
Returns the multi type for a WKB type.
QList< QgsFeature > QgsFeatureList