QGIS API Documentation 3.99.0-Master (357b655ed83)
Loading...
Searching...
No Matches
qgsalgorithmsymmetricaldifference.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmsymmetricaldifference.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
17
18#include "qgsoverlayutils.h"
19
20#include <QString>
21
22using namespace Qt::StringLiterals;
23
25
26QString QgsSymmetricalDifferenceAlgorithm::name() const
27{
28 return u"symmetricaldifference"_s;
29}
30
31QString QgsSymmetricalDifferenceAlgorithm::displayName() const
32{
33 return QObject::tr( "Symmetrical difference" );
34}
35
36QStringList QgsSymmetricalDifferenceAlgorithm::tags() const
37{
38 return QObject::tr( "difference,symdiff,not overlap" ).split( ',' );
39}
40
41QString QgsSymmetricalDifferenceAlgorithm::group() const
42{
43 return QObject::tr( "Vector overlay" );
44}
45
46QString QgsSymmetricalDifferenceAlgorithm::groupId() const
47{
48 return u"vectoroverlay"_s;
49}
50
51QString QgsSymmetricalDifferenceAlgorithm::shortHelpString() const
52{
53 return QObject::tr( "This algorithm extracts the portions of features from both the Input and Overlay layers that do not overlap. "
54 "Overlapping areas between the two layers are removed. The attribute table of the Symmetrical Difference layer "
55 "contains original attributes from both the Input and Overlay layers." );
56}
57
58QString QgsSymmetricalDifferenceAlgorithm::shortDescription() const
59{
60 return QObject::tr( "Extracts the portions of features from two layers that do not overlap." );
61}
62
63Qgis::ProcessingAlgorithmDocumentationFlags QgsSymmetricalDifferenceAlgorithm::documentationFlags() const
64{
66}
67
68QgsProcessingAlgorithm *QgsSymmetricalDifferenceAlgorithm::createInstance() const
69{
70 return new QgsSymmetricalDifferenceAlgorithm();
71}
72
73void QgsSymmetricalDifferenceAlgorithm::initAlgorithm( const QVariantMap & )
74{
75 addParameter( new QgsProcessingParameterFeatureSource( u"INPUT"_s, QObject::tr( "Input layer" ) ) );
76 addParameter( new QgsProcessingParameterFeatureSource( u"OVERLAY"_s, QObject::tr( "Overlay layer" ) ) );
77
78 auto prefix = std::make_unique<QgsProcessingParameterString>( u"OVERLAY_FIELDS_PREFIX"_s, QObject::tr( "Overlay fields prefix" ), QString(), false, true );
79 prefix->setFlags( prefix->flags() | Qgis::ProcessingParameterFlag::Advanced );
80 addParameter( prefix.release() );
81
82 addParameter( new QgsProcessingParameterFeatureSink( u"OUTPUT"_s, QObject::tr( "Symmetrical difference" ) ) );
83
84 auto gridSize = std::make_unique<QgsProcessingParameterNumber>( u"GRID_SIZE"_s, QObject::tr( "Grid size" ), Qgis::ProcessingNumberParameterType::Double, QVariant(), true, 0 );
85 gridSize->setFlags( gridSize->flags() | Qgis::ProcessingParameterFlag::Advanced );
86 addParameter( gridSize.release() );
87}
88
89
90QVariantMap QgsSymmetricalDifferenceAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
91{
92 std::unique_ptr<QgsFeatureSource> sourceA( parameterAsSource( parameters, u"INPUT"_s, context ) );
93 if ( !sourceA )
94 throw QgsProcessingException( invalidSourceError( parameters, u"INPUT"_s ) );
95
96 std::unique_ptr<QgsFeatureSource> sourceB( parameterAsSource( parameters, u"OVERLAY"_s, context ) );
97 if ( !sourceB )
98 throw QgsProcessingException( invalidSourceError( parameters, u"OVERLAY"_s ) );
99
100 const Qgis::WkbType geomTypeA = QgsWkbTypes::promoteNonPointTypesToMulti( sourceA->wkbType() );
101 const Qgis::WkbType geomTypeB = QgsWkbTypes::promoteNonPointTypesToMulti( sourceB->wkbType() );
102
103 if ( geomTypeA != geomTypeB )
104 feedback->pushWarning( QObject::tr( "Performing symmetrical difference between layers with different geometry types (INPUT has %1 and OVERLAY has %2) can lead to unexpected results" ).arg( QgsWkbTypes::displayString( sourceA->wkbType() ), QgsWkbTypes::displayString( sourceB->wkbType() ) ) );
105
106 const QString overlayFieldsPrefix = parameterAsString( parameters, u"OVERLAY_FIELDS_PREFIX"_s, context );
107 const QgsFields fields = QgsProcessingUtils::combineFields( sourceA->fields(), sourceB->fields(), overlayFieldsPrefix );
108
109 QString dest;
110 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u"OUTPUT"_s, context, dest, fields, geomTypeA, sourceA->sourceCrs(), QgsFeatureSink::RegeneratePrimaryKey ) );
111 if ( !sink )
112 throw QgsProcessingException( invalidSinkError( parameters, u"OUTPUT"_s ) );
113
114 QVariantMap outputs;
115 outputs.insert( u"OUTPUT"_s, dest );
116
117 long count = 0;
118 const long total = sourceA->featureCount() + sourceB->featureCount();
119
120 QgsGeometryParameters geometryParameters;
121 if ( parameters.value( u"GRID_SIZE"_s ).isValid() )
122 {
123 geometryParameters.setGridSize( parameterAsDouble( parameters, u"GRID_SIZE"_s, context ) );
124 }
125
126 QgsOverlayUtils::difference( *sourceA, *sourceB, *sink, context, feedback, count, total, QgsOverlayUtils::OutputAB, geometryParameters, QgsOverlayUtils::SanitizeFlag::DontPromotePointGeometryToMultiPoint );
127 if ( feedback->isCanceled() )
128 return outputs;
129
130 QgsOverlayUtils::difference( *sourceB, *sourceA, *sink, context, feedback, count, total, QgsOverlayUtils::OutputBA, geometryParameters, QgsOverlayUtils::SanitizeFlag::DontPromotePointGeometryToMultiPoint );
131
132 sink->finalize();
133
134 return outputs;
135}
136
@ 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.
virtual void pushWarning(const QString &warning)
Pushes a warning informational message from the algorithm.
A feature sink output for processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.
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 Q_INVOKABLE QString displayString(Qgis::WkbType type)
Returns a non-translated display string type for a WKB type, e.g., the geometry name used in WKT geom...
static Qgis::WkbType promoteNonPointTypesToMulti(Qgis::WkbType type)
Promotes a WKB geometry type to its multi-type equivalent, with the exception of point geometry types...