|
| 1 | +/*************************************************************************** |
| 2 | + qgsgeometrymissingvertexcheck.cpp |
| 3 | + --------------------- |
| 4 | + begin : September 2018 |
| 5 | + copyright : (C) 2018 Matthias Kuhn |
| 6 | + email : matthias@opengis.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 "qgsgeometrymissingvertexcheck.h" |
| 17 | + |
| 18 | +#include "qgsfeaturepool.h" |
| 19 | +#include "qgsgeometrycollection.h" |
| 20 | +#include "qgsmultipolygon.h" |
| 21 | +#include "qgscurvepolygon.h" |
| 22 | +#include "qgscurve.h" |
| 23 | +#include "qgslinestring.h" |
| 24 | +#include "qgsgeometryengine.h" |
| 25 | +#include "qgsgeometryutils.h" |
| 26 | + |
| 27 | +void QgsGeometryMissingVertexCheck::collectErrors( QList<QgsGeometryCheckError *> &errors, QStringList &messages, QAtomicInt *progressCounter, const QMap<QString, QgsFeatureIds> &ids ) const |
| 28 | +{ |
| 29 | + Q_UNUSED( messages ); |
| 30 | + if ( progressCounter ) |
| 31 | + progressCounter->fetchAndAddRelaxed( 1 ); |
| 32 | + |
| 33 | + QMap<QString, QgsFeatureIds> featureIds = ids.isEmpty() ? allLayerFeatureIds() : ids; |
| 34 | + |
| 35 | + QgsFeaturePool *featurePool = mContext->featurePools.value( featureIds.firstKey() ); |
| 36 | + |
| 37 | + const QgsGeometryCheckerUtils::LayerFeatures layerFeatures( mContext->featurePools, featureIds, mCompatibleGeometryTypes, nullptr, mContext, true ); |
| 38 | + |
| 39 | + for ( const QgsGeometryCheckerUtils::LayerFeature &layerFeature : layerFeatures ) |
| 40 | + { |
| 41 | + const QgsAbstractGeometry *geom = layerFeature.geometry().constGet(); |
| 42 | + |
| 43 | + if ( QgsCurvePolygon *polygon = qgsgeometry_cast<QgsCurvePolygon *>( geom ) ) |
| 44 | + { |
| 45 | + processPolygon( polygon, featurePool, errors, layerFeature ); |
| 46 | + } |
| 47 | + else if ( QgsGeometryCollection *collection = qgsgeometry_cast<QgsGeometryCollection *>( geom ) ) |
| 48 | + { |
| 49 | + const int numGeometries = collection->numGeometries(); |
| 50 | + for ( int i = 0; i < numGeometries; ++i ) |
| 51 | + { |
| 52 | + if ( QgsCurvePolygon *polygon = qgsgeometry_cast<QgsCurvePolygon *>( collection->geometryN( i ) ) ) |
| 53 | + { |
| 54 | + processPolygon( polygon, featurePool, errors, layerFeature ); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +void QgsGeometryMissingVertexCheck::fixError( QgsGeometryCheckError *error, int method, const QMap<QString, int> &mergeAttributeIndices, Changes &changes ) const |
| 62 | +{ |
| 63 | + Q_UNUSED( mergeAttributeIndices ); |
| 64 | + Q_UNUSED( changes ); |
| 65 | + if ( method == NoChange ) |
| 66 | + { |
| 67 | + error->setFixed( method ); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +QStringList QgsGeometryMissingVertexCheck::resolutionMethods() const |
| 72 | +{ |
| 73 | + static QStringList methods = QStringList() << tr( "No action" ); |
| 74 | + return methods; |
| 75 | +} |
| 76 | + |
| 77 | +void QgsGeometryMissingVertexCheck::processPolygon( const QgsCurvePolygon *polygon, QgsFeaturePool *featurePool, QList<QgsGeometryCheckError *> &errors, const QgsGeometryCheckerUtils::LayerFeature &layerFeature ) const |
| 78 | +{ |
| 79 | + const QgsFeature ¤tFeature = layerFeature.feature(); |
| 80 | + std::unique_ptr<QgsMultiPolygon> boundaries = qgis::make_unique<QgsMultiPolygon>(); |
| 81 | + |
| 82 | + std::unique_ptr< QgsGeometryEngine > geomEngine = QgsGeometryCheckerUtils::createGeomEngine( polygon->exteriorRing(), mContext->tolerance ); |
| 83 | + boundaries->addGeometry( geomEngine->buffer( mContext->tolerance, 5 ) ); |
| 84 | + |
| 85 | + const int numRings = polygon->numInteriorRings(); |
| 86 | + for ( int i = 0; i < numRings; ++i ) |
| 87 | + { |
| 88 | + geomEngine = QgsGeometryCheckerUtils::createGeomEngine( polygon->exteriorRing(), mContext->tolerance ); |
| 89 | + boundaries->addGeometry( geomEngine->buffer( mContext->tolerance, 5 ) ); |
| 90 | + } |
| 91 | + |
| 92 | + geomEngine = QgsGeometryCheckerUtils::createGeomEngine( boundaries.get(), mContext->tolerance ); |
| 93 | + geomEngine->prepareGeometry(); |
| 94 | + |
| 95 | + const QgsFeatureIds fids = featurePool->getIntersects( boundaries->boundingBox() ); |
| 96 | + |
| 97 | + QgsFeature compareFeature; |
| 98 | + for ( QgsFeatureId fid : fids ) |
| 99 | + { |
| 100 | + if ( fid == currentFeature.id() ) |
| 101 | + continue; |
| 102 | + |
| 103 | + if ( featurePool->getFeature( fid, compareFeature ) ) |
| 104 | + { |
| 105 | + QgsVertexIterator vertexIterator = compareFeature.geometry().vertices(); |
| 106 | + while ( vertexIterator.hasNext() ) |
| 107 | + { |
| 108 | + const QgsPoint &pt = vertexIterator.next(); |
| 109 | + if ( geomEngine->intersects( &pt ) ) |
| 110 | + { |
| 111 | + QgsVertexId vertexId; |
| 112 | + QgsPoint closestVertex = QgsGeometryUtils::closestVertex( *polygon, pt, vertexId ); |
| 113 | + |
| 114 | + if ( closestVertex.distance( pt ) > mContext->tolerance ) |
| 115 | + { |
| 116 | + bool alreadyReported = false; |
| 117 | + for ( QgsGeometryCheckError *error : qgis::as_const( errors ) ) |
| 118 | + { |
| 119 | + // Only list missing vertices once |
| 120 | + if ( error->featureId() == currentFeature.id() && error->location() == QgsPointXY( pt ) ) |
| 121 | + { |
| 122 | + alreadyReported = true; |
| 123 | + break; |
| 124 | + } |
| 125 | + } |
| 126 | + if ( !alreadyReported ) |
| 127 | + errors.append( new QgsGeometryCheckError( this, layerFeature, QgsPointXY( pt ) ) ); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments