QGIS API Documentation 3.99.0-Master (09f76ad7019)
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
22#include <QString>
23
24using namespace Qt::StringLiterals;
25
27
28QString QgsPointOnSurfaceAlgorithm::name() const
29{
30 return u"pointonsurface"_s;
31}
32
33QString QgsPointOnSurfaceAlgorithm::displayName() const
34{
35 return QObject::tr( "Point on surface" );
36}
37
38QStringList QgsPointOnSurfaceAlgorithm::tags() const
39{
40 return QObject::tr( "centroid,inside,within" ).split( ',' );
41}
42
43QString QgsPointOnSurfaceAlgorithm::group() const
44{
45 return QObject::tr( "Vector geometry" );
46}
47
48QString QgsPointOnSurfaceAlgorithm::groupId() const
49{
50 return u"vectorgeometry"_s;
51}
52
53QString QgsPointOnSurfaceAlgorithm::outputName() const
54{
55 return QObject::tr( "Point" );
56}
57
58QgsFeatureSink::SinkFlags QgsPointOnSurfaceAlgorithm::sinkFlags() const
59{
60 if ( mAllParts )
62 else
64}
65
66QString QgsPointOnSurfaceAlgorithm::shortHelpString() const
67{
68 return QObject::tr( "This algorithm returns a point guaranteed to lie on the surface of a geometry." );
69}
70
71QString QgsPointOnSurfaceAlgorithm::shortDescription() const
72{
73 return QObject::tr( "Returns a point guaranteed to lie on the surface of a geometry." );
74}
75
76Qgis::ProcessingAlgorithmDocumentationFlags QgsPointOnSurfaceAlgorithm::documentationFlags() const
77{
79}
80
81QgsPointOnSurfaceAlgorithm *QgsPointOnSurfaceAlgorithm::createInstance() const
82{
83 return new QgsPointOnSurfaceAlgorithm();
84}
85
86void QgsPointOnSurfaceAlgorithm::initParameters( const QVariantMap & )
87{
88 auto allParts = std::make_unique<QgsProcessingParameterBoolean>(
89 u"ALL_PARTS"_s,
90 QObject::tr( "Create point on surface for each part" ),
91 false
92 );
93 allParts->setIsDynamic( true );
94 allParts->setDynamicPropertyDefinition( QgsPropertyDefinition( u"All parts"_s, QObject::tr( "Create point on surface for each part" ), QgsPropertyDefinition::Boolean ) );
95 allParts->setDynamicLayerParameterName( u"INPUT"_s );
96 addParameter( allParts.release() );
97}
98
99bool QgsPointOnSurfaceAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
100{
101 mAllParts = parameterAsBoolean( parameters, u"ALL_PARTS"_s, context );
102 mDynamicAllParts = QgsProcessingParameters::isDynamic( parameters, u"ALL_PARTS"_s );
103 if ( mDynamicAllParts )
104 mAllPartsProperty = parameters.value( u"ALL_PARTS"_s ).value<QgsProperty>();
105
106 return true;
107}
108
109QgsFeatureList QgsPointOnSurfaceAlgorithm::processFeature( const QgsFeature &f, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
110{
111 QgsFeatureList list;
112 QgsFeature feature = f;
113 if ( feature.hasGeometry() && !feature.geometry().isEmpty() )
114 {
115 const QgsGeometry geom = feature.geometry();
116
117 bool allParts = mAllParts;
118 if ( mDynamicAllParts )
119 allParts = mAllPartsProperty.valueAsBool( context.expressionContext(), allParts );
120
121 if ( allParts && geom.isMultipart() )
122 {
123 const QgsGeometryCollection *geomCollection = static_cast<const QgsGeometryCollection *>( geom.constGet() );
124
125 const int partCount = geomCollection->partCount();
126 list.reserve( partCount );
127 for ( int i = 0; i < partCount; ++i )
128 {
129 const QgsGeometry partGeometry( geomCollection->geometryN( i )->clone() );
130 const QgsGeometry outputGeometry = partGeometry.pointOnSurface();
131 if ( outputGeometry.isNull() )
132 {
133 feedback->reportError( QObject::tr( "Error calculating point on surface for feature %1 part %2: %3" ).arg( feature.id() ).arg( i ).arg( outputGeometry.lastError() ) );
134 }
135 feature.setGeometry( outputGeometry );
136 list << feature;
137 }
138 }
139 else
140 {
141 const QgsGeometry outputGeometry = feature.geometry().pointOnSurface();
142 if ( outputGeometry.isNull() )
143 {
144 feedback->reportError( QObject::tr( "Error calculating point on surface for feature %1: %2" ).arg( feature.id() ).arg( outputGeometry.lastError() ) );
145 }
146 feature.setGeometry( outputGeometry );
147 list << feature;
148 }
149 }
150 else
151 {
152 list << feature;
153 }
154 return list;
155}
156
@ RegeneratesPrimaryKeyInSomeScenarios
Algorithm may drop the existing primary keys or FID values in some scenarios, depending on algorithm ...
Definition qgis.h:3691
QFlags< ProcessingAlgorithmDocumentationFlag > ProcessingAlgorithmDocumentationFlags
Flags describing algorithm behavior for documentation purposes.
Definition qgis.h:3701
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: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.
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:47
@ Boolean
Boolean value.
Definition qgsproperty.h:53
A store for object properties.
QList< QgsFeature > QgsFeatureList