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