QGIS API Documentation  3.2.0-Bonn (bc43194)
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 
18 #include "qgsgeometrycollection.h"
19 #include "qgsgeometryengine.h"
20 #include "qgsoverlayutils.h"
21 
23 
24 
25 QString QgsIntersectionAlgorithm::name() const
26 {
27  return QStringLiteral( "intersection" );
28 }
29 
30 QString QgsIntersectionAlgorithm::displayName() const
31 {
32  return QObject::tr( "Intersection" );
33 }
34 
35 QString QgsIntersectionAlgorithm::group() const
36 {
37  return QObject::tr( "Vector overlay" );
38 }
39 
40 QString QgsIntersectionAlgorithm::groupId() const
41 {
42  return QStringLiteral( "vectoroverlay" );
43 }
44 
45 QString QgsIntersectionAlgorithm::shortHelpString() const
46 {
47  return QObject::tr( "This algorithm extracts the overlapping portions of features in the Input and Overlay layers. Features in the Overlay layer are assigned the attributes of the overlapping features from both the Input and Overlay layers." )
48  + QStringLiteral( "\n\n" )
49  + QObject::tr( "Optionally, the rotation can occur around a preset point. If not set the rotation occurs around each feature's centroid." );
50 }
51 
52 QgsProcessingAlgorithm *QgsIntersectionAlgorithm::createInstance() const
53 {
54  return new QgsIntersectionAlgorithm();
55 }
56 
57 void QgsIntersectionAlgorithm::initAlgorithm( const QVariantMap & )
58 {
59  addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
60  addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "OVERLAY" ), QObject::tr( "Intersection layer" ) ) );
61 
62  addParameter( new QgsProcessingParameterField(
63  QStringLiteral( "INPUT_FIELDS" ),
64  QObject::tr( "Input fields to keep (leave empty to keep all fields)" ), QVariant(),
65  QStringLiteral( "INPUT" ), QgsProcessingParameterField::Any, true, true ) );
66  addParameter( new QgsProcessingParameterField(
67  QStringLiteral( "OVERLAY_FIELDS" ),
68  QObject::tr( "Intersect fields to keep (leave empty to keep all fields)" ), QVariant(),
69  QStringLiteral( "OVERLAY" ), QgsProcessingParameterField::Any, true, true ) );
70 
71  addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Intersection" ) ) );
72 
73 }
74 
75 
76 QVariantMap QgsIntersectionAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
77 {
78  std::unique_ptr< QgsFeatureSource > sourceA( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
79  if ( !sourceA )
80  throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
81 
82  std::unique_ptr< QgsFeatureSource > sourceB( parameterAsSource( parameters, QStringLiteral( "OVERLAY" ), context ) );
83  if ( !sourceB )
84  throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "OVERLAY" ) ) );
85 
86  QgsWkbTypes::Type geomType = QgsWkbTypes::multiType( sourceA->wkbType() );
87 
88  const QStringList fieldsA = parameterAsFields( parameters, QStringLiteral( "INPUT_FIELDS" ), context );
89  const QStringList fieldsB = parameterAsFields( parameters, QStringLiteral( "OVERLAY_FIELDS" ), context );
90 
91  QList<int> fieldIndicesA = QgsProcessingUtils::fieldNamesToIndices( fieldsA, sourceA->fields() );
92  QList<int> fieldIndicesB = QgsProcessingUtils::fieldNamesToIndices( fieldsB, sourceB->fields() );
93 
95  QgsProcessingUtils::indicesToFields( fieldIndicesA, sourceA->fields() ),
96  QgsProcessingUtils::indicesToFields( fieldIndicesB, sourceB->fields() ) );
97 
98  QString dest;
99  std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, outputFields, geomType, sourceA->sourceCrs() ) );
100  if ( !sink )
101  throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
102 
103  QVariantMap outputs;
104  outputs.insert( QStringLiteral( "OUTPUT" ), dest );
105 
106  int count = 0;
107  int total = sourceA->featureCount();
108 
109  QgsOverlayUtils::intersection( *sourceA.get(), *sourceB.get(), *sink.get(), context, feedback, count, total, fieldIndicesA, fieldIndicesB );
110 
111  return outputs;
112 }
113 
static Type multiType(Type type)
Returns the multi type for a WKB type.
Definition: qgswkbtypes.h:296
Base class for providing feedback from a processing algorithm.
A vector layer or feature source field parameter for processing algorithms.
Container of fields for a vector layer.
Definition: qgsfields.h:42
Abstract base class for processing algorithms.
A feature sink output for processing algorithms.
static QList< int > fieldNamesToIndices(const QStringList &fieldNames, const QgsFields &fields)
Returns a list of field indices parsed from the given list of field names.
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:67
static QgsFields combineFields(const QgsFields &fieldsA, const QgsFields &fieldsB)
Combines two field lists, avoiding duplicate field names (in a case-insensitive manner).
Custom exception class for processing related exceptions.
Definition: qgsexception.h:82
static QgsFields indicesToFields(const QList< int > &indices, const QgsFields &fields)
Returns a subset of fields based on the indices of desired fields.
An input feature source (such as vector layers) parameter for processing algorithms.
Contains information about the context in which a processing algorithm is executed.