Skip to content

Commit

Permalink
Implement distance within search for vector layer iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 5, 2021
1 parent 8ee93ef commit f69ff27
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
Expand Up @@ -158,6 +158,7 @@ Setup the simplification of geometries to fetch using the specified simplify met




private:
QgsVectorLayerFeatureIterator( const QgsVectorLayerFeatureIterator &rhs );
};
Expand Down
27 changes: 27 additions & 0 deletions src/core/vector/qgsvectorlayerfeatureiterator.cpp
Expand Up @@ -27,6 +27,7 @@
#include "qgsmessagelog.h"
#include "qgsexception.h"
#include "qgsexpressioncontextutils.h"
#include "qgsgeometryengine.h"

#include <QThreadStorage>
#include <QStack>
Expand Down Expand Up @@ -137,6 +138,26 @@ QgsVectorLayerFeatureIterator::QgsVectorLayerFeatureIterator( QgsVectorLayerFeat
close();
return;
}


// prepare spatial filter geometries for optimal speed
switch ( mRequest.spatialFilterType() )
{
case Qgis::SpatialFilterType::NoFilter:
case Qgis::SpatialFilterType::BoundingBox:
break;

case Qgis::SpatialFilterType::DistanceWithin:
if ( !mRequest.referenceGeometry().isEmpty() )
{
mDistanceWithinGeom = mRequest.referenceGeometry();
mDistanceWithinEngine.reset( QgsGeometry::createGeometryEngine( mDistanceWithinGeom.constGet() ) );
mDistanceWithinEngine->prepareGeometry();
mDistanceWithin = mRequest.distanceWithin();
}
break;
}

if ( !mFilterRect.isNull() )
{
// update request to be the unprojected filter rect
Expand Down Expand Up @@ -879,6 +900,12 @@ bool QgsVectorLayerFeatureIterator::postProcessFeature( QgsFeature &feature )
bool result = checkGeometryValidity( feature );
if ( result )
geometryToDestinationCrs( feature, mTransform );

if ( result && mDistanceWithinEngine && feature.hasGeometry() )
{
result = mDistanceWithinEngine->distance( feature.geometry().constGet() ) <= mDistanceWithin;
}

return result;
}

Expand Down
4 changes: 4 additions & 0 deletions src/core/vector/qgsvectorlayerfeatureiterator.h
Expand Up @@ -270,6 +270,10 @@ class CORE_EXPORT QgsVectorLayerFeatureIterator : public QgsAbstractFeatureItera
QgsRectangle mFilterRect;
QgsCoordinateTransform mTransform;

QgsGeometry mDistanceWithinGeom;
std::unique_ptr< QgsGeometryEngine > mDistanceWithinEngine;
double mDistanceWithin = 0;

// only related to editing
QSet<QgsFeatureId> mFetchConsidered;
QgsGeometryMap::ConstIterator mFetchChangedGeomIt;
Expand Down

0 comments on commit f69ff27

Please sign in to comment.