QGIS API Documentation 4.3.0-Master (45633be667c)
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
39QStringList QgsIntersectionAlgorithm::tags() const
40{
41 return QObject::tr( "intersect,intersection,overlap" ).split( ',' );
42}
43
44QString QgsIntersectionAlgorithm::group() const
45{
46 return QObject::tr( "Vector overlay" );
47}
48
49QString QgsIntersectionAlgorithm::groupId() const
50{
51 return u"vectoroverlay"_s;
52}
53
54QString QgsIntersectionAlgorithm::shortHelpString() const
55{
56 return QObject::tr(
57 "This algorithm extracts the overlapping portions of features in the Input and Overlay layers. "
58 "Features in the output Intersection layer are assigned the attributes of the overlapping features "
59 "from both the Input and Overlay layers."
60 );
61}
62
63QString QgsIntersectionAlgorithm::shortDescription() const
64{
65 return QObject::tr( "Extracts overlapping portions of features between two layers." );
66}
67
68Qgis::ProcessingAlgorithmDocumentationFlags QgsIntersectionAlgorithm::documentationFlags() const
69{
71}
72
73QgsProcessingAlgorithm *QgsIntersectionAlgorithm::createInstance() const
74{
75 return new QgsIntersectionAlgorithm();
76}
77
78void QgsIntersectionAlgorithm::initAlgorithm( const QVariantMap & )
79{
80 addParameter( new QgsProcessingParameterFeatureSource( u"INPUT"_s, QObject::tr( "Input layer" ) ) );
81 addParameter( new QgsProcessingParameterFeatureSource( u"OVERLAY"_s, QObject::tr( "Overlay layer" ) ) );
82
83 addParameter(
84 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 )
85 );
86 addParameter(
87 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 )
88 );
89
90 auto prefix = std::make_unique<QgsProcessingParameterString>( u"OVERLAY_FIELDS_PREFIX"_s, QObject::tr( "Overlay fields prefix" ), QString(), false, true );
91 prefix->setFlags( prefix->flags() | Qgis::ProcessingParameterFlag::Advanced );
92 addParameter( prefix.release() );
93
94 addParameter( new QgsProcessingParameterFeatureSink( u"OUTPUT"_s, QObject::tr( "Intersection" ) ) );
95
96 auto gridSize = std::make_unique<QgsProcessingParameterNumber>( u"GRID_SIZE"_s, QObject::tr( "Grid size" ), Qgis::ProcessingNumberParameterType::Double, QVariant(), true, 0 );
97 gridSize->setFlags( gridSize->flags() | Qgis::ProcessingParameterFlag::Advanced );
98 addParameter( gridSize.release() );
99}
100
101
102QVariantMap QgsIntersectionAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
103{
104 QGS_MARK_ALGORITHM_SOURCE
105
106 std::unique_ptr<QgsFeatureSource> sourceA( parameterAsSource( parameters, u"INPUT"_s, context ) );
107 if ( !sourceA )
108 throw QgsProcessingException( invalidSourceError( parameters, u"INPUT"_s ) );
109
110 std::unique_ptr<QgsFeatureSource> sourceB( parameterAsSource( parameters, u"OVERLAY"_s, context ) );
111 if ( !sourceB )
112 throw QgsProcessingException( invalidSourceError( parameters, u"OVERLAY"_s ) );
113
114 const Qgis::WkbType geomType = QgsWkbTypes::multiType( sourceA->wkbType() );
115
116 const QStringList fieldsA = parameterAsStrings( parameters, u"INPUT_FIELDS"_s, context );
117 const QStringList fieldsB = parameterAsStrings( parameters, u"OVERLAY_FIELDS"_s, context );
118
119 const QList<int> fieldIndicesA = QgsProcessingUtils::fieldNamesToIndices( fieldsA, sourceA->fields() );
120 const QList<int> fieldIndicesB = QgsProcessingUtils::fieldNamesToIndices( fieldsB, sourceB->fields() );
121
122 const QString overlayFieldsPrefix = parameterAsString( parameters, u"OVERLAY_FIELDS_PREFIX"_s, context );
123 const QgsFields outputFields
124 = QgsProcessingUtils::combineFields( QgsProcessingUtils::indicesToFields( fieldIndicesA, sourceA->fields() ), QgsProcessingUtils::indicesToFields( fieldIndicesB, sourceB->fields() ), overlayFieldsPrefix );
125
126 QString dest;
127 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u"OUTPUT"_s, context, dest, outputFields, geomType, sourceA->sourceCrs(), QgsFeatureSink::RegeneratePrimaryKey ) );
128 if ( !sink )
129 throw QgsProcessingException( invalidSinkError( parameters, u"OUTPUT"_s ) );
130
131 QVariantMap outputs;
132 outputs.insert( u"OUTPUT"_s, dest );
133
134 long count = 0;
135 const long total = sourceA->featureCount();
136
137 QgsGeometryParameters geometryParameters;
138 if ( parameters.value( u"GRID_SIZE"_s ).isValid() )
139 {
140 geometryParameters.setGridSize( parameterAsDouble( parameters, u"GRID_SIZE"_s, context ) );
141 }
142
143 QgsOverlayUtils::intersection( *sourceA, *sourceB, *sink, u"OUTPUT"_s, context, feedback, count, total, fieldIndicesA, fieldIndicesB, geometryParameters );
144
145 sink->finalize();
146 feedback->featureSinkFinalized( u"OUTPUT"_s );
147
148 return outputs;
149}
150
@ RegeneratesPrimaryKey
Algorithm always drops any existing primary keys or FID values and regenerates them in outputs.
Definition qgis.h:3838
QFlags< ProcessingAlgorithmDocumentationFlag > ProcessingAlgorithmDocumentationFlags
Flags describing algorithm behavior for documentation purposes.
Definition qgis.h:3849
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:4006
@ Double
Double/float values.
Definition qgis.h:4047
@ 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:45
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.
void featureSinkFinalized(const QString &output)
Reports that a feature sink has been finalized.
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.