|
| 1 | +/*************************************************************************** |
| 2 | + * qgsgeometryselfcontactcheck.cpp * |
| 3 | + * ------------------- * |
| 4 | + * copyright : (C) 2017 by Sandro Mani / Sourcepole AG * |
| 5 | + * email : smani@sourcepole.ch * |
| 6 | + ***************************************************************************/ |
| 7 | + |
| 8 | +#include "qgsgeometryselfcontactcheck.h" |
| 9 | +#include "qgsgeometryutils.h" |
| 10 | +#include "../utils/qgsfeaturepool.h" |
| 11 | + |
| 12 | +void QgsGeometrySelfContactCheck::collectErrors( QList<QgsGeometryCheckError *> &errors, QStringList &/*messages*/, QAtomicInt *progressCounter, const QgsFeatureIds &ids ) const |
| 13 | +{ |
| 14 | + const QgsFeatureIds &featureIds = ids.isEmpty() ? mFeaturePool->getFeatureIds() : ids; |
| 15 | + double tolerance = QgsGeometryCheckPrecision::tolerance(); |
| 16 | + foreach ( const QgsFeatureId &featureid, featureIds ) |
| 17 | + { |
| 18 | + if ( progressCounter ) progressCounter->fetchAndAddRelaxed( 1 ); |
| 19 | + QgsFeature feature; |
| 20 | + if ( !mFeaturePool->get( featureid, feature ) ) |
| 21 | + { |
| 22 | + continue; |
| 23 | + } |
| 24 | + QgsAbstractGeometry *geom = feature.geometry().geometry(); |
| 25 | + |
| 26 | + for ( int iPart = 0, nParts = geom->partCount(); iPart < nParts; ++iPart ) |
| 27 | + { |
| 28 | + for ( int iRing = 0, nRings = geom->ringCount( iPart ); iRing < nRings; ++iRing ) |
| 29 | + { |
| 30 | + // Test for self-contacts |
| 31 | + int n = geom->vertexCount( iPart, iRing ); |
| 32 | + bool isClosed = geom->vertexAt( QgsVertexId( iPart, iRing, 0 ) ) == geom->vertexAt( QgsVertexId( iPart, iRing, n - 1 ) ); |
| 33 | + |
| 34 | + // Geometry ring without duplicate nodes |
| 35 | + QVector<int> vtxMap; |
| 36 | + QVector<QgsPointV2> ring; |
| 37 | + vtxMap.append( 0 ); |
| 38 | + ring.append( geom->vertexAt( QgsVertexId( iPart, iRing, 0 ) ) ); |
| 39 | + for ( int i = 1; i < n; ++i ) |
| 40 | + { |
| 41 | + QgsPointV2 p = geom->vertexAt( QgsVertexId( iPart, iRing, i ) ); |
| 42 | + if ( QgsGeometryUtils::sqrDistance2D( p, ring.last() ) > tolerance * tolerance ) |
| 43 | + { |
| 44 | + vtxMap.append( i ); |
| 45 | + ring.append( p ); |
| 46 | + } |
| 47 | + } |
| 48 | + while ( QgsGeometryUtils::sqrDistance2D( ring.front(), ring.back() ) < tolerance * tolerance ) |
| 49 | + { |
| 50 | + vtxMap.pop_back(); |
| 51 | + ring.pop_back(); |
| 52 | + } |
| 53 | + if ( isClosed ) |
| 54 | + { |
| 55 | + vtxMap.append( n - 1 ); |
| 56 | + ring.append( ring.front() ); |
| 57 | + } |
| 58 | + n = ring.size(); |
| 59 | + |
| 60 | + // For each vertex, check whether it lies on a segment |
| 61 | + for ( int iVert = 0, nVerts = n - isClosed; iVert < nVerts; ++iVert ) |
| 62 | + { |
| 63 | + const QgsPointV2 &p = ring[iVert]; |
| 64 | + for ( int i = 0, j = 1; j < n; i = j++ ) |
| 65 | + { |
| 66 | + if ( iVert == i || iVert == j || ( isClosed && iVert == 0 && j == n - 1 ) ) |
| 67 | + { |
| 68 | + continue; |
| 69 | + } |
| 70 | + const QgsPointV2 &si = ring[i]; |
| 71 | + const QgsPointV2 &sj = ring[j]; |
| 72 | + QgsPointV2 q = QgsGeometryUtils::projPointOnSegment( p, si, sj ); |
| 73 | + if ( QgsGeometryUtils::sqrDistance2D( p, q ) < tolerance * tolerance ) |
| 74 | + { |
| 75 | + errors.append( new QgsGeometryCheckError( this, featureid, p, QgsVertexId( iPart, iRing, vtxMap[iVert] ) ) ); |
| 76 | + break; // No need to report same contact on different segments multiple times |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +void QgsGeometrySelfContactCheck::fixError( QgsGeometryCheckError *error, int method, int /*mergeAttributeIndex*/, Changes & /*changes*/ ) const |
| 86 | +{ |
| 87 | + if ( method == NoChange ) |
| 88 | + { |
| 89 | + error->setFixed( method ); |
| 90 | + } |
| 91 | + else |
| 92 | + { |
| 93 | + error->setFixFailed( tr( "Unknown method" ) ); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +QStringList QgsGeometrySelfContactCheck::getResolutionMethods() const |
| 98 | +{ |
| 99 | + static QStringList methods = QStringList() << tr( "No action" ); |
| 100 | + return methods; |
| 101 | +} |
0 commit comments