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