22QString QgsMeanCoordinatesAlgorithm::name()
const
24 return QStringLiteral(
"meancoordinates" );
27QString QgsMeanCoordinatesAlgorithm::displayName()
const
29 return QObject::tr(
"Mean coordinate(s)" );
32QStringList QgsMeanCoordinatesAlgorithm::tags()
const
34 return QObject::tr(
"mean,average,coordinate" ).split(
',' );
37QString QgsMeanCoordinatesAlgorithm::group()
const
39 return QObject::tr(
"Vector analysis" );
42QString QgsMeanCoordinatesAlgorithm::groupId()
const
44 return QStringLiteral(
"vectoranalysis" );
47void QgsMeanCoordinatesAlgorithm::initAlgorithm(
const QVariantMap & )
55QString QgsMeanCoordinatesAlgorithm::shortHelpString()
const
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." );
64QgsMeanCoordinatesAlgorithm *QgsMeanCoordinatesAlgorithm::createInstance()
const
66 return new QgsMeanCoordinatesAlgorithm();
71 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
75 const QString weightFieldName = parameterAsString( parameters, QStringLiteral(
"WEIGHT" ), context );
76 const QString uniqueFieldName = parameterAsString( parameters, QStringLiteral(
"UID" ), context );
80 if ( !weightFieldName.isEmpty() )
82 weightIndex = source->fields().lookupField( weightFieldName );
83 if ( weightIndex >= 0 )
84 attributes.append( weightIndex );
87 int uniqueFieldIndex = -1;
88 if ( !uniqueFieldName.isEmpty() )
90 uniqueFieldIndex = source->fields().lookupField( uniqueFieldName );
91 if ( uniqueFieldIndex >= 0 )
92 attributes.append( uniqueFieldIndex );
96 fields.
append(
QgsField( QStringLiteral(
"MEAN_X" ), QMetaType::Type::Double, QString(), 24, 15 ) );
97 fields.
append(
QgsField( QStringLiteral(
"MEAN_Y" ), QMetaType::Type::Double, QString(), 24, 15 ) );
98 if ( uniqueFieldIndex >= 0 )
100 const QgsField uniqueField = source->fields().at( uniqueFieldIndex );
101 fields.
append( uniqueField );
105 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, fields,
Qgis::WkbType::Point, source->sourceCrs() ) );
111 double step = source->featureCount() > 0 ? 50.0 / source->featureCount() : 1;
115 QHash<QVariant, QList<double>> means;
129 QVariant featureClass;
130 if ( uniqueFieldIndex >= 0 )
132 featureClass = feat.
attribute( uniqueFieldIndex );
136 featureClass = QStringLiteral(
"#####singleclass#####" );
140 if ( weightIndex >= 0 )
143 weight = feat.
attribute( weightIndex ).toDouble( &ok );
150 throw QgsProcessingException( QObject::tr(
"Negative weight value found. Please fix your data and try again." ) );
153 const QList<double> values = means.value( featureClass );
156 double totalWeight = 0;
157 if ( !values.empty() )
161 totalWeight = values.at( 2 );
171 cx += pt.
x() * weight;
172 cy += pt.
y() * weight;
173 totalWeight += weight;
176 means[featureClass] = QList<double>() << cx << cy << totalWeight;
180 step = !means.empty() ? 50.0 / means.count() : 1;
181 for (
auto it = means.constBegin(); it != means.constEnd(); ++it )
194 const double cx = it.value().at( 0 ) / it.value().at( 2 );
195 const double cy = it.value().at( 1 ) / it.value().at( 2 );
201 attributes << cx << cy;
202 if ( uniqueFieldIndex >= 0 )
203 attributes.append( it.key() );
213 outputs.insert( QStringLiteral(
"OUTPUT" ), 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.
This class 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.
A class to represent a 2D point.
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.