QGIS API Documentation  3.0.2-Girona (307d082)
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  addParameter( new QgsProcessingParameterNumber( QStringLiteral( "MAX_NODES" ), QObject::tr( "Maximum nodes in parts" ), QgsProcessingParameterNumber::Integer,
25  256, false, 8, 100000 ) );
26 }
27 
28 QString QgsSubdivideAlgorithm::name() const
29 {
30  return QStringLiteral( "subdivide" );
31 }
32 
33 QString QgsSubdivideAlgorithm::displayName() const
34 {
35  return QObject::tr( "Subdivide" );
36 }
37 
38 QStringList QgsSubdivideAlgorithm::tags() const
39 {
40  return QObject::tr( "subdivide,segmentize,split,tessellate" ).split( ',' );
41 }
42 
43 QString QgsSubdivideAlgorithm::group() const
44 {
45  return QObject::tr( "Vector geometry" );
46 }
47 
48 QString QgsSubdivideAlgorithm::groupId() const
49 {
50  return QStringLiteral( "vectorgeometry" );
51 }
52 
53 QString QgsSubdivideAlgorithm::shortHelpString() const
54 {
55  return QObject::tr( "Subdivides the geometry. The returned geometry will be a collection containing subdivided parts "
56  "from the original geometry, where no part has more then the specified maximum number of nodes.\n\n"
57  "This is useful for dividing a complex geometry into less complex parts, which are better able to be spatially "
58  "indexed and faster to perform further operations such as intersects on. The returned geometry parts may "
59  "not be valid and may contain self-intersections.\n\n"
60  "Curved geometries will be segmentized before subdivision." );
61 }
62 
63 QgsSubdivideAlgorithm *QgsSubdivideAlgorithm::createInstance() const
64 {
65  return new QgsSubdivideAlgorithm();
66 }
67 
68 QString QgsSubdivideAlgorithm::outputName() const
69 {
70  return QObject::tr( "Subdivided" );
71 }
72 
73 QgsWkbTypes::Type QgsSubdivideAlgorithm::outputWkbType( QgsWkbTypes::Type inputWkbType ) const
74 {
75  return QgsWkbTypes::multiType( inputWkbType );
76 }
77 
78 QgsFeatureList QgsSubdivideAlgorithm::processFeature( const QgsFeature &f, QgsProcessingContext &, QgsProcessingFeedback *feedback )
79 {
80  QgsFeature feature = f;
81  if ( feature.hasGeometry() )
82  {
83  feature.setGeometry( feature.geometry().subdivide( mMaxNodes ) );
84  if ( !feature.geometry() )
85  {
86  feedback->reportError( QObject::tr( "Error calculating subdivision for feature %1" ).arg( feature.id() ) );
87  }
88  }
89  return QgsFeatureList() << feature;
90 }
91 
92 bool QgsSubdivideAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
93 {
94  mMaxNodes = parameterAsInt( parameters, QStringLiteral( "MAX_NODES" ), context );
95  return true;
96 }
97 
98 
99 
101 
102 
103 
QgsFeatureId id
Definition: qgsfeature.h:71
static Type multiType(Type type)
Returns the multi type for a WKB type.
Definition: qgswkbtypes.h:296
Base class for providing feedback from a processing algorithm.
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:549
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:62
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:190
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:67
QgsGeometry geometry() const
Returns the geometry associated with this feature.
Definition: qgsfeature.cpp:101
A numeric parameter for processing algorithms.
void setGeometry(const QgsGeometry &geometry)
Set the feature&#39;s geometry.
Definition: qgsfeature.cpp:137
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.