QGIS API Documentation 3.43.0-Master (e01d6d7c4c0)
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
21
22
23QString QgsUnionAlgorithm::name() const
24{
25 return QStringLiteral( "union" );
26}
27
28QString QgsUnionAlgorithm::displayName() const
29{
30 return QObject::tr( "Union" );
31}
32
33QString QgsUnionAlgorithm::group() const
34{
35 return QObject::tr( "Vector overlay" );
36}
37
38QString QgsUnionAlgorithm::groupId() const
39{
40 return QStringLiteral( "vectoroverlay" );
41}
42
43QStringList QgsUnionAlgorithm::tags() const
44{
45 return QObject::tr( "overlap,clip,union,not overlap" ).split( ',' );
46}
47
48QString QgsUnionAlgorithm::shortHelpString() const
49{
50 return QObject::tr( "This algorithm checks overlaps between features within the Input layer and creates separate features for overlapping "
51 "and non-overlapping parts. The area of overlap will create as many identical overlapping features as there are "
52 "features that participate in that overlap." )
53 + QStringLiteral( "\n\n" )
54 + QObject::tr( "An Overlay layer can also be used, in which case features from each layer are split at their overlap with features from "
55 "the other one, creating a layer containing all the portions from both Input and Overlay layers. "
56 "The attribute table of the Union layer is filled with attribute values from the respective original layer "
57 "for non-overlapping features, and attribute values from both layers for overlapping features." );
58}
59
60QString QgsUnionAlgorithm::shortDescription() const
61{
62 return QObject::tr( "Checks overlaps between features on the same layer or on two different layers "
63 "and creates separate features for overlapping and non-overlapping parts." );
64}
65
66Qgis::ProcessingAlgorithmDocumentationFlags QgsUnionAlgorithm::documentationFlags() const
67{
69}
70
71QgsProcessingAlgorithm *QgsUnionAlgorithm::createInstance() const
72{
73 return new QgsUnionAlgorithm();
74}
75
76void QgsUnionAlgorithm::initAlgorithm( const QVariantMap & )
77{
78 addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
79 addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "OVERLAY" ), QObject::tr( "Overlay layer" ), QList<int>(), QVariant(), true ) );
80
81 auto prefix = std::make_unique<QgsProcessingParameterString>( QStringLiteral( "OVERLAY_FIELDS_PREFIX" ), QObject::tr( "Overlay fields prefix" ), QString(), false, true );
82 prefix->setFlags( prefix->flags() | Qgis::ProcessingParameterFlag::Advanced );
83 addParameter( prefix.release() );
84
85 addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Union" ) ) );
86
87 auto gridSize = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "GRID_SIZE" ), QObject::tr( "Grid size" ), Qgis::ProcessingNumberParameterType::Double, QVariant(), true, 0 );
88 gridSize->setFlags( gridSize->flags() | Qgis::ProcessingParameterFlag::Advanced );
89 addParameter( gridSize.release() );
90}
91
92QVariantMap QgsUnionAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
93{
94 std::unique_ptr<QgsFeatureSource> sourceA( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
95 if ( !sourceA )
96 throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
97
98 std::unique_ptr<QgsFeatureSource> sourceB( parameterAsSource( parameters, QStringLiteral( "OVERLAY" ), context ) );
99 if ( parameters.value( QStringLiteral( "OVERLAY" ) ).isValid() && !sourceB )
100 throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "OVERLAY" ) ) );
101
102 const Qgis::WkbType geomType = QgsWkbTypes::multiType( sourceA->wkbType() );
103
104 const QString overlayFieldsPrefix = parameterAsString( parameters, QStringLiteral( "OVERLAY_FIELDS_PREFIX" ), context );
105 const QgsFields fields = sourceB ? QgsProcessingUtils::combineFields( sourceA->fields(), sourceB->fields(), overlayFieldsPrefix ) : sourceA->fields();
106
107 QString dest;
108 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, fields, geomType, sourceA->sourceCrs(), QgsFeatureSink::RegeneratePrimaryKey ) );
109 if ( !sink )
110 throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
111
112 QVariantMap outputs;
113 outputs.insert( QStringLiteral( "OUTPUT" ), dest );
114
115 if ( !sourceB )
116 {
117 // we are doing single layer union
118 QgsOverlayUtils::resolveOverlaps( *sourceA, *sink, feedback );
119 return outputs;
120 }
121
122 const QList<int> fieldIndicesA = QgsProcessingUtils::fieldNamesToIndices( QStringList(), sourceA->fields() );
123 const QList<int> fieldIndicesB = QgsProcessingUtils::fieldNamesToIndices( QStringList(), sourceB->fields() );
124
125 long count = 0;
126 const long total = sourceA->featureCount() * 2 + sourceB->featureCount();
127
128 QgsGeometryParameters geometryParameters;
129 if ( parameters.value( QStringLiteral( "GRID_SIZE" ) ).isValid() )
130 {
131 geometryParameters.setGridSize( parameterAsDouble( parameters, QStringLiteral( "GRID_SIZE" ), context ) );
132 }
133
134 QgsOverlayUtils::intersection( *sourceA, *sourceB, *sink, context, feedback, count, total, fieldIndicesA, fieldIndicesB, geometryParameters );
135 if ( feedback->isCanceled() )
136 return outputs;
137
138 QgsOverlayUtils::difference( *sourceA, *sourceB, *sink, context, feedback, count, total, QgsOverlayUtils::OutputAB, geometryParameters );
139 if ( feedback->isCanceled() )
140 return outputs;
141
142 QgsOverlayUtils::difference( *sourceB, *sourceA, *sink, context, feedback, count, total, QgsOverlayUtils::OutputBA, geometryParameters );
143
144 sink->finalize();
145
146 return outputs;
147}
148
@ RegeneratesPrimaryKey
Algorithm always drops any existing primary keys or FID values and regenerates them in outputs.
QFlags< ProcessingAlgorithmDocumentationFlag > ProcessingAlgorithmDocumentationFlags
Flags describing algorithm behavior for documentation purposes.
Definition qgis.h:3496
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:256
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
@ 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:53
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.