LCOV - code coverage report
Current view: top level - analysis/vector/geometry_checker - qgsgeometrycontainedcheck.cpp (source / functions) Hit Total Coverage
Test: coverage.info.cleaned Lines: 50 60 83.3 %
Date: 2021-04-10 08:29:14 Functions: 0 0 -
Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : /***************************************************************************
       2                 :            :     qgsgeometrycontainedcheck.cpp
       3                 :            :     ---------------------
       4                 :            :     begin                : September 2015
       5                 :            :     copyright            : (C) 2014 by Sandro Mani / Sourcepole AG
       6                 :            :     email                : smani at sourcepole dot ch
       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 "qgsgeometrycheckcontext.h"
      17                 :            : #include "qgsgeometryengine.h"
      18                 :            : #include "qgsgeometrycontainedcheck.h"
      19                 :            : #include "qgsfeaturepool.h"
      20                 :            : #include "qgsvectorlayer.h"
      21                 :            : 
      22                 :            : 
      23                 :          1 : void QgsGeometryContainedCheck::collectErrors( const QMap<QString, QgsFeaturePool *> &featurePools, QList<QgsGeometryCheckError *> &errors, QStringList &messages, QgsFeedback *feedback, const LayerFeatureIds &ids ) const
      24                 :            : {
      25                 :          1 :   QMap<QString, QgsFeatureIds> featureIds = ids.isEmpty() ? allLayerFeatureIds( featurePools ) : ids.toMap();
      26                 :          1 :   QgsGeometryCheckerUtils::LayerFeatures layerFeaturesA( featurePools, featureIds, compatibleGeometryTypes(), feedback, mContext );
      27                 :         42 :   for ( const QgsGeometryCheckerUtils::LayerFeature &layerFeatureA : layerFeaturesA )
      28                 :            :   {
      29                 :         41 :     QgsRectangle bboxA = layerFeatureA.geometry().boundingBox();
      30                 :         41 :     std::unique_ptr< QgsGeometryEngine > geomEngineA = QgsGeometryCheckerUtils::createGeomEngine( layerFeatureA.geometry().constGet(), mContext->tolerance );
      31                 :         41 :     if ( !geomEngineA->isValid() )
      32                 :            :     {
      33                 :          2 :       messages.append( tr( "Contained check failed for (%1): the geometry is invalid" ).arg( layerFeatureA.id() ) );
      34                 :          2 :       continue;
      35                 :            :     }
      36                 :         39 :     QgsGeometryCheckerUtils::LayerFeatures layerFeaturesB( featurePools, featureIds.keys(), bboxA, compatibleGeometryTypes(), mContext );
      37                 :        183 :     for ( const QgsGeometryCheckerUtils::LayerFeature &layerFeatureB : layerFeaturesB )
      38                 :            :     {
      39                 :        144 :       if ( layerFeatureA == layerFeatureB )
      40                 :            :       {
      41                 :         39 :         continue;
      42                 :            :       }
      43                 :        105 :       std::unique_ptr< QgsGeometryEngine > geomEngineB = QgsGeometryCheckerUtils::createGeomEngine( layerFeatureB.geometry().constGet(), mContext->tolerance );
      44                 :        105 :       if ( !geomEngineB->isValid() )
      45                 :            :       {
      46                 :          5 :         messages.append( tr( "Contained check failed for (%1): the geometry is invalid" ).arg( layerFeatureB.id() ) );
      47                 :          5 :         continue;
      48                 :            :       }
      49                 :        100 :       QString errMsg;
      50                 :            :       // If A contains B and B contains A, it would mean that the geometries are identical, which is covered by the duplicate check
      51                 :        104 :       if ( geomEngineA->contains( layerFeatureB.geometry().constGet(), &errMsg ) && !geomEngineB->contains( layerFeatureA.geometry().constGet(), &errMsg ) && errMsg.isEmpty() )
      52                 :            :       {
      53                 :          4 :         errors.append( new QgsGeometryContainedCheckError( this, layerFeatureB, layerFeatureB.geometry().constGet()->centroid(), layerFeatureA ) );
      54                 :          4 :       }
      55                 :         96 :       else if ( !errMsg.isEmpty() )
      56                 :            :       {
      57                 :          0 :         messages.append( tr( "Contained check failed for (%1, %2): %3" ).arg( layerFeatureB.id(), layerFeatureA.id(), errMsg ) );
      58                 :          0 :       }
      59                 :        105 :     }
      60                 :         41 :   }
      61                 :          1 : }
      62                 :            : 
      63                 :          1 : void QgsGeometryContainedCheck::fixError( const QMap<QString, QgsFeaturePool *> &featurePools, QgsGeometryCheckError *error, int method, const QMap<QString, int> & /*mergeAttributeIndices*/, Changes &changes ) const
      64                 :            : {
      65                 :          1 :   QgsGeometryContainedCheckError *containerError = static_cast<QgsGeometryContainedCheckError *>( error );
      66                 :          1 :   QgsFeaturePool *featurePoolA = featurePools[ error->layerId() ];
      67                 :          1 :   QgsFeaturePool *featurePoolB = featurePools[ containerError->containingFeature().first ];
      68                 :            : 
      69                 :          1 :   QgsFeature featureA;
      70                 :          1 :   QgsFeature featureB;
      71                 :          2 :   if ( !featurePoolA->getFeature( error->featureId(), featureA ) ||
      72                 :          1 :        !featurePoolB->getFeature( containerError->containingFeature().second, featureB ) )
      73                 :            :   {
      74                 :          0 :     error->setObsolete();
      75                 :          0 :     return;
      76                 :            :   }
      77                 :            : 
      78                 :            :   // Check if error still applies
      79                 :          1 :   QgsGeometryCheckerUtils::LayerFeature layerFeatureA( featurePoolA, featureA, mContext, true );
      80                 :          1 :   QgsGeometryCheckerUtils::LayerFeature layerFeatureB( featurePoolB, featureB, mContext, true );
      81                 :            : 
      82                 :          1 :   std::unique_ptr< QgsGeometryEngine > geomEngineA = QgsGeometryCheckerUtils::createGeomEngine( layerFeatureA.geometry().constGet(), mContext->tolerance );
      83                 :          1 :   std::unique_ptr< QgsGeometryEngine > geomEngineB = QgsGeometryCheckerUtils::createGeomEngine( layerFeatureB.geometry().constGet(), mContext->tolerance );
      84                 :            : 
      85                 :          1 :   if ( !( geomEngineB->contains( layerFeatureA.geometry().constGet() ) && !geomEngineA->contains( layerFeatureB.geometry().constGet() ) ) )
      86                 :            :   {
      87                 :          0 :     error->setObsolete();
      88                 :          0 :     return;
      89                 :            :   }
      90                 :            : 
      91                 :            :   // Fix error
      92                 :          1 :   if ( method == NoChange )
      93                 :            :   {
      94                 :          0 :     error->setFixed( method );
      95                 :          0 :   }
      96                 :          1 :   else if ( method == Delete )
      97                 :            :   {
      98                 :          1 :     changes[error->layerId()][featureA.id()].append( Change( ChangeFeature, ChangeRemoved ) );
      99                 :          1 :     featurePoolA->deleteFeature( featureA.id() );
     100                 :          1 :     error->setFixed( method );
     101                 :          1 :   }
     102                 :            :   else
     103                 :            :   {
     104                 :          0 :     error->setFixFailed( tr( "Unknown method" ) );
     105                 :            :   }
     106                 :          1 : }
     107                 :            : 
     108                 :          1 : QStringList QgsGeometryContainedCheck::resolutionMethods() const
     109                 :            : {
     110                 :          2 :   static QStringList methods = QStringList()
     111                 :          1 :                                << tr( "Delete feature" )
     112                 :          1 :                                << tr( "No action" );
     113                 :          1 :   return methods;
     114                 :          0 : }

Generated by: LCOV version 1.14