QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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(
64 "This algorithm subdivides the geometry. The returned geometry will be a collection containing subdivided parts "
65 "from the original geometry, where no part has more than the specified maximum number of nodes.\n\n"
66 "This is useful for dividing a complex geometry into less complex parts, which are better able to be spatially "
67 "indexed and faster to perform further operations such as intersects on. The returned geometry parts may "
68 "not be valid and may contain self-intersections.\n\n"
69 "Curved geometries will be segmentized before subdivision."
70 );
71}
72
73QString QgsSubdivideAlgorithm::shortDescription() const
74{
75 return QObject::tr( "Subdivides the geometry into parts that have less than a specified maximum number of nodes." );
76}
77
78QgsSubdivideAlgorithm *QgsSubdivideAlgorithm::createInstance() const
79{
80 return new QgsSubdivideAlgorithm();
81}
82
83QString QgsSubdivideAlgorithm::outputName() const
84{
85 return QObject::tr( "Subdivided" );
86}
87
88Qgis::WkbType QgsSubdivideAlgorithm::outputWkbType( Qgis::WkbType inputWkbType ) const
89{
90 return QgsWkbTypes::multiType( inputWkbType );
91}
92
93QgsFeatureList QgsSubdivideAlgorithm::processFeature( const QgsFeature &f, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
94{
95 QgsFeature feature = f;
96 if ( feature.hasGeometry() )
97 {
98 int maxNodes = mMaxNodes;
99 if ( mDynamicMaxNodes )
100 maxNodes = mMaxNodesProperty.valueAsDouble( context.expressionContext(), maxNodes );
101
102 feature.setGeometry( feature.geometry().subdivide( maxNodes ) );
103 if ( !feature.hasGeometry() )
104 {
105 feedback->reportError( QObject::tr( "Error calculating subdivision for feature %1" ).arg( feature.id() ) );
106 }
107 }
108 return QgsFeatureList() << feature;
109}
110
111bool QgsSubdivideAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
112{
113 mMaxNodes = parameterAsInt( parameters, u"MAX_NODES"_s, context );
114 mDynamicMaxNodes = QgsProcessingParameters::isDynamic( parameters, u"MAX_NODES"_s );
115 if ( mDynamicMaxNodes )
116 mMaxNodesProperty = parameters.value( u"MAX_NODES"_s ).value<QgsProperty>();
117
118 return true;
119}
120
121
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:294
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:53
A store for object properties.
static Qgis::WkbType multiType(Qgis::WkbType type)
Returns the multi type for a WKB type.
QList< QgsFeature > QgsFeatureList