Skip to content

Commit

Permalink
Merge pull request #4692 from nyalldawson/prepare
Browse files Browse the repository at this point in the history
Make more use of prepared geometries for intersection tests
  • Loading branch information
nyalldawson authored Jun 7, 2017
2 parents 648ff29 + be501a0 commit 1bbfd5b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/core/providers/memory/qgsmemoryfeatureiterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "qgsmemoryprovider.h"

#include "qgsgeometry.h"
#include "qgsgeometryengine.h"
#include "qgslogger.h"
#include "qgsspatialindex.h"
#include "qgsmessagelog.h"
Expand All @@ -35,6 +36,8 @@ QgsMemoryFeatureIterator::QgsMemoryFeatureIterator( QgsMemoryFeatureSource *sour
if ( !mRequest.filterRect().isNull() && mRequest.flags() & QgsFeatureRequest::ExactIntersect )
{
mSelectRectGeom = QgsGeometry::fromRect( request.filterRect() );
mSelectRectEngine.reset( QgsGeometry::createGeometryEngine( mSelectRectGeom.geometry() ) );
mSelectRectEngine->prepareGeometry();
}

// if there's spatial index, use it!
Expand Down Expand Up @@ -92,7 +95,7 @@ bool QgsMemoryFeatureIterator::nextFeatureUsingList( QgsFeature &feature )
if ( !mRequest.filterRect().isNull() && mRequest.flags() & QgsFeatureRequest::ExactIntersect )
{
// do exact check in case we're doing intersection
if ( mSource->mFeatures.value( *mFeatureIdListIterator ).hasGeometry() && mSource->mFeatures.value( *mFeatureIdListIterator ).geometry().intersects( mSelectRectGeom ) )
if ( mSource->mFeatures.value( *mFeatureIdListIterator ).hasGeometry() && mSelectRectEngine->intersects( *mSource->mFeatures.value( *mFeatureIdListIterator ).geometry().geometry() ) )
hasFeature = true;
}
else
Expand Down Expand Up @@ -144,7 +147,7 @@ bool QgsMemoryFeatureIterator::nextFeatureTraverseAll( QgsFeature &feature )
if ( mRequest.flags() & QgsFeatureRequest::ExactIntersect )
{
// using exact test when checking for intersection
if ( mSelectIterator->hasGeometry() && mSelectIterator->geometry().intersects( mSelectRectGeom ) )
if ( mSelectIterator->hasGeometry() && mSelectRectEngine->intersects( *mSelectIterator->geometry().geometry() ) )
hasFeature = true;
}
else
Expand Down
1 change: 1 addition & 0 deletions src/core/providers/memory/qgsmemoryfeatureiterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class QgsMemoryFeatureIterator : public QgsAbstractFeatureIteratorFromSource<Qgs
bool nextFeatureTraverseAll( QgsFeature &feature );

QgsGeometry mSelectRectGeom;
std::unique_ptr< QgsGeometryEngine > mSelectRectEngine;
QgsFeatureMap::const_iterator mSelectIterator;
bool mUsingFeatureIdList = false;
QList<QgsFeatureId> mFeatureIdList;
Expand Down
8 changes: 6 additions & 2 deletions src/core/qgsmaphittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "qgsvectorlayer.h"
#include "qgssymbollayerutils.h"
#include "qgsgeometry.h"
#include "qgsgeometryengine.h"
#include "qgscrscache.h"

QgsMapHitTest::QgsMapHitTest( const QgsMapSettings &settings, const QgsGeometry &polygon, const LayerFilterExpression &layerFilterExpression )
Expand Down Expand Up @@ -121,6 +122,7 @@ void QgsMapHitTest::runHitTestLayer( QgsVectorLayer *vl, SymbolSet &usedSymbols,

QgsFeature f;
QgsFeatureRequest request;
std::unique_ptr< QgsGeometryEngine > polygonEngine;
if ( !mOnlyExpressions )
{
if ( mPolygon.isNull() )
Expand All @@ -131,6 +133,8 @@ void QgsMapHitTest::runHitTestLayer( QgsVectorLayer *vl, SymbolSet &usedSymbols,
else
{
request.setFilterRect( transformedPolygon.boundingBox() );
polygonEngine.reset( QgsGeometry::createGeometryEngine( transformedPolygon.geometry() ) );
polygonEngine->prepareGeometry();
}
}
QgsFeatureIterator fi = vl->getFeatures( request );
Expand All @@ -149,9 +153,9 @@ void QgsMapHitTest::runHitTestLayer( QgsVectorLayer *vl, SymbolSet &usedSymbols,
{
context.expressionContext().setFeature( f );
// filter out elements outside of the polygon
if ( !mOnlyExpressions && !mPolygon.isNull() )
if ( f.geometry() && polygonEngine )
{
if ( !transformedPolygon.intersects( f.geometry() ) )
if ( !polygonEngine->intersects( *f.geometry().geometry() ) )
{
continue;
}
Expand Down

0 comments on commit 1bbfd5b

Please sign in to comment.