QGIS API Documentation  3.2.0-Bonn (bc43194)
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 QString QgsPointOnSurfaceAlgorithm::shortHelpString() const
54 {
55  return QObject::tr( "Returns a point guaranteed to lie on the surface of a geometry." );
56 }
57 
58 QgsPointOnSurfaceAlgorithm *QgsPointOnSurfaceAlgorithm::createInstance() const
59 {
60  return new QgsPointOnSurfaceAlgorithm();
61 }
62 
63 void QgsPointOnSurfaceAlgorithm::initParameters( const QVariantMap & )
64 {
65  std::unique_ptr< QgsProcessingParameterBoolean> allParts = qgis::make_unique< QgsProcessingParameterBoolean >(
66  QStringLiteral( "ALL_PARTS" ),
67  QObject::tr( "Create point on surface for each part" ),
68  false );
69  allParts->setIsDynamic( true );
70  allParts->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "All parts" ), QObject::tr( "Create point on surface for each part" ), QgsPropertyDefinition::Boolean ) );
71  allParts->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
72  addParameter( allParts.release() );
73 }
74 
75 bool QgsPointOnSurfaceAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
76 {
77  mAllParts = parameterAsBool( parameters, QStringLiteral( "ALL_PARTS" ), context );
78  mDynamicAllParts = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "ALL_PARTS" ) );
79  if ( mDynamicAllParts )
80  mAllPartsProperty = parameters.value( QStringLiteral( "ALL_PARTS" ) ).value< QgsProperty >();
81 
82  return true;
83 }
84 
85 QgsFeatureList QgsPointOnSurfaceAlgorithm::processFeature( const QgsFeature &f, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
86 {
87  QgsFeatureList list;
88  QgsFeature feature = f;
89  if ( feature.hasGeometry() )
90  {
91  QgsGeometry geom = feature.geometry();
92 
93  bool allParts = mAllParts;
94  if ( mDynamicAllParts )
95  allParts = mAllPartsProperty.valueAsBool( context.expressionContext(), allParts );
96 
97  if ( allParts && geom.isMultipart() )
98  {
99  const QgsGeometryCollection *geomCollection = static_cast<const QgsGeometryCollection *>( geom.constGet() );
100 
101  for ( int i = 0; i < geomCollection->partCount(); ++i )
102  {
103  QgsGeometry partGeometry( geomCollection->geometryN( i )->clone() );
104  QgsGeometry outputGeometry = partGeometry.pointOnSurface();
105  if ( !outputGeometry )
106  {
107  feedback->pushInfo( QObject::tr( "Error calculating point on surface for feature %1 part %2: %3" ).arg( feature.id() ).arg( i ).arg( outputGeometry.lastError() ) );
108  }
109  feature.setGeometry( outputGeometry );
110  list << feature;
111  }
112  }
113  else
114  {
115  QgsGeometry outputGeometry = feature.geometry().pointOnSurface();
116  if ( !outputGeometry )
117  {
118  feedback->pushInfo( QObject::tr( "Error calculating point on surface for feature %1: %2" ).arg( feature.id() ).arg( outputGeometry.lastError() ) );
119  }
120  feature.setGeometry( outputGeometry );
121  list << feature;
122  }
123  }
124  else
125  {
126  list << feature;
127  }
128  return list;
129 }
130 
QgsFeatureId id
Definition: qgsfeature.h:71
Base class for providing feedback from a processing algorithm.
bool isMultipart() const
Returns true if WKB of the geometry is of WKBMulti* type.
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:549
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:104
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
virtual QgsAbstractGeometry * clone() const =0
Clones the geometry by performing a deep copy.
Geometry collection.
QgsGeometry pointOnSurface() const
Returns a point guaranteed to lie on the surface of a geometry.
QgsGeometry geometry() const
Returns the geometry associated with this feature.
Definition: qgsfeature.cpp:101
A store for object properties.
Definition: qgsproperty.h:229
QgsExpressionContext & expressionContext()
Returns the expression context.
Definition for a property.
Definition: qgsproperty.h:46
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
int partCount() const override
Returns count of parts contained in the geometry.
QString lastError() const
Returns an error string referring to the last error encountered either when this geometry was created...
const QgsAbstractGeometry * geometryN(int n) const
Returns a const reference to a geometry from within the collection.
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.
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...
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.