QGIS API Documentation  3.2.0-Bonn (bc43194)
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 creates a layer containing features from both the Input and Difference layers but with the overlapping areas between the two layers removed. The attribute table of the Symmetrical Difference layer contains attributes from both the Input and Difference layers." );
45 }
46 
47 QgsProcessingAlgorithm *QgsSymmetricalDifferenceAlgorithm::createInstance() const
48 {
49  return new QgsSymmetricalDifferenceAlgorithm();
50 }
51 
52 void QgsSymmetricalDifferenceAlgorithm::initAlgorithm( const QVariantMap & )
53 {
54  addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
55  addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "OVERLAY" ), QObject::tr( "Difference layer" ) ) );
56  addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Symmetrical difference" ) ) );
57 }
58 
59 
60 QVariantMap QgsSymmetricalDifferenceAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
61 {
62  std::unique_ptr< QgsFeatureSource > sourceA( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
63  if ( !sourceA )
64  throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
65 
66  std::unique_ptr< QgsFeatureSource > sourceB( parameterAsSource( parameters, QStringLiteral( "OVERLAY" ), context ) );
67  if ( !sourceB )
68  throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "OVERLAY" ) ) );
69 
70  QgsWkbTypes::Type geomType = QgsWkbTypes::multiType( sourceA->wkbType() );
71 
72  QgsFields fields = QgsProcessingUtils::combineFields( sourceA->fields(), sourceB->fields() );
73 
74  QString dest;
75  std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, fields, geomType, sourceA->sourceCrs() ) );
76  if ( !sink )
77  throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
78 
79  QVariantMap outputs;
80  outputs.insert( QStringLiteral( "OUTPUT" ), dest );
81 
82  int count = 0;
83  int total = sourceA->featureCount() + sourceB->featureCount();
84 
85  QgsOverlayUtils::difference( *sourceA.get(), *sourceB.get(), *sink.get(), context, feedback, count, total, QgsOverlayUtils::OutputAB );
86 
87  QgsOverlayUtils::difference( *sourceB.get(), *sourceA.get(), *sink.get(), context, feedback, count, total, QgsOverlayUtils::OutputBA );
88 
89  return outputs;
90 }
91 
static Type multiType(Type type)
Returns the multi type for a WKB type.
Definition: qgswkbtypes.h:296
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:67
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.