22using namespace Qt::StringLiterals;
26QString QgsMeanCoordinatesAlgorithm::name()
const
28 return u
"meancoordinates"_s;
31QString QgsMeanCoordinatesAlgorithm::displayName()
const
33 return QObject::tr(
"Mean coordinate(s)" );
36QStringList QgsMeanCoordinatesAlgorithm::tags()
const
38 return QObject::tr(
"mean,average,coordinate" ).split(
',' );
41QString QgsMeanCoordinatesAlgorithm::group()
const
43 return QObject::tr(
"Vector analysis" );
46QString QgsMeanCoordinatesAlgorithm::groupId()
const
48 return u
"vectoranalysis"_s;
51void QgsMeanCoordinatesAlgorithm::initAlgorithm(
const QVariantMap & )
59QString QgsMeanCoordinatesAlgorithm::shortHelpString()
const
61 return QObject::tr(
"This algorithm computes a point layer with the center of mass of geometries in an input layer.\n\n"
62 "An attribute can be specified as containing weights to be applied to each feature when computing the center of mass.\n\n"
63 "If an attribute is selected in the <Unique ID field> parameter, features will be grouped according "
64 "to values in this field. Instead of a single point with the center of mass of the whole layer, "
65 "the output layer will contain a center of mass for the features in each category." );
68QString QgsMeanCoordinatesAlgorithm::shortDescription()
const
70 return QObject::tr(
"Computes a point layer with the center of mass of geometries in an input layer." );
73QgsMeanCoordinatesAlgorithm *QgsMeanCoordinatesAlgorithm::createInstance()
const
75 return new QgsMeanCoordinatesAlgorithm();
80 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u
"INPUT"_s, context ) );
84 const QString weightFieldName = parameterAsString( parameters, u
"WEIGHT"_s, context );
85 const QString uniqueFieldName = parameterAsString( parameters, u
"UID"_s, context );
89 if ( !weightFieldName.isEmpty() )
91 weightIndex = source->fields().lookupField( weightFieldName );
92 if ( weightIndex >= 0 )
93 attributes.append( weightIndex );
96 int uniqueFieldIndex = -1;
97 if ( !uniqueFieldName.isEmpty() )
99 uniqueFieldIndex = source->fields().lookupField( uniqueFieldName );
100 if ( uniqueFieldIndex >= 0 )
101 attributes.append( uniqueFieldIndex );
105 fields.
append(
QgsField( u
"MEAN_X"_s, QMetaType::Type::Double, QString(), 24, 15 ) );
106 fields.
append(
QgsField( u
"MEAN_Y"_s, QMetaType::Type::Double, QString(), 24, 15 ) );
107 if ( uniqueFieldIndex >= 0 )
109 const QgsField uniqueField = source->fields().at( uniqueFieldIndex );
110 fields.
append( uniqueField );
114 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u
"OUTPUT"_s, context, dest, fields,
Qgis::WkbType::Point, source->sourceCrs() ) );
120 double step = source->featureCount() > 0 ? 50.0 / source->featureCount() : 1;
124 QHash<QVariant, QList<double>> means;
138 QVariant featureClass;
139 if ( uniqueFieldIndex >= 0 )
141 featureClass = feat.
attribute( uniqueFieldIndex );
145 featureClass = u
"#####singleclass#####"_s;
149 if ( weightIndex >= 0 )
152 weight = feat.
attribute( weightIndex ).toDouble( &ok );
159 throw QgsProcessingException( QObject::tr(
"Negative weight value found. Please fix your data and try again." ) );
162 const QList<double> values = means.value( featureClass );
165 double totalWeight = 0;
166 if ( !values.empty() )
170 totalWeight = values.at( 2 );
180 cx += pt.
x() * weight;
181 cy += pt.
y() * weight;
182 totalWeight += weight;
185 means[featureClass] = QList<double>() << cx << cy << totalWeight;
189 step = !means.empty() ? 50.0 / means.count() : 1;
190 for (
auto it = means.constBegin(); it != means.constEnd(); ++it )
203 const double cx = it.value().at( 0 ) / it.value().at( 2 );
204 const double cy = it.value().at( 1 ) / it.value().at( 2 );
210 attributes << cx << cy;
211 if ( uniqueFieldIndex >= 0 )
212 attributes.append( it.key() );
222 outputs.insert( u
"OUTPUT"_s, dest );
@ 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.
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...
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
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.
void setProgress(double progress)
Sets the current progress for the feedback object.
Encapsulate a field in an attribute table or data source.
Container of fields for a vector layer.
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
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.
Point geometry type, with support for z-dimension and m-values.
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).
QList< int > QgsAttributeList
Utility class for identifying a unique vertex within a geometry.