QGIS API Documentation 3.43.0-Master (e01d6d7c4c0)
qgsalgorithmintersection.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmintersection.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
19#include "qgsgeometryengine.h"
20#include "qgsoverlayutils.h"
21
23
24
25QString QgsIntersectionAlgorithm::name() const
26{
27 return QStringLiteral( "intersection" );
28}
29
30QString QgsIntersectionAlgorithm::displayName() const
31{
32 return QObject::tr( "Intersection" );
33}
34
35QString QgsIntersectionAlgorithm::group() const
36{
37 return QObject::tr( "Vector overlay" );
38}
39
40QString QgsIntersectionAlgorithm::groupId() const
41{
42 return QStringLiteral( "vectoroverlay" );
43}
44
45QString QgsIntersectionAlgorithm::shortHelpString() const
46{
47 return QObject::tr( "This algorithm extracts the overlapping portions of features in the Input and Overlay layers. "
48 "Features in the output Intersection layer are assigned the attributes of the overlapping features "
49 "from both the Input and Overlay layers." );
50}
51
52QString QgsIntersectionAlgorithm::shortDescription() const
53{
54 return QObject::tr( "Extracts overlapping portions of features between two layers." );
55}
56
57Qgis::ProcessingAlgorithmDocumentationFlags QgsIntersectionAlgorithm::documentationFlags() const
58{
60}
61
62QgsProcessingAlgorithm *QgsIntersectionAlgorithm::createInstance() const
63{
64 return new QgsIntersectionAlgorithm();
65}
66
67void QgsIntersectionAlgorithm::initAlgorithm( const QVariantMap & )
68{
69 addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
70 addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "OVERLAY" ), QObject::tr( "Overlay layer" ) ) );
71
72 addParameter( new QgsProcessingParameterField(
73 QStringLiteral( "INPUT_FIELDS" ),
74 QObject::tr( "Input fields to keep (leave empty to keep all fields)" ), QVariant(),
75 QStringLiteral( "INPUT" ), Qgis::ProcessingFieldParameterDataType::Any, true, true
76 ) );
77 addParameter( new QgsProcessingParameterField(
78 QStringLiteral( "OVERLAY_FIELDS" ),
79 QObject::tr( "Overlay fields to keep (leave empty to keep all fields)" ), QVariant(),
80 QStringLiteral( "OVERLAY" ), Qgis::ProcessingFieldParameterDataType::Any, true, true
81 ) );
82
83 auto prefix = std::make_unique<QgsProcessingParameterString>( QStringLiteral( "OVERLAY_FIELDS_PREFIX" ), QObject::tr( "Overlay fields prefix" ), QString(), false, true );
84 prefix->setFlags( prefix->flags() | Qgis::ProcessingParameterFlag::Advanced );
85 addParameter( prefix.release() );
86
87 addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Intersection" ) ) );
88
89 auto gridSize = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "GRID_SIZE" ), QObject::tr( "Grid size" ), Qgis::ProcessingNumberParameterType::Double, QVariant(), true, 0 );
90 gridSize->setFlags( gridSize->flags() | Qgis::ProcessingParameterFlag::Advanced );
91 addParameter( gridSize.release() );
92}
93
94
95QVariantMap QgsIntersectionAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
96{
97 std::unique_ptr<QgsFeatureSource> sourceA( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
98 if ( !sourceA )
99 throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
100
101 std::unique_ptr<QgsFeatureSource> sourceB( parameterAsSource( parameters, QStringLiteral( "OVERLAY" ), context ) );
102 if ( !sourceB )
103 throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "OVERLAY" ) ) );
104
105 const Qgis::WkbType geomType = QgsWkbTypes::multiType( sourceA->wkbType() );
106
107 const QStringList fieldsA = parameterAsStrings( parameters, QStringLiteral( "INPUT_FIELDS" ), context );
108 const QStringList fieldsB = parameterAsStrings( parameters, QStringLiteral( "OVERLAY_FIELDS" ), context );
109
110 const QList<int> fieldIndicesA = QgsProcessingUtils::fieldNamesToIndices( fieldsA, sourceA->fields() );
111 const QList<int> fieldIndicesB = QgsProcessingUtils::fieldNamesToIndices( fieldsB, sourceB->fields() );
112
113 const QString overlayFieldsPrefix = parameterAsString( parameters, QStringLiteral( "OVERLAY_FIELDS_PREFIX" ), context );
114 const QgsFields outputFields = QgsProcessingUtils::combineFields(
115 QgsProcessingUtils::indicesToFields( fieldIndicesA, sourceA->fields() ),
116 QgsProcessingUtils::indicesToFields( fieldIndicesB, sourceB->fields() ),
117 overlayFieldsPrefix
118 );
119
120 QString dest;
121 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, outputFields, geomType, sourceA->sourceCrs(), QgsFeatureSink::RegeneratePrimaryKey ) );
122 if ( !sink )
123 throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
124
125 QVariantMap outputs;
126 outputs.insert( QStringLiteral( "OUTPUT" ), dest );
127
128 long count = 0;
129 const long total = sourceA->featureCount();
130
131 QgsGeometryParameters geometryParameters;
132 if ( parameters.value( QStringLiteral( "GRID_SIZE" ) ).isValid() )
133 {
134 geometryParameters.setGridSize( parameterAsDouble( parameters, QStringLiteral( "GRID_SIZE" ), context ) );
135 }
136
137 QgsOverlayUtils::intersection( *sourceA, *sourceB, *sink, context, feedback, count, total, fieldIndicesA, fieldIndicesB, geometryParameters );
138
139 sink->finalize();
140
141 return outputs;
142}
143
@ RegeneratesPrimaryKey
Algorithm always drops any existing primary keys or FID values and regenerates them in outputs.
QFlags< ProcessingAlgorithmDocumentationFlag > ProcessingAlgorithmDocumentationFlags
Flags describing algorithm behavior for documentation purposes.
Definition qgis.h:3496
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:256
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
@ RegeneratePrimaryKey
This flag indicates, that a primary key field cannot be guaranteed to be unique and the sink should i...
Container of fields for a vector layer.
Definition qgsfields.h:46
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.
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.
A feature sink output for processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.
A vector layer or feature source field parameter for processing algorithms.
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 Qgis::WkbType multiType(Qgis::WkbType type)
Returns the multi type for a WKB type.