LCOV - code coverage report
Current view: top level - analysis/processing - qgsalgorithmintersection.cpp (source / functions) Hit Total Coverage
Test: coverage.info.cleaned Lines: 0 55 0.0 %
Date: 2021-03-26 12:19:53 Functions: 0 0 -
Branches: 0 0 -

           Branch data     Line data    Source code
       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                 :            : 
      16                 :            : #include "qgsalgorithmintersection.h"
      17                 :            : 
      18                 :            : #include "qgsgeometrycollection.h"
      19                 :            : #include "qgsgeometryengine.h"
      20                 :            : #include "qgsoverlayutils.h"
      21                 :            : 
      22                 :            : ///@cond PRIVATE
      23                 :            : 
      24                 :            : 
      25                 :          0 : QString QgsIntersectionAlgorithm::name() const
      26                 :            : {
      27                 :          0 :   return QStringLiteral( "intersection" );
      28                 :            : }
      29                 :            : 
      30                 :          0 : QString QgsIntersectionAlgorithm::displayName() const
      31                 :            : {
      32                 :          0 :   return QObject::tr( "Intersection" );
      33                 :            : }
      34                 :            : 
      35                 :          0 : QString QgsIntersectionAlgorithm::group() const
      36                 :            : {
      37                 :          0 :   return QObject::tr( "Vector overlay" );
      38                 :            : }
      39                 :            : 
      40                 :          0 : QString QgsIntersectionAlgorithm::groupId() const
      41                 :            : {
      42                 :          0 :   return QStringLiteral( "vectoroverlay" );
      43                 :            : }
      44                 :            : 
      45                 :          0 : QString QgsIntersectionAlgorithm::shortHelpString() const
      46                 :            : {
      47                 :          0 :   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                 :            : 
      52                 :          0 : QgsProcessingAlgorithm *QgsIntersectionAlgorithm::createInstance() const
      53                 :            : {
      54                 :          0 :   return new QgsIntersectionAlgorithm();
      55                 :            : }
      56                 :            : 
      57                 :          0 : void QgsIntersectionAlgorithm::initAlgorithm( const QVariantMap & )
      58                 :            : {
      59                 :          0 :   addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
      60                 :          0 :   addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "OVERLAY" ), QObject::tr( "Overlay layer" ) ) );
      61                 :            : 
      62                 :          0 :   addParameter( new QgsProcessingParameterField(
      63                 :          0 :                   QStringLiteral( "INPUT_FIELDS" ),
      64                 :          0 :                   QObject::tr( "Input fields to keep (leave empty to keep all fields)" ), QVariant(),
      65                 :          0 :                   QStringLiteral( "INPUT" ), QgsProcessingParameterField::Any, true, true ) );
      66                 :          0 :   addParameter( new QgsProcessingParameterField(
      67                 :          0 :                   QStringLiteral( "OVERLAY_FIELDS" ),
      68                 :          0 :                   QObject::tr( "Overlay fields to keep (leave empty to keep all fields)" ), QVariant(),
      69                 :          0 :                   QStringLiteral( "OVERLAY" ), QgsProcessingParameterField::Any, true, true ) );
      70                 :            : 
      71                 :          0 :   std::unique_ptr< QgsProcessingParameterString > prefix = std::make_unique< QgsProcessingParameterString >( QStringLiteral( "OVERLAY_FIELDS_PREFIX" ), QObject::tr( "Overlay fields prefix" ), QString(), false, true );
      72                 :          0 :   prefix->setFlags( prefix->flags() | QgsProcessingParameterDefinition::FlagAdvanced );
      73                 :          0 :   addParameter( prefix.release() );
      74                 :            : 
      75                 :          0 :   addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Intersection" ) ) );
      76                 :            : 
      77                 :          0 : }
      78                 :            : 
      79                 :            : 
      80                 :          0 : QVariantMap QgsIntersectionAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
      81                 :            : {
      82                 :          0 :   std::unique_ptr< QgsFeatureSource > sourceA( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
      83                 :          0 :   if ( !sourceA )
      84                 :          0 :     throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
      85                 :            : 
      86                 :          0 :   std::unique_ptr< QgsFeatureSource > sourceB( parameterAsSource( parameters, QStringLiteral( "OVERLAY" ), context ) );
      87                 :          0 :   if ( !sourceB )
      88                 :          0 :     throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "OVERLAY" ) ) );
      89                 :            : 
      90                 :          0 :   QgsWkbTypes::Type geomType = QgsWkbTypes::multiType( sourceA->wkbType() );
      91                 :            : 
      92                 :          0 :   const QStringList fieldsA = parameterAsFields( parameters, QStringLiteral( "INPUT_FIELDS" ), context );
      93                 :          0 :   const QStringList fieldsB = parameterAsFields( parameters, QStringLiteral( "OVERLAY_FIELDS" ), context );
      94                 :            : 
      95                 :          0 :   QList<int> fieldIndicesA = QgsProcessingUtils::fieldNamesToIndices( fieldsA, sourceA->fields() );
      96                 :          0 :   QList<int> fieldIndicesB = QgsProcessingUtils::fieldNamesToIndices( fieldsB, sourceB->fields() );
      97                 :            : 
      98                 :          0 :   QString overlayFieldsPrefix = parameterAsString( parameters, QStringLiteral( "OVERLAY_FIELDS_PREFIX" ), context );
      99                 :          0 :   QgsFields outputFields = QgsProcessingUtils::combineFields(
     100                 :          0 :                              QgsProcessingUtils::indicesToFields( fieldIndicesA, sourceA->fields() ),
     101                 :          0 :                              QgsProcessingUtils::indicesToFields( fieldIndicesB, sourceB->fields() ),
     102                 :            :                              overlayFieldsPrefix );
     103                 :            : 
     104                 :          0 :   QString dest;
     105                 :          0 :   std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, outputFields, geomType, sourceA->sourceCrs(), QgsFeatureSink::RegeneratePrimaryKey ) );
     106                 :          0 :   if ( !sink )
     107                 :          0 :     throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
     108                 :            : 
     109                 :          0 :   QVariantMap outputs;
     110                 :          0 :   outputs.insert( QStringLiteral( "OUTPUT" ), dest );
     111                 :            : 
     112                 :          0 :   int count = 0;
     113                 :          0 :   int total = sourceA->featureCount();
     114                 :            : 
     115                 :          0 :   QgsOverlayUtils::intersection( *sourceA, *sourceB, *sink, context, feedback, count, total, fieldIndicesA, fieldIndicesB );
     116                 :            : 
     117                 :          0 :   return outputs;
     118                 :          0 : }
     119                 :            : 
     120                 :            : ///@endcond

Generated by: LCOV version 1.14