24 QString QgsCalculateVectorOverlapsAlgorithm::name()
const
26 return QStringLiteral(
"calculatevectoroverlaps" );
29 QString QgsCalculateVectorOverlapsAlgorithm::displayName()
const
31 return QObject::tr(
"Overlap analysis" );
34 QStringList QgsCalculateVectorOverlapsAlgorithm::tags()
const
36 return QObject::tr(
"vector,overlay,area,percentage,intersection" ).split(
',' );
39 QString QgsCalculateVectorOverlapsAlgorithm::group()
const
41 return QObject::tr(
"Vector analysis" );
44 QString QgsCalculateVectorOverlapsAlgorithm::groupId()
const
46 return QStringLiteral(
"vectoranalysis" );
49 void QgsCalculateVectorOverlapsAlgorithm::initAlgorithm(
const QVariantMap & )
56 QIcon QgsCalculateVectorOverlapsAlgorithm::icon()
const
61 QString QgsCalculateVectorOverlapsAlgorithm::svgIconPath()
const
66 QString QgsCalculateVectorOverlapsAlgorithm::shortHelpString()
const
68 return QObject::tr(
"This algorithm calculates the area and percentage cover by which features from an input layer "
69 "are overlapped by features from a selection of overlay layers.\n\n"
70 "New attributes are added to the output layer reporting the total area of overlap and percentage of the input feature overlapped "
71 "by each of the selected overlay layers." );
74 QgsCalculateVectorOverlapsAlgorithm *QgsCalculateVectorOverlapsAlgorithm::createInstance()
const
76 return new QgsCalculateVectorOverlapsAlgorithm();
81 mSource.reset( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
85 mOutputFields = mSource->fields();
87 const QList< QgsMapLayer * > layers = parameterAsLayerList( parameters, QStringLiteral(
"LAYERS" ), context );
88 mOverlayerSources.reserve( layers.size() );
89 mLayerNames.reserve( layers.size() );
92 if (
QgsVectorLayer *vl = qobject_cast< QgsVectorLayer * >( layer ) )
94 mLayerNames << layer->name();
95 mOverlayerSources.emplace_back( qgis::make_unique< QgsVectorLayerFeatureSource >( vl ) );
96 mOutputFields.append(
QgsField( QStringLiteral(
"%1_area" ).arg( vl->name() ), QVariant::Double ) );
97 mOutputFields.append(
QgsField( QStringLiteral(
"%1_pc" ).arg( vl->name() ), QVariant::Double ) );
101 mOutputType = mSource->wkbType();
102 mCrs = mSource->sourceCrs();
103 mInputCount = mSource->featureCount();
104 mInputFeatures = mSource->getFeatures();
111 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, destId, mOutputFields,
112 mOutputType, mCrs ) );
118 QList< QgsSpatialIndex > spatialIndices;
119 spatialIndices.reserve( mLayerNames.size() );
120 auto nameIt = mLayerNames.constBegin();
121 for (
auto sourceIt = mOverlayerSources.begin(); sourceIt != mOverlayerSources.end(); ++sourceIt, ++nameIt )
123 feedback->
pushInfo( QObject::tr(
"Preparing %1" ).arg( *nameIt ) );
133 double step = mInputCount > 0 ? 100.0 / mInputCount : 0;
136 while ( mInputFeatures.nextFeature( feature ) )
145 const double inputArea = da.
measureArea( inputGeom );
149 bufferGeomEngine->prepareGeometry();
152 auto spatialIteratorIt = spatialIndices.begin();
153 for (
auto it = mOverlayerSources.begin(); it != mOverlayerSources.end(); ++ it, ++spatialIteratorIt )
160 QVector< QgsGeometry > intersectingGeoms;
161 intersectingGeoms.reserve( matches.count() );
168 if ( bufferGeomEngine->intersects( overlayGeometry.
constGet() ) )
170 intersectingGeoms.append( overlayGeometry );
185 const double overlayArea = da.
measureArea( overlayIntersection );
186 outAttributes.append( overlayArea );
187 outAttributes.append( 100 * overlayArea / inputArea );
193 for (
auto it = mOverlayerSources.begin(); it != mOverlayerSources.end(); ++ it )
195 outAttributes.append( QVariant() );
196 outAttributes.append( QVariant() );
208 outputs.insert( QStringLiteral(
"OUTPUT" ), destId );