QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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(
55 "This algorithm checks overlaps between features within the Input layer and creates separate features for overlapping "
56 "and non-overlapping parts. The area of overlap will create as many identical overlapping features as there are "
57 "features that participate in that overlap."
58 )
59 + u"\n\n"_s
60 + QObject::tr(
61 "An Overlay layer can also be used, in which case features from each layer are split at their overlap with features from "
62 "the other one, creating a layer containing all the portions from both Input and Overlay layers. "
63 "The attribute table of the Union layer is filled with attribute values from the respective original layer "
64 "for non-overlapping features, and attribute values from both layers for overlapping features."
65 );
66}
67
68QString QgsUnionAlgorithm::shortDescription() const
69{
70 return QObject::tr(
71 "Checks overlaps between features on the same layer or on two different layers "
72 "and creates separate features for overlapping and non-overlapping parts."
73 );
74}
75
76Qgis::ProcessingAlgorithmDocumentationFlags QgsUnionAlgorithm::documentationFlags() const
77{
79}
80
81QgsProcessingAlgorithm *QgsUnionAlgorithm::createInstance() const
82{
83 return new QgsUnionAlgorithm();
84}
85
86void QgsUnionAlgorithm::initAlgorithm( const QVariantMap & )
87{
88 addParameter( new QgsProcessingParameterFeatureSource( u"INPUT"_s, QObject::tr( "Input layer" ) ) );
89 addParameter( new QgsProcessingParameterFeatureSource( u"OVERLAY"_s, QObject::tr( "Overlay layer" ), QList<int>(), QVariant(), true ) );
90
91 auto prefix = std::make_unique<QgsProcessingParameterString>( u"OVERLAY_FIELDS_PREFIX"_s, QObject::tr( "Overlay fields prefix" ), QString(), false, true );
92 prefix->setFlags( prefix->flags() | Qgis::ProcessingParameterFlag::Advanced );
93 addParameter( prefix.release() );
94
95 addParameter( new QgsProcessingParameterFeatureSink( u"OUTPUT"_s, QObject::tr( "Union" ) ) );
96
97 auto gridSize = std::make_unique<QgsProcessingParameterNumber>( u"GRID_SIZE"_s, QObject::tr( "Grid size" ), Qgis::ProcessingNumberParameterType::Double, QVariant(), true, 0 );
98 gridSize->setFlags( gridSize->flags() | Qgis::ProcessingParameterFlag::Advanced );
99 addParameter( gridSize.release() );
100}
101
102QVariantMap QgsUnionAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
103{
104 std::unique_ptr<QgsFeatureSource> sourceA( parameterAsSource( parameters, u"INPUT"_s, context ) );
105 if ( !sourceA )
106 throw QgsProcessingException( invalidSourceError( parameters, u"INPUT"_s ) );
107
108 std::unique_ptr<QgsFeatureSource> sourceB( parameterAsSource( parameters, u"OVERLAY"_s, context ) );
109 if ( parameters.value( u"OVERLAY"_s ).isValid() && !sourceB )
110 throw QgsProcessingException( invalidSourceError( parameters, u"OVERLAY"_s ) );
111
112 const Qgis::WkbType geomType = QgsWkbTypes::multiType( sourceA->wkbType() );
113
114 const QString overlayFieldsPrefix = parameterAsString( parameters, u"OVERLAY_FIELDS_PREFIX"_s, context );
115 const QgsFields fields = sourceB ? QgsProcessingUtils::combineFields( sourceA->fields(), sourceB->fields(), overlayFieldsPrefix ) : sourceA->fields();
116
117 QString dest;
118 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u"OUTPUT"_s, context, dest, fields, geomType, sourceA->sourceCrs(), QgsFeatureSink::RegeneratePrimaryKey ) );
119 if ( !sink )
120 throw QgsProcessingException( invalidSinkError( parameters, u"OUTPUT"_s ) );
121
122 QVariantMap outputs;
123 outputs.insert( u"OUTPUT"_s, dest );
124
125 if ( !sourceB )
126 {
127 // we are doing single layer union
128 QgsOverlayUtils::resolveOverlaps( *sourceA, *sink, feedback );
129 return outputs;
130 }
131
132 const QList<int> fieldIndicesA = QgsProcessingUtils::fieldNamesToIndices( QStringList(), sourceA->fields() );
133 const QList<int> fieldIndicesB = QgsProcessingUtils::fieldNamesToIndices( QStringList(), sourceB->fields() );
134
135 long count = 0;
136 const long total = sourceA->featureCount() * 2 + sourceB->featureCount();
137
138 QgsGeometryParameters geometryParameters;
139 if ( parameters.value( u"GRID_SIZE"_s ).isValid() )
140 {
141 geometryParameters.setGridSize( parameterAsDouble( parameters, u"GRID_SIZE"_s, context ) );
142 }
143
144 QgsOverlayUtils::intersection( *sourceA, *sourceB, *sink, context, feedback, count, total, fieldIndicesA, fieldIndicesB, geometryParameters );
145 if ( feedback->isCanceled() )
146 return outputs;
147
148 QgsOverlayUtils::difference( *sourceA, *sourceB, *sink, context, feedback, count, total, QgsOverlayUtils::OutputAB, geometryParameters );
149 if ( feedback->isCanceled() )
150 return outputs;
151
152 QgsOverlayUtils::difference( *sourceB, *sourceA, *sink, context, feedback, count, total, QgsOverlayUtils::OutputBA, geometryParameters );
153
154 sink->finalize();
155
156 return outputs;
157}
158
@ RegeneratesPrimaryKey
Algorithm always drops any existing primary keys or FID values and regenerates them in outputs.
Definition qgis.h:3734
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
@ Double
Double/float values.
Definition qgis.h:3921
@ 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:56
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.