QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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
17
18#include "qgsoverlayutils.h"
19#include "qgsvectorlayer.h"
20
22
23
24QString QgsDifferenceAlgorithm::name() const
25{
26 return QStringLiteral( "difference" );
27}
28
29QString QgsDifferenceAlgorithm::displayName() const
30{
31 return QObject::tr( "Difference" );
32}
33
34QStringList QgsDifferenceAlgorithm::tags() const
35{
36 return QObject::tr( "difference,erase,not overlap" ).split( ',' );
37}
38
39QString QgsDifferenceAlgorithm::group() const
40{
41 return QObject::tr( "Vector overlay" );
42}
43
44QString QgsDifferenceAlgorithm::groupId() const
45{
46 return QStringLiteral( "vectoroverlay" );
47}
48
49QString QgsDifferenceAlgorithm::shortHelpString() const
50{
51 return QObject::tr( "This algorithm extracts features from the Input layer that fall outside, or partially overlap, features in the Overlay layer. "
52 "Input layer features that partially overlap feature(s) in the Overlay layer are split along those features' boundary "
53 "and only the portions outside the Overlay layer features are retained." )
54 + QStringLiteral( "\n\n" )
55 + QObject::tr( "Attributes are not modified, although properties such as area or length of the features will "
56 "be modified by the difference operation. If such properties are stored as attributes, those attributes will have to "
57 "be manually updated." );
58}
59
60bool QgsDifferenceAlgorithm::supportInPlaceEdit( const QgsMapLayer *l ) const
61{
62 const QgsVectorLayer *layer = qobject_cast< const QgsVectorLayer * >( l );
63 if ( !layer )
64 return false;
65
66 return layer->isSpatial();
67}
68
69Qgis::ProcessingAlgorithmFlags QgsDifferenceAlgorithm::flags() const
70{
73 return f;
74}
75
76QgsProcessingAlgorithm *QgsDifferenceAlgorithm::createInstance() const
77{
78 return new QgsDifferenceAlgorithm();
79}
80
81void QgsDifferenceAlgorithm::initAlgorithm( const QVariantMap & )
82{
83 addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
84 addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "OVERLAY" ), QObject::tr( "Overlay layer" ) ) );
85 addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Difference" ) ) );
86
87 std::unique_ptr< QgsProcessingParameterNumber > gridSize = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "GRID_SIZE" ),
88 QObject::tr( "Grid size" ), Qgis::ProcessingNumberParameterType::Double, QVariant(), true, 0 );
89 gridSize->setFlags( gridSize->flags() | Qgis::ProcessingParameterFlag::Advanced );
90 addParameter( gridSize.release() );
91}
92
93
94QVariantMap QgsDifferenceAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
95{
96 std::unique_ptr< QgsFeatureSource > sourceA( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
97 if ( !sourceA )
98 throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
99
100 const std::unique_ptr< QgsFeatureSource > sourceB( parameterAsSource( parameters, QStringLiteral( "OVERLAY" ), context ) );
101 if ( !sourceB )
102 throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "OVERLAY" ) ) );
103
104 const Qgis::WkbType geomType = QgsWkbTypes::promoteNonPointTypesToMulti( sourceA->wkbType() );
105
106 QString dest;
107 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, sourceA->fields(), geomType, sourceA->sourceCrs() ) );
108 if ( !sink )
109 throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
110
111 QVariantMap outputs;
112 outputs.insert( QStringLiteral( "OUTPUT" ), dest );
113
114 long count = 0;
115 const long total = sourceA->featureCount();
116
117 QgsGeometryParameters geometryParameters;
118 if ( parameters.value( QStringLiteral( "GRID_SIZE" ) ).isValid() )
119 {
120 geometryParameters.setGridSize( parameterAsDouble( parameters, QStringLiteral( "GRID_SIZE" ), context ) );
121 }
122
123 QgsOverlayUtils::difference( *sourceA, *sourceB, *sink, context, feedback, count, total, QgsOverlayUtils::OutputA, geometryParameters, QgsOverlayUtils::SanitizeFlag::DontPromotePointGeometryToMultiPoint );
124
125 return outputs;
126}
127
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
Definition: qgis.h:2934
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition: qgis.h:182
@ SupportsInPlaceEdits
Algorithm supports in-place editing.
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
Encapsulates parameters under which a geometry operation is performed.
Definition: qgsgeometry.h:109
void setGridSize(double size)
Sets the grid size which will be used to snap vertices of a geometry.
Definition: qgsgeometry.h:134
Base class for all map layer types.
Definition: qgsmaplayer.h:75
Abstract base class for processing algorithms.
virtual Qgis::ProcessingAlgorithmFlags flags() const
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Definition: qgsexception.h:83
Base class for providing feedback from a processing algorithm.
A feature sink output for processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.
Represents a vector layer which manages a vector based data sets.
bool isSpatial() const FINAL
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
static Qgis::WkbType promoteNonPointTypesToMulti(Qgis::WkbType type)
Promotes a WKB geometry type to its multi-type equivalent, with the exception of point geometry types...
Definition: qgswkbtypes.h:347