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 );