-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Geometry checker] Add self-contact check
- Loading branch information
1 parent
9754590
commit cad9e46
Showing
5 changed files
with
178 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/plugins/geometry_checker/checks/qgsgeometryselfcontactcheck.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/*************************************************************************** | ||
* qgsgeometryselfcontactcheck.cpp * | ||
* ------------------- * | ||
* copyright : (C) 2017 by Sandro Mani / Sourcepole AG * | ||
* email : smani@sourcepole.ch * | ||
***************************************************************************/ | ||
|
||
#include "qgsgeometryselfcontactcheck.h" | ||
#include "qgsgeometryutils.h" | ||
#include "../utils/qgsfeaturepool.h" | ||
|
||
void QgsGeometrySelfContactCheck::collectErrors( QList<QgsGeometryCheckError *> &errors, QStringList &/*messages*/, QAtomicInt *progressCounter, const QgsFeatureIds &ids ) const | ||
{ | ||
const QgsFeatureIds &featureIds = ids.isEmpty() ? mFeaturePool->getFeatureIds() : ids; | ||
double tolerance = QgsGeometryCheckPrecision::tolerance(); | ||
foreach ( const QgsFeatureId &featureid, featureIds ) | ||
{ | ||
if ( progressCounter ) progressCounter->fetchAndAddRelaxed( 1 ); | ||
QgsFeature feature; | ||
if ( !mFeaturePool->get( featureid, feature ) ) | ||
{ | ||
continue; | ||
} | ||
QgsAbstractGeometry *geom = feature.geometry().geometry(); | ||
|
||
for ( int iPart = 0, nParts = geom->partCount(); iPart < nParts; ++iPart ) | ||
{ | ||
for ( int iRing = 0, nRings = geom->ringCount( iPart ); iRing < nRings; ++iRing ) | ||
{ | ||
// Test for self-contacts | ||
int n = geom->vertexCount( iPart, iRing ); | ||
bool isClosed = geom->vertexAt( QgsVertexId( iPart, iRing, 0 ) ) == geom->vertexAt( QgsVertexId( iPart, iRing, n - 1 ) ); | ||
|
||
// Geometry ring without duplicate nodes | ||
QVector<int> vtxMap; | ||
QVector<QgsPointV2> ring; | ||
vtxMap.append( 0 ); | ||
ring.append( geom->vertexAt( QgsVertexId( iPart, iRing, 0 ) ) ); | ||
for ( int i = 1; i < n; ++i ) | ||
{ | ||
QgsPointV2 p = geom->vertexAt( QgsVertexId( iPart, iRing, i ) ); | ||
if ( QgsGeometryUtils::sqrDistance2D( p, ring.last() ) > tolerance * tolerance ) | ||
{ | ||
vtxMap.append( i ); | ||
ring.append( p ); | ||
} | ||
} | ||
while ( QgsGeometryUtils::sqrDistance2D( ring.front(), ring.back() ) < tolerance * tolerance ) | ||
{ | ||
vtxMap.pop_back(); | ||
ring.pop_back(); | ||
} | ||
if ( isClosed ) | ||
{ | ||
vtxMap.append( n - 1 ); | ||
ring.append( ring.front() ); | ||
} | ||
n = ring.size(); | ||
|
||
// For each vertex, check whether it lies on a segment | ||
for ( int iVert = 0, nVerts = n - isClosed; iVert < nVerts; ++iVert ) | ||
{ | ||
const QgsPointV2 &p = ring[iVert]; | ||
for ( int i = 0, j = 1; j < n; i = j++ ) | ||
{ | ||
if ( iVert == i || iVert == j || ( isClosed && iVert == 0 && j == n - 1 ) ) | ||
{ | ||
continue; | ||
} | ||
const QgsPointV2 &si = ring[i]; | ||
const QgsPointV2 &sj = ring[j]; | ||
QgsPointV2 q = QgsGeometryUtils::projPointOnSegment( p, si, sj ); | ||
if ( QgsGeometryUtils::sqrDistance2D( p, q ) < tolerance * tolerance ) | ||
{ | ||
errors.append( new QgsGeometryCheckError( this, featureid, p, QgsVertexId( iPart, iRing, vtxMap[iVert] ) ) ); | ||
break; // No need to report same contact on different segments multiple times | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
void QgsGeometrySelfContactCheck::fixError( QgsGeometryCheckError *error, int method, int /*mergeAttributeIndex*/, Changes & /*changes*/ ) const | ||
{ | ||
if ( method == NoChange ) | ||
{ | ||
error->setFixed( method ); | ||
} | ||
else | ||
{ | ||
error->setFixFailed( tr( "Unknown method" ) ); | ||
} | ||
} | ||
|
||
QStringList QgsGeometrySelfContactCheck::getResolutionMethods() const | ||
{ | ||
static QStringList methods = QStringList() << tr( "No action" ); | ||
return methods; | ||
} |
29 changes: 29 additions & 0 deletions
29
src/plugins/geometry_checker/checks/qgsgeometryselfcontactcheck.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/*************************************************************************** | ||
* qgsgeometryselfcontactcheck.h * | ||
* ------------------- * | ||
* copyright : (C) 2017 by Sandro Mani / Sourcepole AG * | ||
* email : smani@sourcepole.ch * | ||
***************************************************************************/ | ||
|
||
#ifndef QGS_GEOMETRY_SELFCONTACT_CHECK_H | ||
#define QGS_GEOMETRY_SELFCONTACT_CHECK_H | ||
|
||
#include "qgsgeometrycheck.h" | ||
|
||
class QgsGeometrySelfContactCheck : public QgsGeometryCheck | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
QgsGeometrySelfContactCheck( QgsFeaturePool *featurePool ) | ||
: QgsGeometryCheck( FeatureNodeCheck, featurePool ) {} | ||
void collectErrors( QList<QgsGeometryCheckError *> &errors, QStringList &messages, QAtomicInt *progressCounter = 0, const QgsFeatureIds &ids = QgsFeatureIds() ) const; | ||
void fixError( QgsGeometryCheckError *error, int method, int, Changes & ) const; | ||
QStringList getResolutionMethods() const; | ||
QString errorDescription() const { return tr( "Self contact" ); } | ||
QString errorName() const { return "QgsGeometrySelfContactCheck"; } | ||
private: | ||
enum ResolutionMethod { NoChange }; | ||
}; | ||
|
||
#endif // QGS_GEOMETRY_SELFCONTACT_CHECK_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters