Skip to content
Permalink
Browse files

[geometry fixer] PR review - spatial index

(cherry picked from commit 4d11d79)
  • Loading branch information
olivierdalang authored and nyalldawson committed Nov 20, 2020
1 parent 7eb610e commit e56c50d7d8bcd18ffcec839fbc5aad96a0728496
Showing with 20 additions and 27 deletions.
  1. +20 −27 src/analysis/vector/geometry_checker/qgsgeometrygapcheck.cpp
@@ -371,40 +371,33 @@ bool QgsGeometryGapCheck::mergeWithNeighbor( const QMap<QString, QgsFeaturePool
return false;
}

// Create an index of all neighbouring vertices
QgsSpatialIndex neighbourVerticesIndex( QgsSpatialIndex::Flag::FlagStoreFeatureGeometries );
int id = 0;
for ( const QgsFeature &neighbour : neighbours )
{
QgsVertexIterator vit = neighbour.geometry().vertices();
while ( vit.hasNext() )
{
QgsPoint pt = vit.next();
QgsFeature f;
f.setId( id ); // required for SpatialIndex to return the correct result
f.setGeometry( QgsGeometry( pt.clone() ) );
neighbourVerticesIndex.addFeature( f );
id++;
}
}

// Snap to the closest vertex
QgsPolyline snappedRing;
QgsVertexIterator iterator = errGeometry->vertices();
while ( iterator.hasNext() )
{
QgsPoint pt = iterator.next();
QgsVertexId id;

// We iterate through all neighbours points to find the closest one.
// Note that we cannot use a nearestNeighbour as it's not guaranteed to return the feature
// with the closest vertex (distance can be 0 if we are lying inside the feature, even if far
// from any vertex)
double nearestDist = -1;
QgsPoint closestPoint;
QListIterator<QgsFeature> neighboursIt( neighbours );
while ( neighboursIt.hasNext() )
{
QgsGeometry neighbourGeom = neighboursIt.next().geometry();
QgsVertexIterator neighbourIterator = neighbourGeom.vertices();
while ( neighbourIterator.hasNext() )
{
QgsPoint neighbourPt = neighbourIterator.next();
double dist = pt.distanceSquared( neighbourPt );
if ( nearestDist == -1 || dist < nearestDist )
{
nearestDist = dist;
closestPoint = neighbourPt;
}
}
}

if ( nearestDist != -1 )
QgsGeometry closestGeom = neighbourVerticesIndex.geometry( neighbourVerticesIndex.nearestNeighbor( QgsPointXY( pt ) ).first() );
if ( !closestGeom.isEmpty() )
{
snappedRing.append( closestPoint );
snappedRing.append( QgsPoint( closestGeom.vertexAt( 0 ) ) );
}
}

0 comments on commit e56c50d

Please sign in to comment.
You can’t perform that action at this time.