QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
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
43QString QgsUnionAlgorithm::shortHelpString() const
44{
45 return QObject::tr( "This algorithm checks overlaps between features within the Input layer and creates separate features for overlapping "
46 "and non-overlapping parts. The area of overlap will create as many identical overlapping features as there are "
47 "features that participate in that overlap." )
48 + QStringLiteral( "\n\n" )
49 + QObject::tr( "An Overlay layer can also be used, in which case features from each layer are split at their overlap with features from "
50 "the other one, creating a layer containing all the portions from both Input and Overlay layers. "
51 "The attribute table of the Union layer is filled with attribute values from the respective original layer "
52 "for non-overlapping features, and attribute values from both layers for overlapping features." );
53}
54
55QgsProcessingAlgorithm *QgsUnionAlgorithm::createInstance() const
56{
57 return new QgsUnionAlgorithm();
58}
59
60void QgsUnionAlgorithm::initAlgorithm( const QVariantMap & )
61{
62 addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
63 addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "OVERLAY" ), QObject::tr( "Overlay layer" ), QList< int >(), QVariant(), true ) );
64
65 std::unique_ptr< QgsProcessingParameterString > prefix = std::make_unique< QgsProcessingParameterString >( QStringLiteral( "OVERLAY_FIELDS_PREFIX" ), QObject::tr( "Overlay fields prefix" ), QString(), false, true );
66 prefix->setFlags( prefix->flags() | QgsProcessingParameterDefinition::FlagAdvanced );
67 addParameter( prefix.release() );
68
69 addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Union" ) ) );
70
71 std::unique_ptr< QgsProcessingParameterNumber > gridSize = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "GRID_SIZE" ),
72 QObject::tr( "Grid size" ), QgsProcessingParameterNumber::Double, QVariant(), true, 0 );
73 gridSize->setFlags( gridSize->flags() | QgsProcessingParameterDefinition::FlagAdvanced );
74 addParameter( gridSize.release() );
75}
76
77QVariantMap QgsUnionAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
78{
79 std::unique_ptr< QgsFeatureSource > sourceA( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
80 if ( !sourceA )
81 throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
82
83 std::unique_ptr< QgsFeatureSource > sourceB( parameterAsSource( parameters, QStringLiteral( "OVERLAY" ), context ) );
84 if ( parameters.value( QStringLiteral( "OVERLAY" ) ).isValid() && !sourceB )
85 throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "OVERLAY" ) ) );
86
87 const QgsWkbTypes::Type geomType = QgsWkbTypes::multiType( sourceA->wkbType() );
88
89 const QString overlayFieldsPrefix = parameterAsString( parameters, QStringLiteral( "OVERLAY_FIELDS_PREFIX" ), context );
90 const QgsFields fields = sourceB ? QgsProcessingUtils::combineFields( sourceA->fields(), sourceB->fields(), overlayFieldsPrefix ) : sourceA->fields();
91
92 QString dest;
93 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, fields, geomType, sourceA->sourceCrs(), QgsFeatureSink::RegeneratePrimaryKey ) );
94 if ( !sink )
95 throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
96
97 QVariantMap outputs;
98 outputs.insert( QStringLiteral( "OUTPUT" ), dest );
99
100 if ( !sourceB )
101 {
102 // we are doing single layer union
103 QgsOverlayUtils::resolveOverlaps( *sourceA, *sink, feedback );
104 return outputs;
105 }
106
107 const QList<int> fieldIndicesA = QgsProcessingUtils::fieldNamesToIndices( QStringList(), sourceA->fields() );
108 const QList<int> fieldIndicesB = QgsProcessingUtils::fieldNamesToIndices( QStringList(), sourceB->fields() );
109
110 long count = 0;
111 const long total = sourceA->featureCount() * 2 + sourceB->featureCount();
112
113 QgsGeometryParameters geometryParameters;
114 if ( parameters.value( QStringLiteral( "GRID_SIZE" ) ).isValid() )
115 {
116 geometryParameters.setGridSize( parameterAsDouble( parameters, QStringLiteral( "GRID_SIZE" ), context ) );
117 }
118
119 QgsOverlayUtils::intersection( *sourceA, *sourceB, *sink, context, feedback, count, total, fieldIndicesA, fieldIndicesB, geometryParameters );
120 if ( feedback->isCanceled() )
121 return outputs;
122
123 QgsOverlayUtils::difference( *sourceA, *sourceB, *sink, context, feedback, count, total, QgsOverlayUtils::OutputAB, geometryParameters );
124 if ( feedback->isCanceled() )
125 return outputs;
126
127 QgsOverlayUtils::difference( *sourceB, *sourceA, *sink, context, feedback, count, total, QgsOverlayUtils::OutputBA, geometryParameters );
128
129 return outputs;
130}
131
@ RegeneratePrimaryKey
This flag indicates, that a primary key field cannot be guaranteed to be unique and the sink should i...
bool isCanceled() const SIP_HOLDGIL
Tells whether the operation has been canceled already.
Definition: qgsfeedback.h:54
Container of fields for a vector layer.
Definition: qgsfields.h:45
Encapsulates parameters under which a geometry operation is performed.
Definition: qgsgeometry.h:111
void setGridSize(double size)
Sets the grid size which will be used to snap vertices of a geometry.
Definition: qgsgeometry.h:136
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.
Definition: qgsexception.h:83
Base class for providing feedback from a processing algorithm.
@ FlagAdvanced
Parameter is an advanced parameter which should be hidden from users by default.
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).
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:70
static Type multiType(Type type) SIP_HOLDGIL
Returns the multi type for a WKB type.
Definition: qgswkbtypes.h:304