QGIS API Documentation 3.99.0-Master (357b655ed83)
Loading...
Searching...
No Matches
qgsalgorithmmultidifference.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmmultidifference.cpp
3 ------------------
4 begin : December 2021
5 copyright : (C) 2021 by Alexander Bruy
6 email : alexander dot bruy at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
20#include "qgsoverlayutils.h"
21#include "qgsvectorlayer.h"
22
23#include <QString>
24
25using namespace Qt::StringLiterals;
26
28
29
30QString QgsMultiDifferenceAlgorithm::name() const
31{
32 return u"multidifference"_s;
33}
34
35QString QgsMultiDifferenceAlgorithm::displayName() const
36{
37 return QObject::tr( "Difference (multiple)" );
38}
39
40QStringList QgsMultiDifferenceAlgorithm::tags() const
41{
42 return QObject::tr( "difference,erase,not overlap" ).split( ',' );
43}
44
45QString QgsMultiDifferenceAlgorithm::group() const
46{
47 return QObject::tr( "Vector overlay" );
48}
49
50QString QgsMultiDifferenceAlgorithm::groupId() const
51{
52 return u"vectoroverlay"_s;
53}
54
55QString QgsMultiDifferenceAlgorithm::shortHelpString() const
56{
57 return QObject::tr( "This algorithm extracts features from the Input layer that fall completely outside or only partially overlap the features from any of the Overlay layer(s). "
58 "For each overlay layer the difference is calculated between the result of all previous difference operations and this overlay layer. "
59 "Input layer features that partially overlap feature(s) in the Overlay layers are split along those features' boundary "
60 "and only the portions outside the Overlay layer features are retained." )
61 + u"\n\n"_s
62 + QObject::tr( "Attributes are not modified, although properties such as area or length of the features will "
63 "be modified by the difference operation. If such properties are stored as attributes, those attributes will have to "
64 "be manually updated." );
65}
66
67QString QgsMultiDifferenceAlgorithm::shortDescription() const
68{
69 return QObject::tr( "Extracts features from a layer that fall completely outside or only partially overlap the features from other layer(s)." );
70}
71
72QgsProcessingAlgorithm *QgsMultiDifferenceAlgorithm::createInstance() const
73{
74 return new QgsMultiDifferenceAlgorithm();
75}
76
77void QgsMultiDifferenceAlgorithm::initAlgorithm( const QVariantMap & )
78{
79 addParameter( new QgsProcessingParameterFeatureSource( u"INPUT"_s, QObject::tr( "Input layer" ) ) );
80 addParameter( new QgsProcessingParameterMultipleLayers( u"OVERLAYS"_s, QObject::tr( "Overlay layers" ), Qgis::ProcessingSourceType::VectorAnyGeometry ) );
81 addParameter( new QgsProcessingParameterFeatureSink( u"OUTPUT"_s, QObject::tr( "Difference" ) ) );
82}
83
84
85QVariantMap QgsMultiDifferenceAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
86{
87 std::unique_ptr<QgsFeatureSource> sourceA( parameterAsSource( parameters, u"INPUT"_s, context ) );
88 if ( !sourceA )
89 throw QgsProcessingException( invalidSourceError( parameters, u"INPUT"_s ) );
90
91 const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, u"OVERLAYS"_s, context );
92
93 // loop through overlay layers and check whether they are vectors
94 long totalLayerCount = 0;
95 for ( QgsMapLayer *layer : layers )
96 {
97 if ( feedback->isCanceled() )
98 break;
99
100 if ( !layer )
101 throw QgsProcessingException( QObject::tr( "Error retrieving map layer." ) );
102
103 if ( layer->type() != Qgis::LayerType::Vector )
104 throw QgsProcessingException( QObject::tr( "All layers must be vector layers!" ) );
105
106 totalLayerCount++;
107 }
108
109 const Qgis::WkbType geometryType = QgsWkbTypes::multiType( sourceA->wkbType() );
110 const QgsCoordinateReferenceSystem crs = sourceA->sourceCrs();
111 std::unique_ptr<QgsFeatureSink> sink;
112 long count = 0;
113 QVariantMap outputs;
114
115 if ( totalLayerCount == 1 )
116 {
117 QString dest;
118 sink.reset( parameterAsSink( parameters, u"OUTPUT"_s, context, dest, sourceA->fields(), geometryType, crs ) );
119 if ( !sink )
120 throw QgsProcessingException( invalidSinkError( parameters, u"OUTPUT"_s ) );
121
122 outputs.insert( u"OUTPUT"_s, dest );
123
124 QgsVectorLayer *overlayLayer = qobject_cast<QgsVectorLayer *>( layers.at( 0 ) );
125
126 const long total = sourceA->featureCount();
127 QgsOverlayUtils::difference( *sourceA, *overlayLayer, *sink, context, feedback, count, total, QgsOverlayUtils::OutputA );
128 sink->finalize();
129 }
130 else
131 {
132 QgsProcessingMultiStepFeedback multiStepFeedback( totalLayerCount, feedback );
133 QgsVectorLayer *differenceLayer = nullptr;
134
135 long i = 0;
136 for ( QgsMapLayer *layer : layers )
137 {
138 if ( feedback->isCanceled() )
139 break;
140
141 multiStepFeedback.setCurrentStep( i );
142
143 if ( !layer )
144 continue;
145
146 QgsVectorLayer *overlayLayer = qobject_cast<QgsVectorLayer *>( layer );
147 if ( !overlayLayer )
148 continue;
149
150 count = 0;
151 if ( i == 0 )
152 {
153 QString id = u"memory:"_s;
154 sink.reset( QgsProcessingUtils::createFeatureSink( id, context, sourceA->fields(), geometryType, crs ) );
155 QgsOverlayUtils::difference( *sourceA, *overlayLayer, *sink, context, &multiStepFeedback, count, sourceA->featureCount(), QgsOverlayUtils::OutputA );
156
157 differenceLayer = qobject_cast<QgsVectorLayer *>( QgsProcessingUtils::mapLayerFromString( id, context ) );
158 }
159 else if ( i == totalLayerCount - 1 )
160 {
161 QString dest;
162 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u"OUTPUT"_s, context, dest, differenceLayer->fields(), geometryType, crs ) );
163 if ( !sink )
164 throw QgsProcessingException( invalidSinkError( parameters, u"OUTPUT"_s ) );
165
166 outputs.insert( u"OUTPUT"_s, dest );
167
168 QgsOverlayUtils::difference( *differenceLayer, *overlayLayer, *sink, context, &multiStepFeedback, count, differenceLayer->featureCount(), QgsOverlayUtils::OutputA );
169 }
170 else
171 {
172 QString id = u"memory:"_s;
173 sink.reset( QgsProcessingUtils::createFeatureSink( id, context, differenceLayer->fields(), geometryType, crs ) );
174 QgsOverlayUtils::difference( *differenceLayer, *overlayLayer, *sink, context, &multiStepFeedback, count, differenceLayer->featureCount(), QgsOverlayUtils::OutputA );
175
176 differenceLayer = qobject_cast<QgsVectorLayer *>( QgsProcessingUtils::mapLayerFromString( id, context ) );
177 }
178
179 i++;
180 }
181 }
182
183 return outputs;
184}
185
@ VectorAnyGeometry
Any vector layer with geometry.
Definition qgis.h:3604
@ Vector
Vector layer.
Definition qgis.h:194
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:280
Represents a coordinate reference system (CRS).
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:55
Base class for all map layer types.
Definition qgsmaplayer.h:83
Abstract base class for processing algorithms.
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
Processing feedback object for multi-step operations.
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.
static QgsFeatureSink * createFeatureSink(QString &destination, QgsProcessingContext &context, const QgsFields &fields, Qgis::WkbType geometryType, const QgsCoordinateReferenceSystem &crs, const QVariantMap &createOptions=QVariantMap(), const QStringList &datasourceOptions=QStringList(), const QStringList &layerOptions=QStringList(), QgsFeatureSink::SinkFlags sinkFlags=QgsFeatureSink::SinkFlags(), QgsRemappingSinkDefinition *remappingDefinition=nullptr)
Creates a feature sink ready for adding features.
static QgsMapLayer * mapLayerFromString(const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers=true, QgsProcessingUtils::LayerHint typeHint=QgsProcessingUtils::LayerHint::UnknownType, QgsProcessing::LayerOptionsFlags flags=QgsProcessing::LayerOptionsFlags())
Interprets a string as a map layer within the supplied context.
Represents a vector layer which manages a vector based dataset.
long long featureCount(const QString &legendKey) const
Number of features rendered with specified legend key.
static Qgis::WkbType multiType(Qgis::WkbType type)
Returns the multi type for a WKB type.