25QString QgsCalculateVectorOverlapsAlgorithm::name()
const
27 return QStringLiteral(
"calculatevectoroverlaps" );
30QString QgsCalculateVectorOverlapsAlgorithm::displayName()
const
32 return QObject::tr(
"Overlap analysis" );
35QStringList QgsCalculateVectorOverlapsAlgorithm::tags()
const
37 return QObject::tr(
"vector,overlay,area,percentage,intersection" ).split(
',' );
40QString QgsCalculateVectorOverlapsAlgorithm::group()
const
42 return QObject::tr(
"Vector analysis" );
45QString QgsCalculateVectorOverlapsAlgorithm::groupId()
const
47 return QStringLiteral(
"vectoranalysis" );
50void QgsCalculateVectorOverlapsAlgorithm::initAlgorithm(
const QVariantMap & )
56 std::unique_ptr< QgsProcessingParameterNumber > gridSize = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral(
"GRID_SIZE" ),
59 addParameter( gridSize.release() );
62QIcon QgsCalculateVectorOverlapsAlgorithm::icon()
const
67QString QgsCalculateVectorOverlapsAlgorithm::svgIconPath()
const
72QString QgsCalculateVectorOverlapsAlgorithm::shortHelpString()
const
74 return QObject::tr(
"This algorithm calculates the area and percentage cover by which features from an input layer "
75 "are overlapped by features from a selection of overlay layers.\n\n"
76 "New attributes are added to the output layer reporting the total area of overlap and percentage of the input feature overlapped "
77 "by each of the selected overlay layers." );
80QgsCalculateVectorOverlapsAlgorithm *QgsCalculateVectorOverlapsAlgorithm::createInstance()
const
82 return new QgsCalculateVectorOverlapsAlgorithm();
87 mSource.reset( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
91 mOutputFields = mSource->fields();
93 const QList< QgsMapLayer * > layers = parameterAsLayerList( parameters, QStringLiteral(
"LAYERS" ), context );
94 mOverlayerSources.reserve( layers.size() );
95 mLayerNames.reserve( layers.size() );
98 if (
QgsVectorLayer *vl = qobject_cast< QgsVectorLayer * >( layer ) )
100 mLayerNames << layer->name();
101 mOverlayerSources.emplace_back( std::make_unique< QgsVectorLayerFeatureSource >( vl ) );
102 mOutputFields.append(
QgsField( QStringLiteral(
"%1_area" ).arg( vl->name() ), QVariant::Double ) );
103 mOutputFields.append(
QgsField( QStringLiteral(
"%1_pc" ).arg( vl->name() ), QVariant::Double ) );
107 mOutputType = mSource->wkbType();
108 mCrs = mSource->sourceCrs();
109 mInputCount = mSource->featureCount();
110 mInputFeatures = mSource->getFeatures();
117 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, destId, mOutputFields,
118 mOutputType, mCrs ) );
124 QList< QgsSpatialIndex > spatialIndices;
125 spatialIndices.reserve( mLayerNames.size() );
126 auto nameIt = mLayerNames.constBegin();
127 for (
auto sourceIt = mOverlayerSources.begin(); sourceIt != mOverlayerSources.end(); ++sourceIt, ++nameIt )
129 feedback->
pushInfo( QObject::tr(
"Preparing %1" ).arg( *nameIt ) );
139 if ( parameters.value( QStringLiteral(
"GRID_SIZE" ) ).isValid() )
141 geometryParameters.
setGridSize( parameterAsDouble( parameters, QStringLiteral(
"GRID_SIZE" ), context ) );
145 const double step = mInputCount > 0 ? 100.0 / mInputCount : 0;
148 while ( mInputFeatures.nextFeature( feature ) )
157 const double inputArea = da.
measureArea( inputGeom );
161 bufferGeomEngine->prepareGeometry();
164 auto spatialIteratorIt = spatialIndices.begin();
165 for (
auto it = mOverlayerSources.begin(); it != mOverlayerSources.end(); ++ it, ++spatialIteratorIt )
172 QVector< QgsGeometry > intersectingGeoms;
173 intersectingGeoms.reserve( matches.count() );
180 if ( bufferGeomEngine->intersects( overlayGeometry.
constGet() ) )
182 intersectingGeoms.append( overlayGeometry );
197 const double overlayArea = da.
measureArea( overlayIntersection );
198 outAttributes.append( overlayArea );
199 outAttributes.append( 100 * overlayArea / inputArea );
205 for (
auto it = mOverlayerSources.begin(); it != mOverlayerSources.end(); ++ it )
207 outAttributes.append( QVariant() );
208 outAttributes.append( QVariant() );
221 outputs.insert( QStringLiteral(
"OUTPUT" ), destId );
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static QString iconPath(const QString &iconFile)
Returns path to the desired icon file.
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
double measureArea(const QgsGeometry &geometry) const
Measures the area of a geometry.
void setSourceCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets source spatial reference system crs.
bool setEllipsoid(const QString &ellipsoid)
Sets the ellipsoid by its acronym.
Wrapper for iterator of features from vector data provider or vector layer.
This class wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setInvalidGeometryCallback(const std::function< void(const QgsFeature &)> &callback)
Sets a callback function to use when encountering an invalid geometry and invalidGeometryCheck() is s...
QgsFeatureRequest & setInvalidGeometryCheck(InvalidGeometryCheck check)
Sets invalid geometry checking behavior.
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
bool hasGeometry() const
Returns true if the feature has an associated geometry.
bool isCanceled() const SIP_HOLDGIL
Tells whether the operation has been canceled already.
void setProgress(double progress)
Sets the current progress for the feedback object.
Encapsulate a field in an attribute table or data source.
Encapsulates parameters under which a geometry operation is performed.
void setGridSize(double size)
Sets the grid size which will be used to snap vertices of a geometry.
A geometry is the spatial representation of a feature.
const QgsAbstractGeometry * constGet() const SIP_HOLDGIL
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
double area() const
Returns the planar, 2-dimensional area of the geometry.
static QgsGeometryEngine * createGeometryEngine(const QgsAbstractGeometry *geometry)
Creates and returns a new geometry engine representing the specified geometry.
QgsGeometry intersection(const QgsGeometry &geometry, const QgsGeometryParameters ¶meters=QgsGeometryParameters()) const
Returns a geometry representing the points shared by this geometry and other.
static QgsGeometry unaryUnion(const QVector< QgsGeometry > &geometries, const QgsGeometryParameters ¶meters=QgsGeometryParameters())
Compute the unary union on a list of geometries.
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
Base class for all map layer types.
Contains information about the context in which a processing algorithm is executed.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
std::function< void(const QgsFeature &) > invalidGeometryCallback(QgsFeatureSource *source=nullptr) const
Returns the callback function to use when encountering an invalid geometry and invalidGeometryCheck()...
QString ellipsoid() const
Returns the ellipsoid to use for distance and area calculations.
QgsFeatureRequest::InvalidGeometryCheck invalidGeometryCheck() const
Returns the behavior used for checking invalid geometries in input layers.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
@ FlagAdvanced
Parameter is an advanced parameter which should be hidden from users by default.
A feature sink output for processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.
A parameter for processing algorithms which accepts multiple map layers.
@ Double
Double/float values.
@ TypeVectorPolygon
Vector polygon layers.
A spatial index for QgsFeature objects.
@ FlagStoreFeatureGeometries
Indicates that the spatial index should also store feature geometries. This requires more memory,...
QList< QgsFeatureId > intersects(const QgsRectangle &rectangle) const
Returns a list of features with a bounding box which intersects the specified rectangle.
QgsGeometry geometry(QgsFeatureId id) const
Returns the stored geometry for the indexed feature with matching id.
Represents a vector layer which manages a vector based data sets.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
QList< int > QgsAttributeList