QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 #include "qgsvectorlayer.h"
20 
22 
23 
24 QString QgsDifferenceAlgorithm::name() const
25 {
26  return QStringLiteral( "difference" );
27 }
28 
29 QString QgsDifferenceAlgorithm::displayName() const
30 {
31  return QObject::tr( "Difference" );
32 }
33 
34 QString QgsDifferenceAlgorithm::group() const
35 {
36  return QObject::tr( "Vector overlay" );
37 }
38 
39 QString QgsDifferenceAlgorithm::groupId() const
40 {
41  return QStringLiteral( "vectoroverlay" );
42 }
43 
44 QString QgsDifferenceAlgorithm::shortHelpString() const
45 {
46  return QObject::tr( "This algorithm extracts features from the Input layer that fall outside, or partially overlap, features in the Overlay layer. "
47  "Input layer features that partially overlap feature(s) in the Overlay layer are split along those features' boundary "
48  "and only the portions outside the Overlay layer features are retained." )
49  + QStringLiteral( "\n\n" )
50  + QObject::tr( "Attributes are not modified, although properties such as area or length of the features will "
51  "be modified by the difference operation. If such properties are stored as attributes, those attributes will have to "
52  "be manually updated." );
53 }
54 
55 bool QgsDifferenceAlgorithm::supportInPlaceEdit( const QgsMapLayer *l ) const
56 {
57  const QgsVectorLayer *layer = qobject_cast< const QgsVectorLayer * >( l );
58  if ( !layer )
59  return false;
60 
61  return layer->isSpatial();
62 }
63 
64 QgsProcessingAlgorithm::Flags QgsDifferenceAlgorithm::flags() const
65 {
68  return f;
69 }
70 
71 QgsProcessingAlgorithm *QgsDifferenceAlgorithm::createInstance() const
72 {
73  return new QgsDifferenceAlgorithm();
74 }
75 
76 void QgsDifferenceAlgorithm::initAlgorithm( const QVariantMap & )
77 {
78  addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
79  addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "OVERLAY" ), QObject::tr( "Overlay layer" ) ) );
80  addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Difference" ) ) );
81 }
82 
83 
84 QVariantMap QgsDifferenceAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
85 {
86  std::unique_ptr< QgsFeatureSource > sourceA( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
87  if ( !sourceA )
88  throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
89 
90  std::unique_ptr< QgsFeatureSource > sourceB( parameterAsSource( parameters, QStringLiteral( "OVERLAY" ), context ) );
91  if ( !sourceB )
92  throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "OVERLAY" ) ) );
93 
94  QgsWkbTypes::Type geomType = QgsWkbTypes::multiType( sourceA->wkbType() );
95 
96  QString dest;
97  std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, sourceA->fields(), geomType, sourceA->sourceCrs() ) );
98  if ( !sink )
99  throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
100 
101  QVariantMap outputs;
102  outputs.insert( QStringLiteral( "OUTPUT" ), dest );
103 
104  int count = 0;
105  int total = sourceA->featureCount();
106  QgsOverlayUtils::difference( *sourceA, *sourceB, *sink, context, feedback, count, total, QgsOverlayUtils::OutputA );
107 
108  return outputs;
109 }
110 
Base class for all map layer types.
Definition: qgsmaplayer.h:79
static Type multiType(Type type)
Returns the multi type for a WKB type.
Definition: qgswkbtypes.h:301
Base class for providing feedback from a processing algorithm.
virtual Flags flags() const
Returns the flags indicating how and when the algorithm operates and should be exposed to users...
Abstract base class for processing algorithms.
bool isSpatial() const FINAL
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
A feature sink output for processing algorithms.
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
Custom exception class for processing related exceptions.
Definition: qgsexception.h:82
An input feature source (such as vector layers) parameter for processing algorithms.
Represents a vector layer which manages a vector based data sets.
Contains information about the context in which a processing algorithm is executed.