QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
Loading...
Searching...
No Matches
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
21#include <QString>
22
23using namespace Qt::StringLiterals;
24
26
27
28QString QgsDifferenceAlgorithm::name() const
29{
30 return u"difference"_s;
31}
32
33QString QgsDifferenceAlgorithm::displayName() const
34{
35 return QObject::tr( "Difference" );
36}
37
38QStringList QgsDifferenceAlgorithm::tags() const
39{
40 return QObject::tr( "difference,erase,not overlap" ).split( ',' );
41}
42
43QString QgsDifferenceAlgorithm::group() const
44{
45 return QObject::tr( "Vector overlay" );
46}
47
48QString QgsDifferenceAlgorithm::groupId() const
49{
50 return u"vectoroverlay"_s;
51}
52
53QString QgsDifferenceAlgorithm::shortHelpString() const
54{
55 return QObject::tr(
56 "This algorithm extracts features from the Input layer that fall outside, or partially overlap, features in the Overlay layer. "
57 "Input layer features that partially overlap feature(s) in the Overlay layer are split along those features' boundary "
58 "and only the portions outside the Overlay layer features are retained."
59 )
60 + u"\n\n"_s
61 + QObject::tr(
62 "Attributes are not modified, although properties such as area or length of the features will "
63 "be modified by the difference operation. If such properties are stored as attributes, those attributes will have to "
64 "be manually updated."
65 );
66}
67
68QString QgsDifferenceAlgorithm::shortDescription() const
69{
70 return QObject::tr( "Extracts features from an input layer that fall outside, or partially overlap, features in an overlay layer." );
71}
72
73bool QgsDifferenceAlgorithm::supportInPlaceEdit( const QgsMapLayer *l ) const
74{
75 const QgsVectorLayer *layer = qobject_cast<const QgsVectorLayer *>( l );
76 if ( !layer )
77 return false;
78
79 return layer->isSpatial();
80}
81
82Qgis::ProcessingAlgorithmFlags QgsDifferenceAlgorithm::flags() const
83{
86 return f;
87}
88
89QgsProcessingAlgorithm *QgsDifferenceAlgorithm::createInstance() const
90{
91 return new QgsDifferenceAlgorithm();
92}
93
94void QgsDifferenceAlgorithm::initAlgorithm( const QVariantMap & )
95{
96 addParameter( new QgsProcessingParameterFeatureSource( u"INPUT"_s, QObject::tr( "Input layer" ) ) );
97 addParameter( new QgsProcessingParameterFeatureSource( u"OVERLAY"_s, QObject::tr( "Overlay layer" ) ) );
98 addParameter( new QgsProcessingParameterFeatureSink( u"OUTPUT"_s, QObject::tr( "Difference" ) ) );
99
100 auto gridSize = std::make_unique<QgsProcessingParameterNumber>( u"GRID_SIZE"_s, QObject::tr( "Grid size" ), Qgis::ProcessingNumberParameterType::Double, QVariant(), true, 0 );
101 gridSize->setFlags( gridSize->flags() | Qgis::ProcessingParameterFlag::Advanced );
102 addParameter( gridSize.release() );
103}
104
105
106QVariantMap QgsDifferenceAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
107{
108 std::unique_ptr<QgsFeatureSource> sourceA( parameterAsSource( parameters, u"INPUT"_s, context ) );
109 if ( !sourceA )
110 throw QgsProcessingException( invalidSourceError( parameters, u"INPUT"_s ) );
111
112 const std::unique_ptr<QgsFeatureSource> sourceB( parameterAsSource( parameters, u"OVERLAY"_s, context ) );
113 if ( !sourceB )
114 throw QgsProcessingException( invalidSourceError( parameters, u"OVERLAY"_s ) );
115
116 const Qgis::WkbType geomType = QgsWkbTypes::promoteNonPointTypesToMulti( sourceA->wkbType() );
117
118 QString dest;
119 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u"OUTPUT"_s, context, dest, sourceA->fields(), geomType, sourceA->sourceCrs() ) );
120 if ( !sink )
121 throw QgsProcessingException( invalidSinkError( parameters, u"OUTPUT"_s ) );
122
123 QVariantMap outputs;
124 outputs.insert( u"OUTPUT"_s, dest );
125
126 long count = 0;
127 const long total = sourceA->featureCount();
128
129 QgsGeometryParameters geometryParameters;
130 if ( parameters.value( u"GRID_SIZE"_s ).isValid() )
131 {
132 geometryParameters.setGridSize( parameterAsDouble( parameters, u"GRID_SIZE"_s, context ) );
133 }
134
135 QgsOverlayUtils::difference( *sourceA, *sourceB, *sink, context, feedback, count, total, QgsOverlayUtils::OutputA, geometryParameters, QgsOverlayUtils::SanitizeFlag::DontPromotePointGeometryToMultiPoint );
136
137 sink->finalize();
138 return outputs;
139}
140
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
Definition qgis.h:3724
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:294
@ SupportsInPlaceEdits
Algorithm supports in-place editing.
Definition qgis.h:3705
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
Definition qgis.h:3880
@ Double
Double/float values.
Definition qgis.h:3921
Encapsulates parameters under which a geometry operation is performed.
void setGridSize(double size)
Sets the grid size which will be used to snap vertices of a geometry.
Base class for all map layer types.
Definition qgsmaplayer.h:83
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.
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 dataset.
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...