Skip to content

Commit

Permalink
Merge pull request #60114 from lbartoletti/spatialindex_rm_duplicate_…
Browse files Browse the repository at this point in the history
…code

chore(spatial index): Remove duplicate rectangleToRegion code
  • Loading branch information
rouault authored Jan 12, 2025
2 parents 38ec9ef + 90e4d2f commit f999386
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 26 deletions.
10 changes: 2 additions & 8 deletions src/core/mesh/qgsmeshspatialindex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "qgsmeshspatialindex.h"
#include "qgsrectangle.h"
#include "qgsspatialindexutils.h"
#include "qgslogger.h"
#include "qgsfeedback.h"

Expand Down Expand Up @@ -74,13 +75,6 @@ static Region edgeToRegion( const QgsMesh &mesh, int id, bool &ok )
return SpatialIndex::Region( pt1, pt2, 2 );
}

static Region rectToRegion( const QgsRectangle &rect )
{
double pt1[2] = { rect.xMinimum(), rect.yMinimum() };
double pt2[2] = { rect.xMaximum(), rect.yMaximum() };
return SpatialIndex::Region( pt1, pt2, 2 );
}

/**
* \ingroup core
* \class QgisMeshVisitor
Expand Down Expand Up @@ -376,7 +370,7 @@ QList<int> QgsMeshSpatialIndex::intersects( const QgsRectangle &rect ) const
QList<int> list;
QgisMeshVisitor visitor( list );

const SpatialIndex::Region r = rectToRegion( rect );
const SpatialIndex::Region r = QgsSpatialIndexUtils::rectangleToRegion( rect );

const QMutexLocker locker( &d->mMutex );
d->mRTree->intersectsWithQuery( r, visitor );
Expand Down
29 changes: 11 additions & 18 deletions src/core/qgspointlocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "qgscurvepolygon.h"
#include "qgsrendercontext.h"
#include "qgspointlocatorinittask.h"
#include "qgsspatialindexutils.h"
#include <spatialindex/SpatialIndex.h>

#include <QLinkedListIterator>
Expand All @@ -46,14 +47,6 @@ static SpatialIndex::Point point2point( const QgsPointXY &point )
}


static SpatialIndex::Region rect2region( const QgsRectangle &rect )
{
double pLow[2] = { rect.xMinimum(), rect.yMinimum() };
double pHigh[2] = { rect.xMaximum(), rect.yMaximum() };
return SpatialIndex::Region( pLow, pHigh, 2 );
}


// Ahh.... another magic number. Taken from QgsVectorLayer::snapToGeometry() call to closestSegmentWithContext().
// The default epsilon used for sqrDistToSegment (1e-8) is too high when working with lat/lon coordinates
// I still do not fully understand why the sqrDistToSegment() code uses epsilon and if the square distance
Expand Down Expand Up @@ -1128,7 +1121,7 @@ bool QgsPointLocator::rebuildIndex( int maxFeaturesToIndex )
const QgsRectangle bbox = f.geometry().boundingBox();
if ( bbox.isFinite() )
{
SpatialIndex::Region r( rect2region( bbox ) );
SpatialIndex::Region r( QgsSpatialIndexUtils::rectangleToRegion( bbox ) );
dataList << new RTree::Data( 0, nullptr, r, f.id() );

auto it = mGeoms.find( f.id() );
Expand Down Expand Up @@ -1262,7 +1255,7 @@ void QgsPointLocator::onFeatureAdded( QgsFeatureId fid )
const QgsRectangle bbox = f.geometry().boundingBox();
if ( bbox.isFinite() )
{
const SpatialIndex::Region r( rect2region( bbox ) );
const SpatialIndex::Region r( QgsSpatialIndexUtils::rectangleToRegion( bbox ) );
mRTree->insertData( 0, nullptr, r, f.id() );

auto it = mGeoms.find( f.id() );
Expand Down Expand Up @@ -1301,7 +1294,7 @@ void QgsPointLocator::onFeatureDeleted( QgsFeatureId fid )
auto it = mGeoms.find( fid );
if ( it != mGeoms.end() )
{
mRTree->deleteData( rect2region( ( *it )->boundingBox() ), fid );
mRTree->deleteData( QgsSpatialIndexUtils::rectangleToRegion( ( *it )->boundingBox() ), fid );
delete *it;
mGeoms.erase( it );
}
Expand Down Expand Up @@ -1335,7 +1328,7 @@ QgsPointLocator::Match QgsPointLocator::nearestVertex( const QgsPointXY &point,
Match m;
QgsPointLocator_VisitorNearestVertex visitor( this, m, point, filter );
const QgsRectangle rect( point.x() - tolerance, point.y() - tolerance, point.x() + tolerance, point.y() + tolerance );
mRTree->intersectsWithQuery( rect2region( rect ), visitor );
mRTree->intersectsWithQuery( QgsSpatialIndexUtils::rectangleToRegion( rect ), visitor );
if ( m.isValid() && m.distance() > tolerance )
return Match(); // make sure that only match strictly within the tolerance is returned
return m;
Expand All @@ -1350,7 +1343,7 @@ QgsPointLocator::Match QgsPointLocator::nearestCentroid( const QgsPointXY &point
QgsPointLocator_VisitorNearestCentroid visitor( this, m, point, filter );

const QgsRectangle rect( point.x() - tolerance, point.y() - tolerance, point.x() + tolerance, point.y() + tolerance );
mRTree->intersectsWithQuery( rect2region( rect ), visitor );
mRTree->intersectsWithQuery( QgsSpatialIndexUtils::rectangleToRegion( rect ), visitor );
if ( m.isValid() && m.distance() > tolerance )
return Match(); // make sure that only match strictly within the tolerance is returned
return m;
Expand All @@ -1365,7 +1358,7 @@ QgsPointLocator::Match QgsPointLocator::nearestMiddleOfSegment( const QgsPointXY
QgsPointLocator_VisitorNearestMiddleOfSegment visitor( this, m, point, filter );

const QgsRectangle rect( point.x() - tolerance, point.y() - tolerance, point.x() + tolerance, point.y() + tolerance );
mRTree->intersectsWithQuery( rect2region( rect ), visitor );
mRTree->intersectsWithQuery( QgsSpatialIndexUtils::rectangleToRegion( rect ), visitor );
if ( m.isValid() && m.distance() > tolerance )
return Match(); // make sure that only match strictly within the tolerance is returned
return m;
Expand All @@ -1380,7 +1373,7 @@ QgsPointLocator::Match QgsPointLocator::nearestLineEndpoints( const QgsPointXY &
QgsPointLocator_VisitorNearestLineEndpoint visitor( this, m, point, filter );

const QgsRectangle rect( point.x() - tolerance, point.y() - tolerance, point.x() + tolerance, point.y() + tolerance );
mRTree->intersectsWithQuery( rect2region( rect ), visitor );
mRTree->intersectsWithQuery( QgsSpatialIndexUtils::rectangleToRegion( rect ), visitor );
if ( m.isValid() && m.distance() > tolerance )
return Match(); // make sure that only match strictly within the tolerance is returned
return m;
Expand All @@ -1398,7 +1391,7 @@ QgsPointLocator::Match QgsPointLocator::nearestEdge( const QgsPointXY &point, do
Match m;
QgsPointLocator_VisitorNearestEdge visitor( this, m, point, filter );
const QgsRectangle rect( point.x() - tolerance, point.y() - tolerance, point.x() + tolerance, point.y() + tolerance );
mRTree->intersectsWithQuery( rect2region( rect ), visitor );
mRTree->intersectsWithQuery( QgsSpatialIndexUtils::rectangleToRegion( rect ), visitor );
if ( m.isValid() && m.distance() > tolerance )
return Match(); // make sure that only match strictly within the tolerance is returned
return m;
Expand Down Expand Up @@ -1445,7 +1438,7 @@ QgsPointLocator::MatchList QgsPointLocator::edgesInRect( const QgsRectangle &rec

MatchList lst;
QgsPointLocator_VisitorEdgesInRect visitor( this, lst, rect, filter );
mRTree->intersectsWithQuery( rect2region( rect ), visitor );
mRTree->intersectsWithQuery( QgsSpatialIndexUtils::rectangleToRegion( rect ), visitor );

return lst;
}
Expand All @@ -1463,7 +1456,7 @@ QgsPointLocator::MatchList QgsPointLocator::verticesInRect( const QgsRectangle &

MatchList lst;
QgsPointLocator_VisitorVerticesInRect visitor( this, lst, rect, filter );
mRTree->intersectsWithQuery( rect2region( rect ), visitor );
mRTree->intersectsWithQuery( QgsSpatialIndexUtils::rectangleToRegion( rect ), visitor );

return lst;
}
Expand Down

0 comments on commit f999386

Please sign in to comment.