QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 #include "qgsgeometrycollection.h"
20 
22 
23 QString QgsPointOnSurfaceAlgorithm::name() const
24 {
25  return QStringLiteral( "pointonsurface" );
26 }
27 
28 QString QgsPointOnSurfaceAlgorithm::displayName() const
29 {
30  return QObject::tr( "Point on surface" );
31 }
32 
33 QStringList QgsPointOnSurfaceAlgorithm::tags() const
34 {
35  return QObject::tr( "centroid,inside,within" ).split( ',' );
36 }
37 
38 QString QgsPointOnSurfaceAlgorithm::group() const
39 {
40  return QObject::tr( "Vector geometry" );
41 }
42 
43 QString QgsPointOnSurfaceAlgorithm::groupId() const
44 {
45  return QStringLiteral( "vectorgeometry" );
46 }
47 
48 QString QgsPointOnSurfaceAlgorithm::outputName() const
49 {
50  return QObject::tr( "Point" );
51 }
52 
53 QgsFeatureSink::SinkFlags QgsPointOnSurfaceAlgorithm::sinkFlags() const
54 {
55  if ( mAllParts )
57  else
59 }
60 
61 QString QgsPointOnSurfaceAlgorithm::shortHelpString() const
62 {
63  return QObject::tr( "Returns a point guaranteed to lie on the surface of a geometry." );
64 }
65 
66 QgsPointOnSurfaceAlgorithm *QgsPointOnSurfaceAlgorithm::createInstance() const
67 {
68  return new QgsPointOnSurfaceAlgorithm();
69 }
70 
71 void QgsPointOnSurfaceAlgorithm::initParameters( const QVariantMap & )
72 {
73  std::unique_ptr< QgsProcessingParameterBoolean> allParts = std::make_unique< QgsProcessingParameterBoolean >(
74  QStringLiteral( "ALL_PARTS" ),
75  QObject::tr( "Create point on surface for each part" ),
76  false );
77  allParts->setIsDynamic( true );
78  allParts->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "All parts" ), QObject::tr( "Create point on surface for each part" ), QgsPropertyDefinition::Boolean ) );
79  allParts->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
80  addParameter( allParts.release() );
81 }
82 
83 bool QgsPointOnSurfaceAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
84 {
85  mAllParts = parameterAsBoolean( parameters, QStringLiteral( "ALL_PARTS" ), context );
86  mDynamicAllParts = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "ALL_PARTS" ) );
87  if ( mDynamicAllParts )
88  mAllPartsProperty = parameters.value( QStringLiteral( "ALL_PARTS" ) ).value< QgsProperty >();
89 
90  return true;
91 }
92 
93 QgsFeatureList QgsPointOnSurfaceAlgorithm::processFeature( const QgsFeature &f, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
94 {
95  QgsFeatureList list;
96  QgsFeature feature = f;
97  if ( feature.hasGeometry() && !feature.geometry().isEmpty() )
98  {
99  const QgsGeometry geom = feature.geometry();
100 
101  bool allParts = mAllParts;
102  if ( mDynamicAllParts )
103  allParts = mAllPartsProperty.valueAsBool( context.expressionContext(), allParts );
104 
105  if ( allParts && geom.isMultipart() )
106  {
107  const QgsGeometryCollection *geomCollection = static_cast<const QgsGeometryCollection *>( geom.constGet() );
108 
109  const int partCount = geomCollection->partCount();
110  list.reserve( partCount );
111  for ( int i = 0; i < partCount; ++i )
112  {
113  const QgsGeometry partGeometry( geomCollection->geometryN( i )->clone() );
114  const QgsGeometry outputGeometry = partGeometry.pointOnSurface();
115  if ( outputGeometry.isNull() )
116  {
117  feedback->reportError( QObject::tr( "Error calculating point on surface for feature %1 part %2: %3" ).arg( feature.id() ).arg( i ).arg( outputGeometry.lastError() ) );
118  }
119  feature.setGeometry( outputGeometry );
120  list << feature;
121  }
122  }
123  else
124  {
125  const QgsGeometry outputGeometry = feature.geometry().pointOnSurface();
126  if ( outputGeometry.isNull() )
127  {
128  feedback->reportError( QObject::tr( "Error calculating point on surface for feature %1: %2" ).arg( feature.id() ).arg( outputGeometry.lastError() ) );
129  }
130  feature.setGeometry( outputGeometry );
131  list << feature;
132  }
133  }
134  else
135  {
136  list << feature;
137  }
138  return list;
139 }
140 
QgsGeometry::lastError
QString lastError() const SIP_HOLDGIL
Returns an error string referring to the last error encountered either when this geometry was created...
Definition: qgsgeometry.cpp:3308
QgsProperty
A store for object properties.
Definition: qgsproperty.h:230
QgsProcessingParameters::isDynamic
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: qgsprocessingparameters.cpp:114
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
Definition: qgsprocessingfeedback.h:37
QgsProcessingFeedback::reportError
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
Definition: qgsprocessingfeedback.cpp:59
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:71
QgsProcessingFeatureBasedAlgorithm::sinkFlags
virtual QgsFeatureSink::SinkFlags sinkFlags() const
Returns the feature sink flags to be used for the output.
Definition: qgsprocessingalgorithm.cpp:1050
QgsGeometry::isMultipart
bool isMultipart() const SIP_HOLDGIL
Returns true if WKB of the geometry is of WKBMulti* type.
Definition: qgsgeometry.cpp:389
QgsFeature::id
QgsFeatureId id
Definition: qgsfeature.h:68
QgsGeometryCollection
Geometry collection.
Definition: qgsgeometrycollection.h:36
QgsGeometryCollection::partCount
int partCount() const override
Returns count of parts contained in the geometry.
Definition: qgsgeometrycollection.cpp:894
QgsFeature::setGeometry
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:170
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:46
QgsAbstractGeometry::clone
virtual QgsAbstractGeometry * clone() const =0
Clones the geometry by performing a deep copy.
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:882
QgsPropertyDefinition
Definition for a property.
Definition: qgsproperty.h:46
QgsGeometry::isEmpty
bool isEmpty() const
Returns true if the geometry is empty (eg a linestring with no vertices, or a collection with no geom...
Definition: qgsgeometry.cpp:379
QgsGeometry::isNull
bool isNull
Definition: qgsgeometry.h:127
QgsGeometry::pointOnSurface
QgsGeometry pointOnSurface() const
Returns a point guaranteed to lie on the surface of a geometry.
Definition: qgsgeometry.cpp:2308
QgsGeometry::constGet
const QgsAbstractGeometry * constGet() const SIP_HOLDGIL
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
Definition: qgsgeometry.cpp:136
QgsFeatureSink::RegeneratePrimaryKey
@ RegeneratePrimaryKey
This flag indicates, that a primary key field cannot be guaranteed to be unique and the sink should i...
Definition: qgsfeaturesink.h:55
QgsGeometryCollection::geometryN
const QgsAbstractGeometry * geometryN(int n) const
Returns a const reference to a geometry from within the collection.
Definition: qgsgeometrycollection.h:86
QgsPropertyDefinition::Boolean
@ Boolean
Boolean value.
Definition: qgsproperty.h:53
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsFeature::hasGeometry
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:230
QgsProcessingContext::expressionContext
QgsExpressionContext & expressionContext()
Returns the expression context.
Definition: qgsprocessingcontext.h:149
qgsgeometrycollection.h
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
qgsalgorithmpointonsurface.h