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 );
90 if (
QgsVectorLayer *vl = qobject_cast< QgsVectorLayer * >( layer ) )
92 mOverlayerSources[ layer->name() ] = qgis::make_unique< QgsVectorLayerFeatureSource >( vl );
93 mOutputFields.append(
QgsField( QStringLiteral(
"%1_area" ).arg( vl->name() ), QVariant::Double ) );
94 mOutputFields.append(
QgsField( QStringLiteral(
"%1_pc" ).arg( vl->name() ), QVariant::Double ) );
98 mOutputType = mSource->wkbType();
99 mCrs = mSource->sourceCrs();
100 mInputCount = mSource->featureCount();
101 mInputFeatures = mSource->getFeatures();
108 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, destId, mOutputFields,
109 mOutputType, mCrs ) );
115 QMap< QString, QgsSpatialIndex > spatialIndices;
116 for (
auto it = mOverlayerSources.begin(); it != mOverlayerSources.end(); ++ it )
118 feedback->
pushInfo( QObject::tr(
"Preparing %1" ).arg( it->first ) );
128 double step = mInputCount > 0 ? 100.0 / mInputCount : 0;
131 while ( mInputFeatures.nextFeature( feature ) )
140 const double inputArea = da.
measureArea( inputGeom );
144 bufferGeomEngine->prepareGeometry();
148 for (
auto it = mOverlayerSources.begin(); it != mOverlayerSources.end(); ++ it )
155 QVector< QgsGeometry > intersectingGeoms;
156 intersectingGeoms.reserve( matches.count() );
163 if ( bufferGeomEngine->intersects( overlayGeometry.
constGet() ) )
165 intersectingGeoms.append( overlayGeometry );
180 const double overlayArea = da.
measureArea( overlayIntersection );
181 outAttributes.append( overlayArea );
182 outAttributes.append( 100 * overlayArea / inputArea );
188 for (
auto it = mOverlayerSources.begin(); it != mOverlayerSources.end(); ++ it )
190 outAttributes.append( QVariant() );
191 outAttributes.append( QVariant() );
203 outputs.insert( QStringLiteral(
"OUTPUT" ), destId );
Wrapper for iterator of features from vector data provider or vector layer.
A parameter for processing algorithms which accepts multiple map layers.
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
Base class for all map layer types.
Base class for providing feedback from a processing algorithm.
QgsGeometry geometry(QgsFeatureId id) const
Returns the stored geometry for the indexed feature with matching id.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
void setProgress(double progress)
Sets the current progress for the feedback object.
static QString iconPath(const QString &iconFile)
Returns path to the desired icon file.
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.
bool setEllipsoid(const QString &ellipsoid)
Sets the ellipsoid by its acronym.
QgsGeometry intersection(const QgsGeometry &geometry) const
Returns a geometry representing the points shared by this geometry and other.
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.
A feature sink output for processing algorithms.
QgsProject * project() const
Returns the project in which the algorithm is being executed.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
QgsFeatureRequest::InvalidGeometryCheck invalidGeometryCheck() const
Returns the behavior used for checking invalid geometries in input layers.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
Custom exception class for processing related exceptions.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
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.
A general purpose distance and area calculator, capable of performing ellipsoid based calculations...
A spatial index for QgsFeature objects.
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.
std::function< void(const QgsFeature &) > invalidGeometryCallback() const
Returns the callback function to use when encountering an invalid geometry and invalidGeometryCheck()...
void setSourceCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets source spatial reference system crs.
QList< int > QgsAttributeList
double area() const
Returns the area of the geometry using GEOS.
Represents a vector layer which manages a vector based data sets.
Contains information about the context in which a processing algorithm is executed.
static QgsGeometry unaryUnion(const QVector< QgsGeometry > &geometries)
Compute the unary union on a list of geometries.
QList< QgsFeatureId > intersects(const QgsRectangle &rectangle) const
Returns a list of features with a bounding box which intersects the specified rectangle.
double measureArea(const QgsGeometry &geometry) const
Measures the area of a geometry.
Indicates that the spatial index should also store feature geometries. This requires more memory...