QGIS API Documentation  3.2.0-Bonn (bc43194)
qgsalgorithmdifference.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsalgorithmdifference.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 "qgsalgorithmdifference.h"
17 
18 #include "qgsoverlayutils.h"
19 
21 
22 
23 QString QgsDifferenceAlgorithm::name() const
24 {
25  return QStringLiteral( "difference" );
26 }
27 
28 QString QgsDifferenceAlgorithm::displayName() const
29 {
30  return QObject::tr( "Difference" );
31 }
32 
33 QString QgsDifferenceAlgorithm::group() const
34 {
35  return QObject::tr( "Vector overlay" );
36 }
37 
38 QString QgsDifferenceAlgorithm::groupId() const
39 {
40  return QStringLiteral( "vectoroverlay" );
41 }
42 
43 QString QgsDifferenceAlgorithm::shortHelpString() const
44 {
45  return QObject::tr( "This algorithm extracts features from the Input layer that fall outside, or partially overlap, features in the Difference layer. Input layer features that partially overlap the difference layer feature(s) are split along the boundary of the difference layer feature(s) and only the portions outside the difference layer features are retained." )
46  + QStringLiteral( "\n\n" )
47  + QObject::tr( "Attributes are not modified." );
48 }
49 
50 QgsProcessingAlgorithm *QgsDifferenceAlgorithm::createInstance() const
51 {
52  return new QgsDifferenceAlgorithm();
53 }
54 
55 void QgsDifferenceAlgorithm::initAlgorithm( const QVariantMap & )
56 {
57  addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
58  addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "OVERLAY" ), QObject::tr( "Difference layer" ) ) );
59  addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Difference" ) ) );
60 }
61 
62 
63 QVariantMap QgsDifferenceAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
64 {
65  std::unique_ptr< QgsFeatureSource > sourceA( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
66  if ( !sourceA )
67  throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
68 
69  std::unique_ptr< QgsFeatureSource > sourceB( parameterAsSource( parameters, QStringLiteral( "OVERLAY" ), context ) );
70  if ( !sourceB )
71  throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "OVERLAY" ) ) );
72 
73  QgsWkbTypes::Type geomType = QgsWkbTypes::multiType( sourceA->wkbType() );
74 
75  QString dest;
76  std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, sourceA->fields(), geomType, sourceA->sourceCrs() ) );
77  if ( !sink )
78  throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
79 
80  QVariantMap outputs;
81  outputs.insert( QStringLiteral( "OUTPUT" ), dest );
82 
83  int count = 0;
84  int total = sourceA->featureCount();
85  QgsOverlayUtils::difference( *sourceA.get(), *sourceB.get(), *sink.get(), context, feedback, count, total, QgsOverlayUtils::OutputA );
86 
87  return outputs;
88 }
89 
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.
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
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.