QGIS API Documentation  3.6.0-Noosa (5873452)
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  addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Symmetrical difference" ) ) );
59 }
60 
61 
62 QVariantMap QgsSymmetricalDifferenceAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
63 {
64  std::unique_ptr< QgsFeatureSource > sourceA( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
65  if ( !sourceA )
66  throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
67 
68  std::unique_ptr< QgsFeatureSource > sourceB( parameterAsSource( parameters, QStringLiteral( "OVERLAY" ), context ) );
69  if ( !sourceB )
70  throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "OVERLAY" ) ) );
71 
72  QgsWkbTypes::Type geomType = QgsWkbTypes::multiType( sourceA->wkbType() );
73 
74  QgsFields fields = QgsProcessingUtils::combineFields( sourceA->fields(), sourceB->fields() );
75 
76  QString dest;
77  std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, fields, geomType, sourceA->sourceCrs() ) );
78  if ( !sink )
79  throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
80 
81  QVariantMap outputs;
82  outputs.insert( QStringLiteral( "OUTPUT" ), dest );
83 
84  int count = 0;
85  int total = sourceA->featureCount() + sourceB->featureCount();
86 
87  QgsOverlayUtils::difference( *sourceA, *sourceB, *sink, context, feedback, count, total, QgsOverlayUtils::OutputAB );
88 
89  QgsOverlayUtils::difference( *sourceB, *sourceA, *sink, context, feedback, count, total, QgsOverlayUtils::OutputBA );
90 
91  return outputs;
92 }
93 
static Type multiType(Type type)
Returns the multi type for a WKB type.
Definition: qgswkbtypes.h:298
Base class for providing feedback from a processing algorithm.
Container of fields for a vector layer.
Definition: qgsfields.h:42
Abstract base class for processing algorithms.
A feature sink output for processing algorithms.
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
static QgsFields combineFields(const QgsFields &fieldsA, const QgsFields &fieldsB)
Combines two field lists, avoiding duplicate field names (in a case-insensitive manner).
Custom exception class for processing related exceptions.
Definition: qgsexception.h:82
An input feature source (such as vector layers) parameter for processing algorithms.
Contains information about the context in which a processing algorithm is executed.