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 )