QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
Loading...
Searching...
No Matches
qgsalgorithmmultiintersection.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmmultiintersection.cpp
3 ------------------
4 begin : December 2021
5 copyright : (C) 2021 by Alexander Bruy
6 email : alexander dot bruy at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
20#include "qgsoverlayutils.h"
21#include "qgsvectorlayer.h"
22
24
25
26QString QgsMultiIntersectionAlgorithm::name() const
27{
28 return QStringLiteral( "multiintersection" );
29}
30
31QString QgsMultiIntersectionAlgorithm::displayName() const
32{
33 return QObject::tr( "Intersection (multiple)" );
34}
35
36QStringList QgsMultiIntersectionAlgorithm::tags() const
37{
38 return QObject::tr( "intersection,extract,overlap" ).split( ',' );
39}
40
41QString QgsMultiIntersectionAlgorithm::group() const
42{
43 return QObject::tr( "Vector overlay" );
44}
45
46QString QgsMultiIntersectionAlgorithm::groupId() const
47{
48 return QStringLiteral( "vectoroverlay" );
49}
50
51QString QgsMultiIntersectionAlgorithm::shortHelpString() const
52{
53 return QObject::tr( "This algorithm extracts the overlapping portions of features in the Input and all Overlay layers. "
54 "Features in the output layer are assigned the attributes of the overlapping features "
55 "from both the Input and Overlay layers." );
56}
57
58QgsProcessingAlgorithm *QgsMultiIntersectionAlgorithm::createInstance() const
59{
60 return new QgsMultiIntersectionAlgorithm();
61}
62
63void QgsMultiIntersectionAlgorithm::initAlgorithm( const QVariantMap & )
64{
65 addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
66 addParameter( new QgsProcessingParameterMultipleLayers( QStringLiteral( "OVERLAYS" ), QObject::tr( "Overlay layers" ), QgsProcessing::TypeVectorAnyGeometry ) );
67
68 std::unique_ptr< QgsProcessingParameterString > prefix = std::make_unique< QgsProcessingParameterString >( QStringLiteral( "OVERLAY_FIELDS_PREFIX" ), QObject::tr( "Overlay fields prefix" ), QString(), false, true );
69 prefix->setFlags( prefix->flags() | QgsProcessingParameterDefinition::FlagAdvanced );
70 addParameter( prefix.release() );
71
72 addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Intersection" ) ) );
73}
74
75QVariantMap QgsMultiIntersectionAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
76{
77 std::unique_ptr< QgsFeatureSource > sourceA( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
78 if ( !sourceA )
79 throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
80
81 const QList< QgsMapLayer * > layers = parameterAsLayerList( parameters, QStringLiteral( "OVERLAYS" ), context );
82
83 // loop through overlay layers and check whether they are vectors
84 long totalLayerCount = 0;
85 for ( QgsMapLayer *layer : layers )
86 {
87 if ( feedback->isCanceled() )
88 break;
89
90 if ( !layer )
91 throw QgsProcessingException( QObject::tr( "Error retrieving map layer." ) );
92
93 if ( layer->type() != Qgis::LayerType::Vector )
94 throw QgsProcessingException( QObject::tr( "All layers must be vector layers!" ) );
95
96 totalLayerCount++;
97 }
98
99 const QString overlayFieldsPrefix = parameterAsString( parameters, QStringLiteral( "OVERLAY_FIELDS_PREFIX" ), context );
100
101 const Qgis::WkbType geometryType = QgsWkbTypes::multiType( sourceA->wkbType() );
102 const QgsCoordinateReferenceSystem crs = sourceA->sourceCrs();
103 std::unique_ptr< QgsFeatureSink > sink;
104 long count = 0;
105 QVariantMap outputs;
106
107 QList<int> fieldIndicesA, fieldIndicesB;
108 QgsFields outputFields;
109
110 if ( totalLayerCount == 1 )
111 {
112 QgsVectorLayer *overlayLayer = qobject_cast< QgsVectorLayer * >( layers.at( 0 ) );
113
114 fieldIndicesA = QgsProcessingUtils::fieldNamesToIndices( QStringList(), sourceA->fields() );
115 fieldIndicesB = QgsProcessingUtils::fieldNamesToIndices( QStringList(), overlayLayer->fields() );
116
118 QgsProcessingUtils::indicesToFields( fieldIndicesA, sourceA->fields() ),
119 QgsProcessingUtils::indicesToFields( fieldIndicesB, overlayLayer->fields() ),
120 overlayFieldsPrefix );
121
122 QString dest;
123 sink.reset( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, outputFields, geometryType, crs, QgsFeatureSink::RegeneratePrimaryKey ) );
124 if ( !sink )
125 throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
126
127 outputs.insert( QStringLiteral( "OUTPUT" ), dest );
128
129 const long total = sourceA->featureCount();
130 QgsOverlayUtils::intersection( *sourceA, *overlayLayer, *sink, context, feedback, count, total, fieldIndicesA, fieldIndicesB );
131 }
132 else
133 {
134 QgsProcessingMultiStepFeedback multiStepFeedback( totalLayerCount, feedback );
135 QgsVectorLayer *intersectionLayer = nullptr;
136
137 long i = 0;
138 for ( QgsMapLayer *layer : layers )
139 {
140 if ( feedback->isCanceled() )
141 break;
142
143 multiStepFeedback.setCurrentStep( i );
144
145 if ( !layer )
146 continue;
147
148 QgsVectorLayer *overlayLayer = qobject_cast< QgsVectorLayer * >( layer );
149 if ( !overlayLayer )
150 continue;
151
152 count = 0;
153 if ( i == 0 )
154 {
155 fieldIndicesA = QgsProcessingUtils::fieldNamesToIndices( QStringList(), sourceA->fields() );
156 fieldIndicesB = QgsProcessingUtils::fieldNamesToIndices( QStringList(), overlayLayer->fields() );
157
159 QgsProcessingUtils::indicesToFields( fieldIndicesA, sourceA->fields() ),
160 QgsProcessingUtils::indicesToFields( fieldIndicesB, overlayLayer->fields() ),
161 overlayFieldsPrefix );
162
163 QString id = QStringLiteral( "memory:" );
164 sink.reset( QgsProcessingUtils::createFeatureSink( id, context, outputFields, geometryType, crs ) );
165 QgsOverlayUtils::intersection( *sourceA, *overlayLayer, *sink, context, &multiStepFeedback, count, sourceA->featureCount(), fieldIndicesA, fieldIndicesB );
166
167 intersectionLayer = qobject_cast< QgsVectorLayer * >( QgsProcessingUtils::mapLayerFromString( id, context ) );
168 }
169 else if ( i == totalLayerCount - 1 )
170 {
171 fieldIndicesA = QgsProcessingUtils::fieldNamesToIndices( QStringList(), intersectionLayer->fields() );
172 fieldIndicesB = QgsProcessingUtils::fieldNamesToIndices( QStringList(), overlayLayer->fields() );
173
175 QgsProcessingUtils::indicesToFields( fieldIndicesA, intersectionLayer->fields() ),
176 QgsProcessingUtils::indicesToFields( fieldIndicesB, overlayLayer->fields() ),
177 overlayFieldsPrefix );
178
179 QString dest;
180 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, outputFields, geometryType, crs ) );
181 if ( !sink )
182 throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
183
184 outputs.insert( QStringLiteral( "OUTPUT" ), dest );
185
186 QgsOverlayUtils::intersection( *intersectionLayer, *overlayLayer, *sink, context, &multiStepFeedback, count, intersectionLayer->featureCount(), fieldIndicesA, fieldIndicesB );
187 }
188 else
189 {
190 fieldIndicesA = QgsProcessingUtils::fieldNamesToIndices( QStringList(), intersectionLayer->fields() );
191 fieldIndicesB = QgsProcessingUtils::fieldNamesToIndices( QStringList(), overlayLayer->fields() );
192
194 QgsProcessingUtils::indicesToFields( fieldIndicesA, intersectionLayer->fields() ),
195 QgsProcessingUtils::indicesToFields( fieldIndicesB, overlayLayer->fields() ),
196 overlayFieldsPrefix );
197
198 QString id = QStringLiteral( "memory:" );
199 sink.reset( QgsProcessingUtils::createFeatureSink( id, context, outputFields, geometryType, crs ) );
200 QgsOverlayUtils::intersection( *intersectionLayer, *overlayLayer, *sink, context, &multiStepFeedback, count, intersectionLayer->featureCount(), fieldIndicesA, fieldIndicesB );
201
202 intersectionLayer = qobject_cast< QgsVectorLayer * >( QgsProcessingUtils::mapLayerFromString( id, context ) );
203 }
204
205 i++;
206 }
207 }
208
209 return outputs;
210}
211
@ Vector
Vector layer.
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:182
This class represents a coordinate reference system (CRS).
@ RegeneratePrimaryKey
This flag indicates, that a primary key field cannot be guaranteed to be unique and the sink should i...
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:54
Container of fields for a vector layer.
Definition qgsfields.h:45
Base class for all map layer types.
Definition qgsmaplayer.h:74
Abstract base class for processing algorithms.
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.
Processing feedback object for multi-step operations.
@ FlagAdvanced
Parameter is an advanced parameter which should be hidden from users by default.
A feature sink output for processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.
A parameter for processing algorithms which accepts multiple map layers.
static QgsFields indicesToFields(const QList< int > &indices, const QgsFields &fields)
Returns a subset of fields based on the indices of desired fields.
static QList< int > fieldNamesToIndices(const QStringList &fieldNames, const QgsFields &fields)
Returns a list of field indices parsed from the given list of field names.
static QgsFields combineFields(const QgsFields &fieldsA, const QgsFields &fieldsB, const QString &fieldsBPrefix=QString())
Combines two field lists, avoiding duplicate field names (in a case-insensitive manner).
static QgsFeatureSink * createFeatureSink(QString &destination, QgsProcessingContext &context, const QgsFields &fields, Qgis::WkbType geometryType, const QgsCoordinateReferenceSystem &crs, const QVariantMap &createOptions=QVariantMap(), const QStringList &datasourceOptions=QStringList(), const QStringList &layerOptions=QStringList(), QgsFeatureSink::SinkFlags sinkFlags=QgsFeatureSink::SinkFlags(), QgsRemappingSinkDefinition *remappingDefinition=nullptr)
Creates a feature sink ready for adding features.
static QgsMapLayer * mapLayerFromString(const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers=true, QgsProcessingUtils::LayerHint typeHint=QgsProcessingUtils::LayerHint::UnknownType, QgsProcessing::LayerOptionsFlags flags=QgsProcessing::LayerOptionsFlags())
Interprets a string as a map layer within the supplied context.
@ TypeVectorAnyGeometry
Any vector layer with geometry.
Represents a vector layer which manages a vector based data sets.
long long featureCount(const QString &legendKey) const
Number of features rendered with specified legend key.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
static Qgis::WkbType multiType(Qgis::WkbType type)
Returns the multi type for a WKB type.
const QgsCoordinateReferenceSystem & crs