QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
21 
22 QString QgsSymmetricalDifferenceAlgorithm::name() const
23 {
24  return QStringLiteral( "symmetricaldifference" );
25 }
26 
27 QString QgsSymmetricalDifferenceAlgorithm::displayName() const
28 {
29  return QObject::tr( "Symmetrical difference" );
30 }
31 
32 QString QgsSymmetricalDifferenceAlgorithm::group() const
33 {
34  return QObject::tr( "Vector overlay" );
35 }
36 
37 QString QgsSymmetricalDifferenceAlgorithm::groupId() const
38 {
39  return QStringLiteral( "vectoroverlay" );
40 }
41 
42 QString QgsSymmetricalDifferenceAlgorithm::shortHelpString() const
43 {
44  return QObject::tr( "This algorithm extracts the portions of features from both the Input and Overlay layers that do not overlap. "
45  "Overlapping areas between the two layers are removed. The attribute table of the Symmetrical Difference layer "
46  "contains original attributes from both the Input and Difference layers." );
47 }
48 
49 QgsProcessingAlgorithm *QgsSymmetricalDifferenceAlgorithm::createInstance() const
50 {
51  return new QgsSymmetricalDifferenceAlgorithm();
52 }
53 
54 void QgsSymmetricalDifferenceAlgorithm::initAlgorithm( const QVariantMap & )
55 {
56  addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
57  addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "OVERLAY" ), QObject::tr( "Overlay layer" ) ) );
58 
59  std::unique_ptr< QgsProcessingParameterString > prefix = qgis::make_unique< QgsProcessingParameterString >( QStringLiteral( "OVERLAY_FIELDS_PREFIX" ), QObject::tr( "Overlay fields prefix" ), QString(), false, true );
60  prefix->setFlags( prefix->flags() | QgsProcessingParameterDefinition::FlagAdvanced );
61  addParameter( prefix.release() );
62 
63  addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Symmetrical difference" ) ) );
64 }
65 
66 
67 QVariantMap QgsSymmetricalDifferenceAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
68 {
69  std::unique_ptr< QgsFeatureSource > sourceA( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
70  if ( !sourceA )
71  throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
72 
73  std::unique_ptr< QgsFeatureSource > sourceB( parameterAsSource( parameters, QStringLiteral( "OVERLAY" ), context ) );
74  if ( !sourceB )
75  throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "OVERLAY" ) ) );
76 
77  QgsWkbTypes::Type geomType = QgsWkbTypes::multiType( sourceA->wkbType() );
78 
79  QString overlayFieldsPrefix = parameterAsString( parameters, QStringLiteral( "OVERLAY_FIELDS_PREFIX" ), context );
80  QgsFields fields = QgsProcessingUtils::combineFields( sourceA->fields(), sourceB->fields(), overlayFieldsPrefix );
81 
82  QString dest;
83  std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, fields, geomType, sourceA->sourceCrs(), QgsFeatureSink::RegeneratePrimaryKey ) );
84  if ( !sink )
85  throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
86 
87  QVariantMap outputs;
88  outputs.insert( QStringLiteral( "OUTPUT" ), dest );
89 
90  int count = 0;
91  int total = sourceA->featureCount() + sourceB->featureCount();
92 
93  QgsOverlayUtils::difference( *sourceA, *sourceB, *sink, context, feedback, count, total, QgsOverlayUtils::OutputAB );
94 
95  QgsOverlayUtils::difference( *sourceB, *sourceA, *sink, context, feedback, count, total, QgsOverlayUtils::OutputBA );
96 
97  return outputs;
98 }
99 
QgsWkbTypes::multiType
static Type multiType(Type type)
Returns the multi type for a WKB type.
Definition: qgswkbtypes.h:301
qgsalgorithmsymmetricaldifference.h
QgsProcessingFeedback
Definition: qgsprocessingfeedback.h:37
QgsFields
Definition: qgsfields.h:44
QgsProcessingParameterDefinition::FlagAdvanced
@ FlagAdvanced
Parameter is an advanced parameter which should be hidden from users by default.
Definition: qgsprocessingparameters.h:419
QgsProcessingParameterFeatureSource
Definition: qgsprocessingparameters.h:2612
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
QgsProcessingUtils::combineFields
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).
Definition: qgsprocessingutils.cpp:1113
QgsProcessingParameterFeatureSink
Definition: qgsprocessingparameters.h:2773
QgsProcessingContext
Definition: qgsprocessingcontext.h:43
QgsFeatureSink::RegeneratePrimaryKey
@ RegeneratePrimaryKey
This flag indicates, that a primary key field cannot be guaranteed to be unique and the sink should i...
Definition: qgsfeaturesink.h:55
QgsProcessingAlgorithm
Definition: qgsprocessingalgorithm.h:51
qgsoverlayutils.h
QgsProcessingException
Definition: qgsexception.h:82