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