QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
Loading...
Searching...
No Matches
qgsalgorithmmultiunion.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmmultiunion.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 QgsMultiUnionAlgorithm::name() const
31{
32 return u"multiunion"_s;
33}
34
35QString QgsMultiUnionAlgorithm::displayName() const
36{
37 return QObject::tr( "Union (multiple)" );
38}
39
40QStringList QgsMultiUnionAlgorithm::tags() const
41{
42 return QObject::tr( "union,overlap,not overlap" ).split( ',' );
43}
44
45QString QgsMultiUnionAlgorithm::group() const
46{
47 return QObject::tr( "Vector overlay" );
48}
49
50QString QgsMultiUnionAlgorithm::groupId() const
51{
52 return u"vectoroverlay"_s;
53}
54
55QString QgsMultiUnionAlgorithm::shortHelpString() const
56{
57 return QObject::tr(
58 "This algorithm checks overlaps between features within the Input layer and creates separate features for overlapping "
59 "and non-overlapping parts. The area of overlap will create as many identical overlapping features as there are "
60 "features that participate in that overlap."
61 )
62 + u"\n\n"_s
63 + QObject::tr(
64 "Multiple Overlay layers can also be used, in which case features from each layer are split at their overlap with features from "
65 "all other layers, creating a layer containing all the portions from both Input and Overlay layers. "
66 "The attribute table of the Union layer is filled with attribute values from the respective original layer "
67 "for non-overlapping features, and attribute values from both layers for overlapping features."
68 );
69}
70
71QString QgsMultiUnionAlgorithm::shortDescription() const
72{
73 return QObject::tr(
74 "Checks overlaps between features on the same layer or on more layers "
75 "and creates separate features for overlapping and non-overlapping parts."
76 );
77}
78
79Qgis::ProcessingAlgorithmDocumentationFlags QgsMultiUnionAlgorithm::documentationFlags() const
80{
82}
83
84QgsProcessingAlgorithm *QgsMultiUnionAlgorithm::createInstance() const
85{
86 return new QgsMultiUnionAlgorithm();
87}
88
89void QgsMultiUnionAlgorithm::initAlgorithm( const QVariantMap & )
90{
91 addParameter( new QgsProcessingParameterFeatureSource( u"INPUT"_s, QObject::tr( "Input layer" ) ) );
92 addParameter( new QgsProcessingParameterMultipleLayers( u"OVERLAYS"_s, QObject::tr( "Overlay layers" ), Qgis::ProcessingSourceType::VectorAnyGeometry, QVariant(), true ) );
93
94 auto prefix = std::make_unique<QgsProcessingParameterString>( u"OVERLAY_FIELDS_PREFIX"_s, QObject::tr( "Overlay fields prefix" ), QString(), false, true );
95 prefix->setFlags( prefix->flags() | Qgis::ProcessingParameterFlag::Advanced );
96 addParameter( prefix.release() );
97
98 addParameter( new QgsProcessingParameterFeatureSink( u"OUTPUT"_s, QObject::tr( "Union" ) ) );
99}
100
101QVariantMap QgsMultiUnionAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
102{
103 std::unique_ptr<QgsFeatureSource> sourceA( parameterAsSource( parameters, u"INPUT"_s, context ) );
104 if ( !sourceA )
105 throw QgsProcessingException( invalidSourceError( parameters, u"INPUT"_s ) );
106
107 const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, u"OVERLAYS"_s, context );
108
109 // loop through overlay layers and check whether they are vectors
110 long totalLayerCount = 0;
111 for ( QgsMapLayer *layer : layers )
112 {
113 if ( feedback->isCanceled() )
114 break;
115
116 if ( !layer )
117 throw QgsProcessingException( QObject::tr( "Error retrieving map layer." ) );
118
119 if ( layer->type() != Qgis::LayerType::Vector )
120 throw QgsProcessingException( QObject::tr( "All layers must be vector layers!" ) );
121
122 totalLayerCount++;
123 }
124
125 const Qgis::WkbType geometryType = QgsWkbTypes::multiType( sourceA->wkbType() );
126 const QgsCoordinateReferenceSystem crs = sourceA->sourceCrs();
127 const QString overlayFieldsPrefix = parameterAsString( parameters, u"OVERLAY_FIELDS_PREFIX"_s, context );
128 std::unique_ptr<QgsFeatureSink> sink;
129 QVariantMap outputs;
130 bool ok;
131
132 if ( totalLayerCount == 0 )
133 {
134 // we are doing single layer union
135 QString dest;
136 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u"OUTPUT"_s, context, dest, sourceA->fields(), geometryType, sourceA->sourceCrs(), QgsFeatureSink::RegeneratePrimaryKey ) );
137 if ( !sink )
138 throw QgsProcessingException( invalidSinkError( parameters, u"OUTPUT"_s ) );
139
140 outputs.insert( u"OUTPUT"_s, dest );
141
142 QgsOverlayUtils::resolveOverlaps( *sourceA, *sink, feedback );
143 sink->finalize();
144 return outputs;
145 }
146 else
147 {
148 QgsProcessingMultiStepFeedback multiStepFeedback( totalLayerCount, feedback );
149 QgsVectorLayer *unionLayer = nullptr;
150 QgsFields fields;
151
152 long i = 0;
153 for ( QgsMapLayer *layer : layers )
154 {
155 if ( feedback->isCanceled() )
156 break;
157
158 multiStepFeedback.setCurrentStep( i );
159
160 if ( !layer )
161 continue;
162
163 QgsVectorLayer *overlayLayer = qobject_cast<QgsVectorLayer *>( layer );
164 if ( !overlayLayer )
165 continue;
166
167 if ( i == 0 )
168 {
169 QString id = u"memory:"_s;
170 fields = QgsProcessingUtils::combineFields( sourceA->fields(), overlayLayer->fields(), overlayFieldsPrefix );
171 sink.reset( QgsProcessingUtils::createFeatureSink( id, context, fields, geometryType, crs, QVariantMap(), QStringList(), QStringList(), QgsFeatureSink::RegeneratePrimaryKey ) );
172 ok = makeUnion( *sourceA, *overlayLayer, *sink, context, &multiStepFeedback );
173
174 if ( !ok )
175 throw QgsProcessingException( QObject::tr( "Interrupted by user." ) );
176
177 unionLayer = qobject_cast<QgsVectorLayer *>( QgsProcessingUtils::mapLayerFromString( id, context ) );
178 }
179 else if ( i == totalLayerCount - 1 )
180 {
181 fields = QgsProcessingUtils::combineFields( unionLayer->fields(), overlayLayer->fields(), overlayFieldsPrefix );
182
183
184 QString dest;
185 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u"OUTPUT"_s, context, dest, fields, geometryType, crs, QgsFeatureSink::RegeneratePrimaryKey ) );
186 if ( !sink )
187 throw QgsProcessingException( invalidSinkError( parameters, u"OUTPUT"_s ) );
188
189 outputs.insert( u"OUTPUT"_s, dest );
190 ok = makeUnion( *unionLayer, *overlayLayer, *sink, context, &multiStepFeedback );
191 if ( !ok )
192 throw QgsProcessingException( QObject::tr( "Interrupted by user." ) );
193 }
194 else
195 {
196 QString id = u"memory:"_s;
197 fields = QgsProcessingUtils::combineFields( unionLayer->fields(), overlayLayer->fields(), overlayFieldsPrefix );
198 sink.reset( QgsProcessingUtils::createFeatureSink( id, context, fields, geometryType, crs, QVariantMap(), QStringList(), QStringList(), QgsFeatureSink::RegeneratePrimaryKey ) );
199 ok = makeUnion( *unionLayer, *overlayLayer, *sink, context, &multiStepFeedback );
200 if ( !ok )
201 throw QgsProcessingException( QObject::tr( "Interrupted by user." ) );
202
203 unionLayer = qobject_cast<QgsVectorLayer *>( QgsProcessingUtils::mapLayerFromString( id, context ) );
204 }
205
206 i++;
207 }
208 }
209
210 return outputs;
211}
212
213bool QgsMultiUnionAlgorithm::makeUnion( const QgsFeatureSource &sourceA, const QgsFeatureSource &sourceB, QgsFeatureSink &sink, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
214{
215 const QList<int> fieldIndicesA = QgsProcessingUtils::fieldNamesToIndices( QStringList(), sourceA.fields() );
216 const QList<int> fieldIndicesB = QgsProcessingUtils::fieldNamesToIndices( QStringList(), sourceB.fields() );
217
218 long count = 0;
219 const long total = sourceA.featureCount() * 2 + sourceB.featureCount();
220
221 QgsOverlayUtils::intersection( sourceA, sourceB, sink, context, feedback, count, total, fieldIndicesA, fieldIndicesB );
222 if ( feedback->isCanceled() )
223 return false;
224
225 QgsOverlayUtils::difference( sourceA, sourceB, sink, context, feedback, count, total, QgsOverlayUtils::OutputAB );
226 if ( feedback->isCanceled() )
227 return false;
228
229 QgsOverlayUtils::difference( sourceB, sourceA, sink, context, feedback, count, total, QgsOverlayUtils::OutputBA );
230 return true;
231}
232
@ VectorAnyGeometry
Any vector layer with geometry.
Definition qgis.h:3647
@ RegeneratesPrimaryKey
Algorithm always drops any existing primary keys or FID values and regenerates them in outputs.
Definition qgis.h:3734
@ Vector
Vector layer.
Definition qgis.h:207
QFlags< ProcessingAlgorithmDocumentationFlag > ProcessingAlgorithmDocumentationFlags
Flags describing algorithm behavior for documentation purposes.
Definition qgis.h:3745
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:294
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
Definition qgis.h:3880
Represents a coordinate reference system (CRS).
An interface for objects which accept features via addFeature(s) methods.
@ RegeneratePrimaryKey
This flag indicates, that a primary key field cannot be guaranteed to be unique and the sink should i...
An interface for objects which provide features via a getFeatures method.
virtual QgsFields fields() const =0
Returns the fields associated with features in the source.
virtual long long featureCount() const =0
Returns the number of features contained in the source, or -1 if the feature count is unknown.
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:56
Container of fields for a vector layer.
Definition qgsfields.h:46
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 QList< int > fieldNamesToIndices(const QStringList &fieldNames, const QgsFields &fields)
Returns a list of field indices parsed from the given list of field names.
static QgsFields combineFields(const QgsFields &fieldsA, const QgsFields &fieldsB, const QString &fieldsBPrefix=QString())
Combines two field lists, avoiding duplicate field names (in a case-insensitive manner).
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.
static Qgis::WkbType multiType(Qgis::WkbType type)
Returns the multi type for a WKB type.