QGIS API Documentation 3.41.0-Master (af5edcb665c)
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
21
22void QgsSubdivideAlgorithm::initParameters( const QVariantMap & )
23{
24 std::unique_ptr<QgsProcessingParameterNumber> 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( "Subdivides the geometry. The returned geometry will be a collection containing subdivided parts "
60 "from the original geometry, where no part has more then 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
67QgsSubdivideAlgorithm *QgsSubdivideAlgorithm::createInstance() const
68{
69 return new QgsSubdivideAlgorithm();
70}
71
72QString QgsSubdivideAlgorithm::outputName() const
73{
74 return QObject::tr( "Subdivided" );
75}
76
77Qgis::WkbType QgsSubdivideAlgorithm::outputWkbType( Qgis::WkbType inputWkbType ) const
78{
79 return QgsWkbTypes::multiType( inputWkbType );
80}
81
82QgsFeatureList QgsSubdivideAlgorithm::processFeature( const QgsFeature &f, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
83{
84 QgsFeature feature = f;
85 if ( feature.hasGeometry() )
86 {
87 int maxNodes = mMaxNodes;
88 if ( mDynamicMaxNodes )
89 maxNodes = mMaxNodesProperty.valueAsDouble( context.expressionContext(), maxNodes );
90
91 feature.setGeometry( feature.geometry().subdivide( maxNodes ) );
92 if ( !feature.hasGeometry() )
93 {
94 feedback->reportError( QObject::tr( "Error calculating subdivision for feature %1" ).arg( feature.id() ) );
95 }
96 }
97 return QgsFeatureList() << feature;
98}
99
100bool QgsSubdivideAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
101{
102 mMaxNodes = parameterAsInt( parameters, QStringLiteral( "MAX_NODES" ), context );
103 mDynamicMaxNodes = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "MAX_NODES" ) );
104 if ( mDynamicMaxNodes )
105 mMaxNodesProperty = parameters.value( QStringLiteral( "MAX_NODES" ) ).value<QgsProperty>();
106
107 return true;
108}
109
110
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