QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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(
58 "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). "
59 "For each overlay layer the difference is calculated between the result of all previous difference operations and this overlay layer. "
60 "Input layer features that partially overlap feature(s) in the Overlay layers are split along those features' boundary "
61 "and only the portions outside the Overlay layer features are retained."
62 )
63 + u"\n\n"_s
64 + QObject::tr(
65 "Attributes are not modified, although properties such as area or length of the features will "
66 "be modified by the difference operation. If such properties are stored as attributes, those attributes will have to "
67 "be manually updated."
68 );
69}
70
71QString QgsMultiDifferenceAlgorithm::shortDescription() const
72{
73 return QObject::tr( "Extracts features from a layer that fall completely outside or only partially overlap the features from other layer(s)." );
74}
75
76QgsProcessingAlgorithm *QgsMultiDifferenceAlgorithm::createInstance() const
77{
78 return new QgsMultiDifferenceAlgorithm();
79}
80
81void QgsMultiDifferenceAlgorithm::initAlgorithm( const QVariantMap & )
82{
83 addParameter( new QgsProcessingParameterFeatureSource( u"INPUT"_s, QObject::tr( "Input layer" ) ) );
84 addParameter( new QgsProcessingParameterMultipleLayers( u"OVERLAYS"_s, QObject::tr( "Overlay layers" ), Qgis::ProcessingSourceType::VectorAnyGeometry ) );
85 addParameter( new QgsProcessingParameterFeatureSink( u"OUTPUT"_s, QObject::tr( "Difference" ) ) );
86}
87
88
89QVariantMap QgsMultiDifferenceAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
90{
91 std::unique_ptr<QgsFeatureSource> sourceA( parameterAsSource( parameters, u"INPUT"_s, context ) );
92 if ( !sourceA )
93 throw QgsProcessingException( invalidSourceError( parameters, u"INPUT"_s ) );
94
95 const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, u"OVERLAYS"_s, context );
96
97 // loop through overlay layers and check whether they are vectors
98 long totalLayerCount = 0;
99 for ( QgsMapLayer *layer : layers )
100 {
101 if ( feedback->isCanceled() )
102 break;
103
104 if ( !layer )
105 throw QgsProcessingException( QObject::tr( "Error retrieving map layer." ) );
106
107 if ( layer->type() != Qgis::LayerType::Vector )
108 throw QgsProcessingException( QObject::tr( "All layers must be vector layers!" ) );
109
110 totalLayerCount++;
111 }
112
113 const Qgis::WkbType geometryType = QgsWkbTypes::multiType( sourceA->wkbType() );
114 const QgsCoordinateReferenceSystem crs = sourceA->sourceCrs();
115 std::unique_ptr<QgsFeatureSink> sink;
116 long count = 0;
117 QVariantMap outputs;
118
119 if ( totalLayerCount == 1 )
120 {
121 QString dest;
122 sink.reset( parameterAsSink( parameters, u"OUTPUT"_s, context, dest, sourceA->fields(), geometryType, crs ) );
123 if ( !sink )
124 throw QgsProcessingException( invalidSinkError( parameters, u"OUTPUT"_s ) );
125
126 outputs.insert( u"OUTPUT"_s, dest );
127
128 QgsVectorLayer *overlayLayer = qobject_cast<QgsVectorLayer *>( layers.at( 0 ) );
129
130 const long total = sourceA->featureCount();
131 QgsOverlayUtils::difference( *sourceA, *overlayLayer, *sink, context, feedback, count, total, QgsOverlayUtils::OutputA );
132 sink->finalize();
133 }
134 else
135 {
136 QgsProcessingMultiStepFeedback multiStepFeedback( totalLayerCount, feedback );
137 QgsVectorLayer *differenceLayer = nullptr;
138
139 long i = 0;
140 for ( QgsMapLayer *layer : layers )
141 {
142 if ( feedback->isCanceled() )
143 break;
144
145 multiStepFeedback.setCurrentStep( i );
146
147 if ( !layer )
148 continue;
149
150 QgsVectorLayer *overlayLayer = qobject_cast<QgsVectorLayer *>( layer );
151 if ( !overlayLayer )
152 continue;
153
154 count = 0;
155 if ( i == 0 )
156 {
157 QString id = u"memory:"_s;
158 sink.reset( QgsProcessingUtils::createFeatureSink( id, context, sourceA->fields(), geometryType, crs ) );
159 QgsOverlayUtils::difference( *sourceA, *overlayLayer, *sink, context, &multiStepFeedback, count, sourceA->featureCount(), QgsOverlayUtils::OutputA );
160
161 differenceLayer = qobject_cast<QgsVectorLayer *>( QgsProcessingUtils::mapLayerFromString( id, context ) );
162 }
163 else if ( i == totalLayerCount - 1 )
164 {
165 QString dest;
166 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u"OUTPUT"_s, context, dest, differenceLayer->fields(), geometryType, crs ) );
167 if ( !sink )
168 throw QgsProcessingException( invalidSinkError( parameters, u"OUTPUT"_s ) );
169
170 outputs.insert( u"OUTPUT"_s, dest );
171
172 QgsOverlayUtils::difference( *differenceLayer, *overlayLayer, *sink, context, &multiStepFeedback, count, differenceLayer->featureCount(), QgsOverlayUtils::OutputA );
173 }
174 else
175 {
176 QString id = u"memory:"_s;
177 sink.reset( QgsProcessingUtils::createFeatureSink( id, context, differenceLayer->fields(), geometryType, crs ) );
178 QgsOverlayUtils::difference( *differenceLayer, *overlayLayer, *sink, context, &multiStepFeedback, count, differenceLayer->featureCount(), QgsOverlayUtils::OutputA );
179
180 differenceLayer = qobject_cast<QgsVectorLayer *>( QgsProcessingUtils::mapLayerFromString( id, context ) );
181 }
182
183 i++;
184 }
185 }
186
187 return outputs;
188}
189
@ VectorAnyGeometry
Any vector layer with geometry.
Definition qgis.h:3647
@ Vector
Vector layer.
Definition qgis.h:207
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:294
Represents a coordinate reference system (CRS).
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:56
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.