QGIS API Documentation 3.99.0-Master (357b655ed83)
Loading...
Searching...
No Matches
qgsalgorithmunion.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmunion.cpp
3 ---------------------
4 Date : April 2018
5 Copyright : (C) 2018 by Martin Dobias
6 Email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgsalgorithmunion.h"
17
18#include "qgsoverlayutils.h"
19
20#include <QString>
21
22using namespace Qt::StringLiterals;
23
25
26
27QString QgsUnionAlgorithm::name() const
28{
29 return u"union"_s;
30}
31
32QString QgsUnionAlgorithm::displayName() const
33{
34 return QObject::tr( "Union" );
35}
36
37QString QgsUnionAlgorithm::group() const
38{
39 return QObject::tr( "Vector overlay" );
40}
41
42QString QgsUnionAlgorithm::groupId() const
43{
44 return u"vectoroverlay"_s;
45}
46
47QStringList QgsUnionAlgorithm::tags() const
48{
49 return QObject::tr( "overlap,clip,union,not overlap" ).split( ',' );
50}
51
52QString QgsUnionAlgorithm::shortHelpString() const
53{
54 return QObject::tr( "This algorithm checks overlaps between features within the Input layer and creates separate features for overlapping "
55 "and non-overlapping parts. The area of overlap will create as many identical overlapping features as there are "
56 "features that participate in that overlap." )
57 + u"\n\n"_s
58 + QObject::tr( "An Overlay layer can also be used, in which case features from each layer are split at their overlap with features from "
59 "the other one, creating a layer containing all the portions from both Input and Overlay layers. "
60 "The attribute table of the Union layer is filled with attribute values from the respective original layer "
61 "for non-overlapping features, and attribute values from both layers for overlapping features." );
62}
63
64QString QgsUnionAlgorithm::shortDescription() const
65{
66 return QObject::tr( "Checks overlaps between features on the same layer or on two different layers "
67 "and creates separate features for overlapping and non-overlapping parts." );
68}
69
70Qgis::ProcessingAlgorithmDocumentationFlags QgsUnionAlgorithm::documentationFlags() const
71{
73}
74
75QgsProcessingAlgorithm *QgsUnionAlgorithm::createInstance() const
76{
77 return new QgsUnionAlgorithm();
78}
79
80void QgsUnionAlgorithm::initAlgorithm( const QVariantMap & )
81{
82 addParameter( new QgsProcessingParameterFeatureSource( u"INPUT"_s, QObject::tr( "Input layer" ) ) );
83 addParameter( new QgsProcessingParameterFeatureSource( u"OVERLAY"_s, QObject::tr( "Overlay layer" ), QList<int>(), QVariant(), true ) );
84
85 auto prefix = std::make_unique<QgsProcessingParameterString>( u"OVERLAY_FIELDS_PREFIX"_s, QObject::tr( "Overlay fields prefix" ), QString(), false, true );
86 prefix->setFlags( prefix->flags() | Qgis::ProcessingParameterFlag::Advanced );
87 addParameter( prefix.release() );
88
89 addParameter( new QgsProcessingParameterFeatureSink( u"OUTPUT"_s, QObject::tr( "Union" ) ) );
90
91 auto gridSize = std::make_unique<QgsProcessingParameterNumber>( u"GRID_SIZE"_s, QObject::tr( "Grid size" ), Qgis::ProcessingNumberParameterType::Double, QVariant(), true, 0 );
92 gridSize->setFlags( gridSize->flags() | Qgis::ProcessingParameterFlag::Advanced );
93 addParameter( gridSize.release() );
94}
95
96QVariantMap QgsUnionAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
97{
98 std::unique_ptr<QgsFeatureSource> sourceA( parameterAsSource( parameters, u"INPUT"_s, context ) );
99 if ( !sourceA )
100 throw QgsProcessingException( invalidSourceError( parameters, u"INPUT"_s ) );
101
102 std::unique_ptr<QgsFeatureSource> sourceB( parameterAsSource( parameters, u"OVERLAY"_s, context ) );
103 if ( parameters.value( u"OVERLAY"_s ).isValid() && !sourceB )
104 throw QgsProcessingException( invalidSourceError( parameters, u"OVERLAY"_s ) );
105
106 const Qgis::WkbType geomType = QgsWkbTypes::multiType( sourceA->wkbType() );
107
108 const QString overlayFieldsPrefix = parameterAsString( parameters, u"OVERLAY_FIELDS_PREFIX"_s, context );
109 const QgsFields fields = sourceB ? QgsProcessingUtils::combineFields( sourceA->fields(), sourceB->fields(), overlayFieldsPrefix ) : sourceA->fields();
110
111 QString dest;
112 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u"OUTPUT"_s, context, dest, fields, geomType, sourceA->sourceCrs(), QgsFeatureSink::RegeneratePrimaryKey ) );
113 if ( !sink )
114 throw QgsProcessingException( invalidSinkError( parameters, u"OUTPUT"_s ) );
115
116 QVariantMap outputs;
117 outputs.insert( u"OUTPUT"_s, dest );
118
119 if ( !sourceB )
120 {
121 // we are doing single layer union
122 QgsOverlayUtils::resolveOverlaps( *sourceA, *sink, feedback );
123 return outputs;
124 }
125
126 const QList<int> fieldIndicesA = QgsProcessingUtils::fieldNamesToIndices( QStringList(), sourceA->fields() );
127 const QList<int> fieldIndicesB = QgsProcessingUtils::fieldNamesToIndices( QStringList(), sourceB->fields() );
128
129 long count = 0;
130 const long total = sourceA->featureCount() * 2 + sourceB->featureCount();
131
132 QgsGeometryParameters geometryParameters;
133 if ( parameters.value( u"GRID_SIZE"_s ).isValid() )
134 {
135 geometryParameters.setGridSize( parameterAsDouble( parameters, u"GRID_SIZE"_s, context ) );
136 }
137
138 QgsOverlayUtils::intersection( *sourceA, *sourceB, *sink, context, feedback, count, total, fieldIndicesA, fieldIndicesB, geometryParameters );
139 if ( feedback->isCanceled() )
140 return outputs;
141
142 QgsOverlayUtils::difference( *sourceA, *sourceB, *sink, context, feedback, count, total, QgsOverlayUtils::OutputAB, geometryParameters );
143 if ( feedback->isCanceled() )
144 return outputs;
145
146 QgsOverlayUtils::difference( *sourceB, *sourceA, *sink, context, feedback, count, total, QgsOverlayUtils::OutputBA, geometryParameters );
147
148 sink->finalize();
149
150 return outputs;
151}
152
@ RegeneratesPrimaryKey
Algorithm always drops any existing primary keys or FID values and regenerates them in outputs.
Definition qgis.h:3690
QFlags< ProcessingAlgorithmDocumentationFlag > ProcessingAlgorithmDocumentationFlags
Flags describing algorithm behavior for documentation purposes.
Definition qgis.h:3701
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:280
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
Definition qgis.h:3834
@ Double
Double/float values.
Definition qgis.h:3875
@ RegeneratePrimaryKey
This flag indicates, that a primary key field cannot be guaranteed to be unique and the sink should i...
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:55
Container of fields for a vector layer.
Definition qgsfields.h:46
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.
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.
A feature sink output for processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.
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 Qgis::WkbType multiType(Qgis::WkbType type)
Returns the multi type for a WKB type.