QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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(
52 "This algorithm extracts the overlapping portions of features in the Input and Overlay layers. "
53 "Features in the output Intersection layer are assigned the attributes of the overlapping features "
54 "from both the Input and Overlay layers."
55 );
56}
57
58QString QgsIntersectionAlgorithm::shortDescription() const
59{
60 return QObject::tr( "Extracts overlapping portions of features between two layers." );
61}
62
63Qgis::ProcessingAlgorithmDocumentationFlags QgsIntersectionAlgorithm::documentationFlags() const
64{
66}
67
68QgsProcessingAlgorithm *QgsIntersectionAlgorithm::createInstance() const
69{
70 return new QgsIntersectionAlgorithm();
71}
72
73void QgsIntersectionAlgorithm::initAlgorithm( const QVariantMap & )
74{
75 addParameter( new QgsProcessingParameterFeatureSource( u"INPUT"_s, QObject::tr( "Input layer" ) ) );
76 addParameter( new QgsProcessingParameterFeatureSource( u"OVERLAY"_s, QObject::tr( "Overlay layer" ) ) );
77
78 addParameter(
79 new QgsProcessingParameterField( u"INPUT_FIELDS"_s, QObject::tr( "Input fields to keep (leave empty to keep all fields)" ), QVariant(), u"INPUT"_s, Qgis::ProcessingFieldParameterDataType::Any, true, true )
80 );
81 addParameter(
82 new QgsProcessingParameterField( u"OVERLAY_FIELDS"_s, QObject::tr( "Overlay fields to keep (leave empty to keep all fields)" ), QVariant(), u"OVERLAY"_s, Qgis::ProcessingFieldParameterDataType::Any, true, true )
83 );
84
85 auto prefix = std::make_unique<QgsProcessingParameterString>( u"OVERLAY_FIELDS_PREFIX"_s, QObject::tr( "Overlay fields prefix" ), QString(), false, true );
86 prefix->setFlags( prefix->flags() | Qgis::ProcessingParameterFlag::Advanced );
87 addParameter( prefix.release() );
88
89 addParameter( new QgsProcessingParameterFeatureSink( u"OUTPUT"_s, QObject::tr( "Intersection" ) ) );
90
91 auto gridSize = std::make_unique<QgsProcessingParameterNumber>( u"GRID_SIZE"_s, QObject::tr( "Grid size" ), Qgis::ProcessingNumberParameterType::Double, QVariant(), true, 0 );
92 gridSize->setFlags( gridSize->flags() | Qgis::ProcessingParameterFlag::Advanced );
93 addParameter( gridSize.release() );
94}
95
96
97QVariantMap QgsIntersectionAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
98{
99 std::unique_ptr<QgsFeatureSource> sourceA( parameterAsSource( parameters, u"INPUT"_s, context ) );
100 if ( !sourceA )
101 throw QgsProcessingException( invalidSourceError( parameters, u"INPUT"_s ) );
102
103 std::unique_ptr<QgsFeatureSource> sourceB( parameterAsSource( parameters, u"OVERLAY"_s, context ) );
104 if ( !sourceB )
105 throw QgsProcessingException( invalidSourceError( parameters, u"OVERLAY"_s ) );
106
107 const Qgis::WkbType geomType = QgsWkbTypes::multiType( sourceA->wkbType() );
108
109 const QStringList fieldsA = parameterAsStrings( parameters, u"INPUT_FIELDS"_s, context );
110 const QStringList fieldsB = parameterAsStrings( parameters, u"OVERLAY_FIELDS"_s, context );
111
112 const QList<int> fieldIndicesA = QgsProcessingUtils::fieldNamesToIndices( fieldsA, sourceA->fields() );
113 const QList<int> fieldIndicesB = QgsProcessingUtils::fieldNamesToIndices( fieldsB, sourceB->fields() );
114
115 const QString overlayFieldsPrefix = parameterAsString( parameters, u"OVERLAY_FIELDS_PREFIX"_s, context );
116 const QgsFields outputFields
117 = QgsProcessingUtils::combineFields( QgsProcessingUtils::indicesToFields( fieldIndicesA, sourceA->fields() ), QgsProcessingUtils::indicesToFields( fieldIndicesB, sourceB->fields() ), overlayFieldsPrefix );
118
119 QString dest;
120 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u"OUTPUT"_s, context, dest, outputFields, geomType, sourceA->sourceCrs(), QgsFeatureSink::RegeneratePrimaryKey ) );
121 if ( !sink )
122 throw QgsProcessingException( invalidSinkError( parameters, u"OUTPUT"_s ) );
123
124 QVariantMap outputs;
125 outputs.insert( u"OUTPUT"_s, dest );
126
127 long count = 0;
128 const long total = sourceA->featureCount();
129
130 QgsGeometryParameters geometryParameters;
131 if ( parameters.value( u"GRID_SIZE"_s ).isValid() )
132 {
133 geometryParameters.setGridSize( parameterAsDouble( parameters, u"GRID_SIZE"_s, context ) );
134 }
135
136 QgsOverlayUtils::intersection( *sourceA, *sourceB, *sink, context, feedback, count, total, fieldIndicesA, fieldIndicesB, geometryParameters );
137
138 sink->finalize();
139
140 return outputs;
141}
142
@ RegeneratesPrimaryKey
Algorithm always drops any existing primary keys or FID values and regenerates them in outputs.
Definition qgis.h:3734
QFlags< ProcessingAlgorithmDocumentationFlag > ProcessingAlgorithmDocumentationFlags
Flags describing algorithm behavior for documentation purposes.
Definition qgis.h:3745
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:294
@ 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
@ 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.