QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
qgsalgorithmpointonsurface.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmpointonsurface.cpp
3 ------------------------
4 begin : March 2018
5 copyright : (C) 2018 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
23
24QString QgsPointOnSurfaceAlgorithm::name() const
25{
26 return QStringLiteral( "pointonsurface" );
27}
28
29QString QgsPointOnSurfaceAlgorithm::displayName() const
30{
31 return QObject::tr( "Point on surface" );
32}
33
34QStringList QgsPointOnSurfaceAlgorithm::tags() const
35{
36 return QObject::tr( "centroid,inside,within" ).split( ',' );
37}
38
39QString QgsPointOnSurfaceAlgorithm::group() const
40{
41 return QObject::tr( "Vector geometry" );
42}
43
44QString QgsPointOnSurfaceAlgorithm::groupId() const
45{
46 return QStringLiteral( "vectorgeometry" );
47}
48
49QString QgsPointOnSurfaceAlgorithm::outputName() const
50{
51 return QObject::tr( "Point" );
52}
53
54QgsFeatureSink::SinkFlags QgsPointOnSurfaceAlgorithm::sinkFlags() const
55{
56 if ( mAllParts )
58 else
60}
61
62QString QgsPointOnSurfaceAlgorithm::shortHelpString() const
63{
64 return QObject::tr( "This algorithm returns a point guaranteed to lie on the surface of a geometry." );
65}
66
67QString QgsPointOnSurfaceAlgorithm::shortDescription() const
68{
69 return QObject::tr( "Returns a point guaranteed to lie on the surface of a geometry." );
70}
71
72Qgis::ProcessingAlgorithmDocumentationFlags QgsPointOnSurfaceAlgorithm::documentationFlags() const
73{
75}
76
77QgsPointOnSurfaceAlgorithm *QgsPointOnSurfaceAlgorithm::createInstance() const
78{
79 return new QgsPointOnSurfaceAlgorithm();
80}
81
82void QgsPointOnSurfaceAlgorithm::initParameters( const QVariantMap & )
83{
84 auto allParts = std::make_unique<QgsProcessingParameterBoolean>(
85 QStringLiteral( "ALL_PARTS" ),
86 QObject::tr( "Create point on surface for each part" ),
87 false
88 );
89 allParts->setIsDynamic( true );
90 allParts->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "All parts" ), QObject::tr( "Create point on surface for each part" ), QgsPropertyDefinition::Boolean ) );
91 allParts->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
92 addParameter( allParts.release() );
93}
94
95bool QgsPointOnSurfaceAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
96{
97 mAllParts = parameterAsBoolean( parameters, QStringLiteral( "ALL_PARTS" ), context );
98 mDynamicAllParts = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "ALL_PARTS" ) );
99 if ( mDynamicAllParts )
100 mAllPartsProperty = parameters.value( QStringLiteral( "ALL_PARTS" ) ).value<QgsProperty>();
101
102 return true;
103}
104
105QgsFeatureList QgsPointOnSurfaceAlgorithm::processFeature( const QgsFeature &f, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
106{
107 QgsFeatureList list;
108 QgsFeature feature = f;
109 if ( feature.hasGeometry() && !feature.geometry().isEmpty() )
110 {
111 const QgsGeometry geom = feature.geometry();
112
113 bool allParts = mAllParts;
114 if ( mDynamicAllParts )
115 allParts = mAllPartsProperty.valueAsBool( context.expressionContext(), allParts );
116
117 if ( allParts && geom.isMultipart() )
118 {
119 const QgsGeometryCollection *geomCollection = static_cast<const QgsGeometryCollection *>( geom.constGet() );
120
121 const int partCount = geomCollection->partCount();
122 list.reserve( partCount );
123 for ( int i = 0; i < partCount; ++i )
124 {
125 const QgsGeometry partGeometry( geomCollection->geometryN( i )->clone() );
126 const QgsGeometry outputGeometry = partGeometry.pointOnSurface();
127 if ( outputGeometry.isNull() )
128 {
129 feedback->reportError( QObject::tr( "Error calculating point on surface for feature %1 part %2: %3" ).arg( feature.id() ).arg( i ).arg( outputGeometry.lastError() ) );
130 }
131 feature.setGeometry( outputGeometry );
132 list << feature;
133 }
134 }
135 else
136 {
137 const QgsGeometry outputGeometry = feature.geometry().pointOnSurface();
138 if ( outputGeometry.isNull() )
139 {
140 feedback->reportError( QObject::tr( "Error calculating point on surface for feature %1: %2" ).arg( feature.id() ).arg( outputGeometry.lastError() ) );
141 }
142 feature.setGeometry( outputGeometry );
143 list << feature;
144 }
145 }
146 else
147 {
148 list << feature;
149 }
150 return list;
151}
152
@ RegeneratesPrimaryKeyInSomeScenarios
Algorithm may drop the existing primary keys or FID values in some scenarios, depending on algorithm ...
Definition qgis.h:3620
QFlags< ProcessingAlgorithmDocumentationFlag > ProcessingAlgorithmDocumentationFlags
Flags describing algorithm behavior for documentation purposes.
Definition qgis.h:3630
virtual QgsAbstractGeometry * clone() const =0
Clones the geometry by performing a deep copy.
QFlags< SinkFlag > SinkFlags
@ RegeneratePrimaryKey
This flag indicates, that a primary key field cannot be guaranteed to be unique and the sink should i...
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.
int partCount() const override
Returns count of parts contained in the geometry.
const QgsAbstractGeometry * geometryN(int n) const
Returns a const reference to a geometry from within the collection.
A geometry is the spatial representation of a feature.
QgsGeometry pointOnSurface() const
Returns a point guaranteed to lie on the surface of a geometry.
QString lastError() const
Returns an error string referring to the last error encountered either when this geometry was created...
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
bool isMultipart() const
Returns true if WKB of the geometry is of WKBMulti* type.
bool isEmpty() const
Returns true if the geometry is empty (eg a linestring with no vertices, or a collection with no geom...
Contains information about the context in which a processing algorithm is executed.
QgsExpressionContext & expressionContext()
Returns the expression context.
virtual QgsFeatureSink::SinkFlags sinkFlags() const
Returns the feature sink flags to be used for the output.
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
@ Boolean
Boolean value.
Definition qgsproperty.h:51
A store for object properties.
QList< QgsFeature > QgsFeatureList