27using namespace Qt::StringLiterals;
31void QgsPointsInPolygonAlgorithm::initParameters(
const QVariantMap &configuration )
33 mIsInPlace = configuration.value( u
"IN_PLACE"_s ).toBool();
48QString QgsPointsInPolygonAlgorithm::name()
const
50 return u
"countpointsinpolygon"_s;
53QString QgsPointsInPolygonAlgorithm::displayName()
const
55 return QObject::tr(
"Count points in polygon" );
58QStringList QgsPointsInPolygonAlgorithm::tags()
const
60 return QObject::tr(
"extract,filter,intersects,intersecting,disjoint,touching,within,contains,overlaps,relation" ).split(
',' );
63QString QgsPointsInPolygonAlgorithm::svgIconPath()
const
68QIcon QgsPointsInPolygonAlgorithm::icon()
const
73QString QgsPointsInPolygonAlgorithm::group()
const
75 return QObject::tr(
"Vector analysis" );
78QString QgsPointsInPolygonAlgorithm::groupId()
const
80 return u
"vectoranalysis"_s;
83QString QgsPointsInPolygonAlgorithm::shortHelpString()
const
86 "This algorithm takes a points layer and a polygon layer and counts the number of points from "
87 "the first one in each polygons of the second one.\n\n"
88 "A new polygons layer is generated, with the exact same content as the input polygons layer, but "
89 "containing an additional field with the points count corresponding to each polygon.\n\n"
90 "An optional weight field can be used to assign weights to each point. If set, the count generated "
91 "will be the sum of the weight field for each point contained by the polygon.\n\n"
92 "Alternatively, a unique class field can be specified. If set, points are classified based on "
93 "the selected attribute, and if several points with the same attribute value are within the polygon, "
94 "only one of them is counted. The final count of the point in a polygon is, therefore, the count of "
95 "different classes that are found in it.\n\n"
96 "Both the weight field and unique class field cannot be specified. If they are, the weight field will "
97 "take precedence and the unique class field will be ignored."
101QString QgsPointsInPolygonAlgorithm::shortDescription()
const
103 return QObject::tr(
"Counts point features located within polygon features." );
106QgsPointsInPolygonAlgorithm *QgsPointsInPolygonAlgorithm::createInstance()
const
108 return new QgsPointsInPolygonAlgorithm();
111QList<int> QgsPointsInPolygonAlgorithm::inputLayerTypes()
const
127QString QgsPointsInPolygonAlgorithm::inputParameterName()
const
129 return u
"POLYGONS"_s;
132QString QgsPointsInPolygonAlgorithm::inputParameterDescription()
const
134 return QObject::tr(
"Polygons" );
137QString QgsPointsInPolygonAlgorithm::outputName()
const
139 return QObject::tr(
"Count" );
144 mFieldName = parameterAsString( parameters, u
"FIELD"_s, context );
145 mWeightFieldName = parameterAsString( parameters, u
"WEIGHT"_s, context );
146 mClassFieldName = parameterAsString( parameters, u
"CLASSFIELD"_s, context );
147 mPointSource.reset( parameterAsSource( parameters, u
"POINTS"_s, context ) );
151 if ( !mWeightFieldName.isEmpty() )
153 mWeightFieldIndex = mPointSource->fields().lookupField( mWeightFieldName );
154 if ( mWeightFieldIndex == -1 )
156 mPointAttributes.append( mWeightFieldIndex );
159 if ( !mClassFieldName.isEmpty() )
161 mClassFieldIndex = mPointSource->fields().lookupField( mClassFieldName );
162 if ( mClassFieldIndex == -1 )
164 mPointAttributes.append( mClassFieldIndex );
168 feedback->
pushWarning( QObject::tr(
"No spatial index exists for points layer, performance will be severely degraded" ) );
179 if ( mDestFieldIndex < 0 )
182 attrs[mDestFieldIndex] = 0;
184 return QList<QgsFeature>() << outputFeature;
190 engine->prepareGeometry();
193 QSet<QVariant> classes;
208 if ( mWeightFieldIndex >= 0 )
210 const QVariant weight = pointFeature.
attribute( mWeightFieldIndex );
211 const double pointWeight = weight.toDouble( &ok );
214 count += pointWeight;
216 feedback->
reportError( QObject::tr(
"Weight field value “%1” is not a numeric value" ).arg( weight.toString() ) );
218 else if ( mClassFieldIndex >= 0 )
220 const QVariant pointClass = pointFeature.
attribute( mClassFieldIndex );
221 classes.insert( pointClass );
233 if ( mClassFieldIndex >= 0 )
234 score = classes.size();
238 if ( mDestFieldIndex < 0 )
239 attrs.append( score );
241 attrs[mDestFieldIndex] = score;
244 return QList<QgsFeature>() << outputFeature;
248QgsFields QgsPointsInPolygonAlgorithm::outputFields(
const QgsFields &inputFields )
const
252 mDestFieldIndex = inputFields.
lookupField( mFieldName );
258 mDestFieldIndex = inputFields.
lookupField( mFieldName );
259 if ( mDestFieldIndex < 0 )
260 outFields.
append(
QgsField( mFieldName, QMetaType::Type::Double ) );
267bool QgsPointsInPolygonAlgorithm::supportInPlaceEdit(
const QgsMapLayer *layer )
const
269 if (
const QgsVectorLayer *vl = qobject_cast<const QgsVectorLayer *>( layer ) )
ProcessingSourceType
Processing data source types.
@ VectorPoint
Vector point layers.
@ VectorPolygon
Vector polygon layers.
@ NotPresent
No spatial index exists for the source.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static QString iconPath(const QString &iconFile)
Returns path to the desired icon file.
Represents a coordinate reference system (CRS).
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).
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
QgsFeatureRequest & setDestinationCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets the destination crs for feature's geometries.
QgsFeatureRequest & setFilterRect(const QgsRectangle &rectangle)
Sets the rectangle from which features will be taken.
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.
bool isCanceled() const
Tells whether the operation has been canceled already.
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.
Q_INVOKABLE int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
A geometry is the spatial representation of a feature.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
static QgsGeometryEngine * createGeometryEngine(const QgsAbstractGeometry *geometry, double precision=0.0, Qgis::GeosCreationFlags flags=Qgis::GeosCreationFlag::SkipEmptyInteriorRings)
Creates and returns a new geometry engine representing the specified geometry using precision on a gr...
Base class for all map layer types.
Contains information about the context in which a processing algorithm is executed.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void pushWarning(const QString &warning)
Pushes a warning informational message from the algorithm.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
An input feature source (such as vector layers) parameter for processing algorithms.
A vector layer or feature source field parameter for processing algorithms.
A string parameter for processing algorithms.
Represents a vector layer which manages a vector based dataset.
QList< QgsFeature > QgsFeatureList