QGIS API Documentation 3.43.0-Master (e01d6d7c4c0)
qgsalgorithmmeancoordinates.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmmeancoordinates.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
19
21
22QString QgsMeanCoordinatesAlgorithm::name() const
23{
24 return QStringLiteral( "meancoordinates" );
25}
26
27QString QgsMeanCoordinatesAlgorithm::displayName() const
28{
29 return QObject::tr( "Mean coordinate(s)" );
30}
31
32QStringList QgsMeanCoordinatesAlgorithm::tags() const
33{
34 return QObject::tr( "mean,average,coordinate" ).split( ',' );
35}
36
37QString QgsMeanCoordinatesAlgorithm::group() const
38{
39 return QObject::tr( "Vector analysis" );
40}
41
42QString QgsMeanCoordinatesAlgorithm::groupId() const
43{
44 return QStringLiteral( "vectoranalysis" );
45}
46
47void QgsMeanCoordinatesAlgorithm::initAlgorithm( const QVariantMap & )
48{
49 addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ), QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorAnyGeometry ) ) );
50 addParameter( new QgsProcessingParameterField( QStringLiteral( "WEIGHT" ), QObject::tr( "Weight field" ), QVariant(), QStringLiteral( "INPUT" ), Qgis::ProcessingFieldParameterDataType::Numeric, false, true ) );
51 addParameter( new QgsProcessingParameterField( QStringLiteral( "UID" ), QObject::tr( "Unique ID field" ), QVariant(), QStringLiteral( "INPUT" ), Qgis::ProcessingFieldParameterDataType::Any, false, true ) );
52 addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Mean coordinates" ), Qgis::ProcessingSourceType::VectorPoint ) );
53}
54
55QString QgsMeanCoordinatesAlgorithm::shortHelpString() const
56{
57 return QObject::tr( "This algorithm computes a point layer with the center of mass of geometries in an input layer.\n\n"
58 "An attribute can be specified as containing weights to be applied to each feature when computing the center of mass.\n\n"
59 "If an attribute is selected in the <Unique ID field> parameter, features will be grouped according "
60 "to values in this field. Instead of a single point with the center of mass of the whole layer, "
61 "the output layer will contain a center of mass for the features in each category." );
62}
63
64QString QgsMeanCoordinatesAlgorithm::shortDescription() const
65{
66 return QObject::tr( "Computes a point layer with the center of mass of geometries in an input layer." );
67}
68
69QgsMeanCoordinatesAlgorithm *QgsMeanCoordinatesAlgorithm::createInstance() const
70{
71 return new QgsMeanCoordinatesAlgorithm();
72}
73
74QVariantMap QgsMeanCoordinatesAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
75{
76 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
77 if ( !source )
78 throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
79
80 const QString weightFieldName = parameterAsString( parameters, QStringLiteral( "WEIGHT" ), context );
81 const QString uniqueFieldName = parameterAsString( parameters, QStringLiteral( "UID" ), context );
82
83 QgsAttributeList attributes;
84 int weightIndex = -1;
85 if ( !weightFieldName.isEmpty() )
86 {
87 weightIndex = source->fields().lookupField( weightFieldName );
88 if ( weightIndex >= 0 )
89 attributes.append( weightIndex );
90 }
91
92 int uniqueFieldIndex = -1;
93 if ( !uniqueFieldName.isEmpty() )
94 {
95 uniqueFieldIndex = source->fields().lookupField( uniqueFieldName );
96 if ( uniqueFieldIndex >= 0 )
97 attributes.append( uniqueFieldIndex );
98 }
99
100 QgsFields fields;
101 fields.append( QgsField( QStringLiteral( "MEAN_X" ), QMetaType::Type::Double, QString(), 24, 15 ) );
102 fields.append( QgsField( QStringLiteral( "MEAN_Y" ), QMetaType::Type::Double, QString(), 24, 15 ) );
103 if ( uniqueFieldIndex >= 0 )
104 {
105 const QgsField uniqueField = source->fields().at( uniqueFieldIndex );
106 fields.append( uniqueField );
107 }
108
109 QString dest;
110 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, fields, Qgis::WkbType::Point, source->sourceCrs() ) );
111 if ( !sink )
112 throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
113
114 QgsFeatureIterator features = source->getFeatures( QgsFeatureRequest().setSubsetOfAttributes( attributes ), Qgis::ProcessingFeatureSourceFlag::SkipGeometryValidityChecks );
115
116 double step = source->featureCount() > 0 ? 50.0 / source->featureCount() : 1;
117 int i = 0;
118 QgsFeature feat;
119
120 QHash<QVariant, QList<double>> means;
121 while ( features.nextFeature( feat ) )
122 {
123 i++;
124 if ( feedback->isCanceled() )
125 {
126 break;
127 }
128
129 feedback->setProgress( i * step );
130 if ( !feat.hasGeometry() )
131 continue;
132
133
134 QVariant featureClass;
135 if ( uniqueFieldIndex >= 0 )
136 {
137 featureClass = feat.attribute( uniqueFieldIndex );
138 }
139 else
140 {
141 featureClass = QStringLiteral( "#####singleclass#####" );
142 }
143
144 double weight = 1;
145 if ( weightIndex >= 0 )
146 {
147 bool ok = false;
148 weight = feat.attribute( weightIndex ).toDouble( &ok );
149 if ( !ok )
150 weight = 1.0;
151 }
152
153 if ( weight < 0 )
154 {
155 throw QgsProcessingException( QObject::tr( "Negative weight value found. Please fix your data and try again." ) );
156 }
157
158 const QList<double> values = means.value( featureClass );
159 double cx = 0;
160 double cy = 0;
161 double totalWeight = 0;
162 if ( !values.empty() )
163 {
164 cx = values.at( 0 );
165 cy = values.at( 1 );
166 totalWeight = values.at( 2 );
167 }
168
169 QgsVertexId vid;
170 QgsPoint pt;
171 const QgsAbstractGeometry *g = feat.geometry().constGet();
172 // NOTE - should this be including the duplicate nodes for closed rings? currently it is,
173 // but I suspect that the expected behavior would be to NOT include these
174 while ( g->nextVertex( vid, pt ) )
175 {
176 cx += pt.x() * weight;
177 cy += pt.y() * weight;
178 totalWeight += weight;
179 }
180
181 means[featureClass] = QList<double>() << cx << cy << totalWeight;
182 }
183
184 i = 0;
185 step = !means.empty() ? 50.0 / means.count() : 1;
186 for ( auto it = means.constBegin(); it != means.constEnd(); ++it )
187 {
188 i++;
189 if ( feedback->isCanceled() )
190 {
191 break;
192 }
193
194 feedback->setProgress( 50 + i * step );
195 if ( qgsDoubleNear( it.value().at( 2 ), 0 ) )
196 continue;
197
198 QgsFeature outFeat;
199 const double cx = it.value().at( 0 ) / it.value().at( 2 );
200 const double cy = it.value().at( 1 ) / it.value().at( 2 );
201
202 const QgsPointXY meanPoint( cx, cy );
203 outFeat.setGeometry( QgsGeometry::fromPointXY( meanPoint ) );
204
205 QgsAttributes attributes;
206 attributes << cx << cy;
207 if ( uniqueFieldIndex >= 0 )
208 attributes.append( it.key() );
209
210 outFeat.setAttributes( attributes );
211 if ( !sink->addFeature( outFeat, QgsFeatureSink::FastInsert ) )
212 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, QStringLiteral( "OUTPUT" ) ) );
213 }
214
215 sink->finalize();
216
217 QVariantMap outputs;
218 outputs.insert( QStringLiteral( "OUTPUT" ), dest );
219 return outputs;
220}
221
222
@ VectorAnyGeometry
Any vector layer with geometry.
@ VectorPoint
Vector point layers.
@ Numeric
Accepts numeric fields.
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Abstract base class for all geometries.
virtual bool nextVertex(QgsVertexId &id, QgsPoint &vertex) const =0
Returns next vertex id and coordinates.
A vector of attributes.
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
Wraps a request for features to a vector layer (or directly its vector data provider).
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
QgsGeometry geometry
Definition qgsfeature.h:69
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Q_INVOKABLE QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:53
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:61
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:53
Container of fields for a vector layer.
Definition qgsfields.h:46
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
Definition qgsfields.cpp:70
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
static QgsGeometry fromPointXY(const QgsPointXY &point)
Creates a new geometry from a QgsPointXY object.
Represents a 2D point.
Definition qgspointxy.h:60
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:49
double x
Definition qgspoint.h:52
double y
Definition qgspoint.h:53
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
A feature sink output for processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.
A vector layer or feature source field parameter for processing algorithms.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition qgis.h:6302
QList< int > QgsAttributeList
Definition qgsfield.h:27
Utility class for identifying a unique vertex within a geometry.
Definition qgsvertexid.h:30