22 QString QgsMeanCoordinatesAlgorithm::name()
const
24 return QStringLiteral(
"meancoordinates" );
27 QString QgsMeanCoordinatesAlgorithm::displayName()
const
29 return QObject::tr(
"Mean coordinate(s)" );
32 QStringList QgsMeanCoordinatesAlgorithm::tags()
const
34 return QObject::tr(
"mean,average,coordinate" ).split(
',' );
37 QString QgsMeanCoordinatesAlgorithm::group()
const
39 return QObject::tr(
"Vector analysis" );
42 QString QgsMeanCoordinatesAlgorithm::groupId()
const
44 return QStringLiteral(
"vectoranalysis" );
47 void QgsMeanCoordinatesAlgorithm::initAlgorithm(
const QVariantMap & )
52 QVariant(), QStringLiteral(
"INPUT" ),
55 QObject::tr(
"Unique ID field" ), QVariant(),
60 QString QgsMeanCoordinatesAlgorithm::shortHelpString()
const
62 return QObject::tr(
"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." );
69 QgsMeanCoordinatesAlgorithm *QgsMeanCoordinatesAlgorithm::createInstance()
const
71 return new QgsMeanCoordinatesAlgorithm();
76 std::unique_ptr< QgsProcessingFeatureSource > source( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
80 QString weightFieldName = parameterAsString( parameters, QStringLiteral(
"WEIGHT" ), context );
81 QString uniqueFieldName = parameterAsString( parameters, QStringLiteral(
"UID" ), context );
85 if ( !weightFieldName.isEmpty() )
87 weightIndex = source->fields().lookupField( weightFieldName );
88 if ( weightIndex >= 0 )
89 attributes.append( weightIndex );
92 int uniqueFieldIndex = -1;
93 if ( !uniqueFieldName.isEmpty() )
95 uniqueFieldIndex = source->fields().lookupField( uniqueFieldName );
96 if ( uniqueFieldIndex >= 0 )
97 attributes.append( uniqueFieldIndex );
101 fields.
append(
QgsField( QStringLiteral(
"MEAN_X" ), QVariant::Double, QString(), 24, 15 ) );
102 fields.
append(
QgsField( QStringLiteral(
"MEAN_Y" ), QVariant::Double, QString(), 24, 15 ) );
103 if ( uniqueFieldIndex >= 0 )
105 QgsField uniqueField = source->fields().at( uniqueFieldIndex );
106 fields.
append( uniqueField );
110 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, fields,
117 double step = source->featureCount() > 0 ? 50.0 / source->featureCount() : 1;
121 QHash< QVariant, QList< double > > means;
135 QVariant featureClass;
136 if ( uniqueFieldIndex >= 0 )
138 featureClass = feat.
attribute( uniqueFieldIndex );
142 featureClass = QStringLiteral(
"#####singleclass#####" );
146 if ( weightIndex >= 0 )
149 weight = feat.
attribute( weightIndex ).toDouble( &ok );
156 throw QgsProcessingException( QObject::tr(
"Negative weight value found. Please fix your data and try again." ) );
159 QList< double > values = means.value( featureClass );
162 double totalWeight = 0;
163 if ( !values.empty() )
167 totalWeight = values.at( 2 );
177 cx += pt.
x() * weight;
178 cy += pt.
y() * weight;
179 totalWeight += weight;
182 means[featureClass] = QList< double >() << cx << cy << totalWeight;
186 step = !means.empty() ? 50.0 / means.count() : 1;
187 for (
auto it = means.constBegin(); it != means.constEnd(); ++it )
200 double cx = it.value().at( 0 ) / it.value().at( 2 );
201 double cy = it.value().at( 1 ) / it.value().at( 2 );
207 attributes << cx << cy;
208 if ( uniqueFieldIndex >= 0 )
209 attributes.append( it.key() );
216 outputs.insert( QStringLiteral(
"OUTPUT" ), dest );
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)
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.
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 SIP_HOLDGIL
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, FieldOrigin origin=OriginProvider, int originIndex=-1)
Appends a field. The field must have unique name, otherwise it is rejected (returns false)
const QgsAbstractGeometry * constGet() const SIP_HOLDGIL
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
static QgsGeometry fromPointXY(const QgsPointXY &point) SIP_HOLDGIL
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.
@ FlagSkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
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.
@ Numeric
Accepts numeric fields.
@ TypeVectorPoint
Vector point layers.
@ TypeVectorAnyGeometry
Any vector layer with geometry.
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.