-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
qgsalgorithmdifference.cpp
128 lines (101 loc) · 5.09 KB
/
qgsalgorithmdifference.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/***************************************************************************
qgsalgorithmdifference.cpp
---------------------
Date : April 2018
Copyright : (C) 2018 by Martin Dobias
Email : wonder dot sk at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsalgorithmdifference.h"
#include "qgsoverlayutils.h"
#include "qgsvectorlayer.h"
///@cond PRIVATE
QString QgsDifferenceAlgorithm::name() const
{
return QStringLiteral( "difference" );
}
QString QgsDifferenceAlgorithm::displayName() const
{
return QObject::tr( "Difference" );
}
QStringList QgsDifferenceAlgorithm::tags() const
{
return QObject::tr( "difference,erase,not overlap" ).split( ',' );
}
QString QgsDifferenceAlgorithm::group() const
{
return QObject::tr( "Vector overlay" );
}
QString QgsDifferenceAlgorithm::groupId() const
{
return QStringLiteral( "vectoroverlay" );
}
QString QgsDifferenceAlgorithm::shortHelpString() const
{
return QObject::tr( "This algorithm extracts features from the Input layer that fall outside, or partially overlap, features in the Overlay layer. "
"Input layer features that partially overlap feature(s) in the Overlay layer are split along those features' boundary "
"and only the portions outside the Overlay layer features are retained." )
+ QStringLiteral( "\n\n" )
+ QObject::tr( "Attributes are not modified, although properties such as area or length of the features will "
"be modified by the difference operation. If such properties are stored as attributes, those attributes will have to "
"be manually updated." );
}
bool QgsDifferenceAlgorithm::supportInPlaceEdit( const QgsMapLayer *l ) const
{
const QgsVectorLayer *layer = qobject_cast< const QgsVectorLayer * >( l );
if ( !layer )
return false;
return layer->isSpatial();
}
QgsProcessingAlgorithm::Flags QgsDifferenceAlgorithm::flags() const
{
Flags f = QgsProcessingAlgorithm::flags();
f |= QgsProcessingAlgorithm::FlagSupportsInPlaceEdits;
return f;
}
QgsProcessingAlgorithm *QgsDifferenceAlgorithm::createInstance() const
{
return new QgsDifferenceAlgorithm();
}
void QgsDifferenceAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "OVERLAY" ), QObject::tr( "Overlay layer" ) ) );
addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Difference" ) ) );
std::unique_ptr< QgsProcessingParameterNumber > gridSize = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "GRID_SIZE" ),
QObject::tr( "Grid size" ), QgsProcessingParameterNumber::Double, QVariant(), true, 0 );
gridSize->setFlags( gridSize->flags() | QgsProcessingParameterDefinition::FlagAdvanced );
addParameter( gridSize.release() );
}
QVariantMap QgsDifferenceAlgorithm::processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
{
std::unique_ptr< QgsFeatureSource > sourceA( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
if ( !sourceA )
throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
const std::unique_ptr< QgsFeatureSource > sourceB( parameterAsSource( parameters, QStringLiteral( "OVERLAY" ), context ) );
if ( !sourceB )
throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "OVERLAY" ) ) );
const QgsWkbTypes::Type geomType = QgsWkbTypes::promoteNonPointTypesToMulti( sourceA->wkbType() );
QString dest;
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, sourceA->fields(), geomType, sourceA->sourceCrs() ) );
if ( !sink )
throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
QVariantMap outputs;
outputs.insert( QStringLiteral( "OUTPUT" ), dest );
long count = 0;
const long total = sourceA->featureCount();
QgsGeometryParameters geometryParameters;
if ( parameters.value( QStringLiteral( "GRID_SIZE" ) ).isValid() )
{
geometryParameters.setGridSize( parameterAsDouble( parameters, QStringLiteral( "GRID_SIZE" ), context ) );
}
QgsOverlayUtils::difference( *sourceA, *sourceB, *sink, context, feedback, count, total, QgsOverlayUtils::OutputA, geometryParameters );
return outputs;
}
///@endcond PRIVATE