26 void QgsPointsInPolygonAlgorithm::initParameters(
const QVariantMap &configuration )
28 mIsInPlace = configuration.value( QStringLiteral(
"IN_PLACE" ) ).toBool();
39 QObject::tr(
"Count field" ), QStringLiteral(
"NUMPOINTS" ), inputParameterName() ) );
44 QObject::tr(
"Count field name" ), QStringLiteral(
"NUMPOINTS" ) ) );
48 QString QgsPointsInPolygonAlgorithm::name()
const
50 return QStringLiteral(
"countpointsinpolygon" );
53 QString QgsPointsInPolygonAlgorithm::displayName()
const
55 return QObject::tr(
"Count points in polygon" );
58 QStringList QgsPointsInPolygonAlgorithm::tags()
const
60 return QObject::tr(
"extract,filter,intersects,intersecting,disjoint,touching,within,contains,overlaps,relation" ).split(
',' );
63 QString QgsPointsInPolygonAlgorithm::svgIconPath()
const
68 QIcon QgsPointsInPolygonAlgorithm::icon()
const
73 QString QgsPointsInPolygonAlgorithm::group()
const
75 return QObject::tr(
"Vector analysis" );
78 QString QgsPointsInPolygonAlgorithm::groupId()
const
80 return QStringLiteral(
"vectoranalysis" );
83 QString QgsPointsInPolygonAlgorithm::shortHelpString()
const
85 return QObject::tr(
"This algorithm takes a points layer and a polygon layer and counts the number of points from "
86 "the first one in each polygons of the second one.\n\n"
87 "A new polygons layer is generated, with the exact same content as the input polygons layer, but "
88 "containing an additional field with the points count corresponding to each polygon.\n\n"
89 "An optional weight field can be used to assign weights to each point. If set, the count generated "
90 "will be the sum of the weight field for each point contained by the polygon.\n\n"
91 "Alternatively, a unique class field can be specified. If set, points are classified based on "
92 "the selected attribute, and if several points with the same attribute value are within the polygon, "
93 "only one of them is counted. The final count of the point in a polygon is, therefore, the count of "
94 "different classes that are found in it.\n\n"
95 "Both the weight field and unique class field cannot be specified. If they are, the weight field will "
96 "take precedence and the unique class field will be ignored." );
99 QString QgsPointsInPolygonAlgorithm::shortDescription()
const
101 return QObject::tr(
"Counts point features located within polygon features." );
104 QgsPointsInPolygonAlgorithm *QgsPointsInPolygonAlgorithm::createInstance()
const
106 return new QgsPointsInPolygonAlgorithm();
109 QList<int> QgsPointsInPolygonAlgorithm::inputLayerTypes()
const
125 QString QgsPointsInPolygonAlgorithm::inputParameterName()
const
127 return QStringLiteral(
"POLYGONS" );
130 QString QgsPointsInPolygonAlgorithm::inputParameterDescription()
const
132 return QObject::tr(
"Polygons" );
135 QString QgsPointsInPolygonAlgorithm::outputName()
const
137 return QObject::tr(
"Count" );
142 mFieldName = parameterAsString( parameters, QStringLiteral(
"FIELD" ), context );
143 mWeightFieldName = parameterAsString( parameters, QStringLiteral(
"WEIGHT" ), context );
144 mClassFieldName = parameterAsString( parameters, QStringLiteral(
"CLASSFIELD" ), context );
145 mPointSource.reset( parameterAsSource( parameters, QStringLiteral(
"POINTS" ), context ) );
149 if ( !mWeightFieldName.isEmpty() )
151 mWeightFieldIndex = mPointSource->fields().lookupField( mWeightFieldName );
152 if ( mWeightFieldIndex == -1 )
154 mPointAttributes.append( mWeightFieldIndex );
157 if ( !mClassFieldName.isEmpty() )
159 mClassFieldIndex = mPointSource->fields().lookupField( mClassFieldName );
160 if ( mClassFieldIndex == -1 )
162 mPointAttributes.append( mClassFieldIndex );
166 feedback->
pushWarning( QObject::tr(
"No spatial index exists for points layer, performance will be severely degraded" ) );
177 if ( mDestFieldIndex < 0 )
180 attrs[mDestFieldIndex] = 0;
182 return QList< QgsFeature > () << outputFeature;
188 engine->prepareGeometry();
191 QSet< QVariant> classes;
206 if ( mWeightFieldIndex >= 0 )
208 const QVariant weight = pointFeature.
attribute( mWeightFieldIndex );
209 const double pointWeight = weight.toDouble( &ok );
212 count += pointWeight;
214 feedback->
reportError( QObject::tr(
"Weight field value “%1” is not a numeric value" ).arg( weight.toString() ) );
216 else if ( mClassFieldIndex >= 0 )
218 const QVariant pointClass = pointFeature.
attribute( mClassFieldIndex );
219 classes.insert( pointClass );
231 if ( mClassFieldIndex >= 0 )
232 score = classes.size();
236 if ( mDestFieldIndex < 0 )
237 attrs.append( score );
239 attrs[mDestFieldIndex] = score;
242 return QList< QgsFeature >() << outputFeature;
246 QgsFields QgsPointsInPolygonAlgorithm::outputFields(
const QgsFields &inputFields )
const
250 mDestFieldIndex = inputFields.
lookupField( mFieldName );
256 mDestFieldIndex = inputFields.
lookupField( mFieldName );
257 if ( mDestFieldIndex < 0 )
265 bool QgsPointsInPolygonAlgorithm::supportInPlaceEdit(
const QgsMapLayer *layer )
const
267 if (
const QgsVectorLayer *vl = qobject_cast< const QgsVectorLayer * >( layer ) )