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