26 void QgsPointsInPolygonAlgorithm::initParameters(
const QVariantMap & )
35 QObject::tr(
"Count field name" ), QStringLiteral(
"NUMPOINTS" ) ) );
38 QString QgsPointsInPolygonAlgorithm::name()
const 40 return QStringLiteral(
"countpointsinpolygon" );
43 QString QgsPointsInPolygonAlgorithm::displayName()
const 45 return QObject::tr(
"Count points in polygon" );
48 QStringList QgsPointsInPolygonAlgorithm::tags()
const 50 return QObject::tr(
"extract,filter,intersects,intersecting,disjoint,touching,within,contains,overlaps,relation" ).split(
',' );
53 QString QgsPointsInPolygonAlgorithm::svgIconPath()
const 58 QIcon QgsPointsInPolygonAlgorithm::icon()
const 63 QString QgsPointsInPolygonAlgorithm::group()
const 65 return QObject::tr(
"Vector analysis" );
68 QString QgsPointsInPolygonAlgorithm::groupId()
const 70 return QStringLiteral(
"vectoranalysis" );
73 QString QgsPointsInPolygonAlgorithm::shortHelpString()
const 75 return QObject::tr(
"This algorithm takes a points layer and a polygon layer and counts the number of points from " 76 "the first one in each polygons of the second one.\n\n" 77 "A new polygons layer is generated, with the exact same content as the input polygons layer, but " 78 "containing an additional field with the points count corresponding to each polygon.\n\n" 79 "An optional weight field can be used to assign weights to each point. If set, the count generated " 80 "will be the sum of the weight field for each point contained by the polygon.\n\n" 81 "Alternatively, a unique class field can be specified. If set, points are classified based on " 82 "the selected attribute, and if several points with the same attribute value are within the polygon, " 83 "only one of them is counted. The final count of the point in a polygon is, therefore, the count of " 84 "different classes that are found in it.\n\n" 85 "Both the weight field and unique class field cannot be specified. If they are, the weight field will " 86 "take precedence and the unique class field will be ignored." );
89 QString QgsPointsInPolygonAlgorithm::shortDescription()
const 91 return QObject::tr(
"Counts point features located within polygon features." );
94 QgsPointsInPolygonAlgorithm *QgsPointsInPolygonAlgorithm::createInstance()
const 96 return new QgsPointsInPolygonAlgorithm();
99 QList<int> QgsPointsInPolygonAlgorithm::inputLayerTypes()
const 115 QString QgsPointsInPolygonAlgorithm::inputParameterName()
const 117 return QStringLiteral(
"POLYGONS" );
120 QString QgsPointsInPolygonAlgorithm::inputParameterDescription()
const 122 return QObject::tr(
"Polygons" );
125 QString QgsPointsInPolygonAlgorithm::outputName()
const 127 return QObject::tr(
"Count" );
132 mFieldName = parameterAsString( parameters, QStringLiteral(
"FIELD" ), context );
133 mWeightFieldName = parameterAsString( parameters, QStringLiteral(
"WEIGHT" ), context );
134 mClassFieldName = parameterAsString( parameters, QStringLiteral(
"CLASSFIELD" ), context );
135 mPointSource.reset( parameterAsSource( parameters, QStringLiteral(
"POINTS" ), context ) );
139 if ( !mWeightFieldName.isEmpty() )
141 mWeightFieldIndex = mPointSource->fields().lookupField( mWeightFieldName );
142 if ( mWeightFieldIndex == -1 )
144 mPointAttributes.append( mWeightFieldIndex );
147 if ( !mClassFieldName.isEmpty() )
149 mClassFieldIndex = mPointSource->fields().lookupField( mClassFieldName );
150 if ( mClassFieldIndex == -1 )
152 mPointAttributes.append( mClassFieldIndex );
156 feedback->
reportError( QObject::tr(
"No spatial index exists for points layer, performance will be severely degraded" ) );
167 if ( mDestFieldIndex < 0 )
170 attrs[mDestFieldIndex] = 0;
172 return QList< QgsFeature > () << outputFeature;
178 engine->prepareGeometry();
181 QSet< QVariant> classes;
196 if ( mWeightFieldIndex >= 0 )
198 const QVariant weight = pointFeature.
attribute( mWeightFieldIndex );
199 double pointWeight = weight.toDouble( &ok );
202 count += pointWeight;
204 feedback->
reportError( QObject::tr(
"Weight field value “%1” is not a numeric value" ).arg( weight.toString() ) );
206 else if ( mClassFieldIndex >= 0 )
208 const QVariant pointClass = pointFeature.
attribute( mClassFieldIndex );
209 classes.insert( pointClass );
221 if ( mClassFieldIndex >= 0 )
222 score = classes.size();
226 if ( mDestFieldIndex < 0 )
227 attrs.append( score );
229 attrs[mDestFieldIndex] = score;
232 return QList< QgsFeature >() << outputFeature;
236 QgsFields QgsPointsInPolygonAlgorithm::outputFields(
const QgsFields &inputFields )
const 239 mDestFieldIndex = inputFields.
lookupField( mFieldName );
240 if ( mDestFieldIndex < 0 )
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
Wrapper for iterator of features from vector data provider or vector layer.
No spatial index exists for the source.
Base class for providing feedback from a processing algorithm.
A vector layer or feature source field parameter for processing algorithms.
QList< QgsFeature > QgsFeatureList
static QString iconPath(const QString &iconFile)
Returns path to the desired icon file.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
Container of fields for a vector layer.
A geometry is the spatial representation of a feature.
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
bool hasGeometry() const
Returns true if the feature has an associated geometry.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
Custom exception class for processing related exceptions.
QgsFeatureRequest & setFilterRect(const QgsRectangle &rectangle)
Sets the rectangle from which features will be taken.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
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) ...
Encapsulate a field in an attribute table or data source.
static QgsGeometryEngine * createGeometryEngine(const QgsAbstractGeometry *geometry)
Creates and returns a new geometry engine.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
bool isCanceled() const
Tells whether the operation has been canceled already.
An input feature source (such as vector layers) parameter for processing algorithms.
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
This class represents a coordinate reference system (CRS).
SourceType
Data source types enum.
const QgsCoordinateReferenceSystem & outputCrs
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
bool nextFeature(QgsFeature &f)
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Contains information about the context in which a processing algorithm is executed.
A string parameter for processing algorithms.