Skip to content

Commit

Permalink
Distance within for virtual provider
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 5, 2021
1 parent 2cab3bb commit 12de6e8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
52 changes: 39 additions & 13 deletions src/providers/virtual/qgsvirtuallayerfeatureiterator.cpp
Expand Up @@ -58,6 +58,23 @@ QgsVirtualLayerFeatureIterator::QgsVirtualLayerFeatureIterator( QgsVirtualLayerF
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();
}
break;
}

try
{
QString tableName = mSource->mTableName;
Expand Down Expand Up @@ -118,8 +135,8 @@ QgsVirtualLayerFeatureIterator::QgsVirtualLayerFeatureIterator( QgsVirtualLayerF
else // never return a feature if the id is negative
offset = QStringLiteral( " LIMIT 0" );
}
if ( !mFilterRect.isNull() &&
mRequest.flags() & QgsFeatureRequest::ExactIntersect )
if ( !mFilterRect.isNull() && mRequest.spatialFilterType() == Qgis::SpatialFilterType::BoundingBox
&& mRequest.flags() & QgsFeatureRequest::ExactIntersect )
{
// if an exact intersection is requested, prepare the geometry to intersect
QgsGeometry rectGeom = QgsGeometry::fromRect( mFilterRect );
Expand Down Expand Up @@ -264,14 +281,14 @@ bool QgsVirtualLayerFeatureIterator::fetchFeature( QgsFeature &feature )
return false;
}


bool skipFeature = false;
do
{
if ( mQuery->step() != SQLITE_ROW )
{
return false;
}
skipFeature = false;

feature.setFields( mSource->mFields, /* init */ true );

Expand Down Expand Up @@ -305,7 +322,7 @@ bool QgsVirtualLayerFeatureIterator::fetchFeature( QgsFeature &feature )
default:
feature.setAttribute( idx, mQuery->columnText( i + 1 ) );
break;
};
}
i++;
}
if ( n > mAttributes.size() + 1 )
Expand All @@ -327,19 +344,28 @@ bool QgsVirtualLayerFeatureIterator::fetchFeature( QgsFeature &feature )

// if the FilterRect has not been applied on the query
// apply it here by skipping features until they intersect
if ( mSource->mDefinition.uid().isNull() && feature.hasGeometry() && mSource->mDefinition.hasDefinedGeometry() && !mFilterRect.isNull() )
if ( mSource->mDefinition.uid().isNull() && feature.hasGeometry() && mSource->mDefinition.hasDefinedGeometry() )
{
if ( mRequest.flags() & QgsFeatureRequest::ExactIntersect )
if ( !mFilterRect.isNull() )
{
// using exact test when checking for intersection
skipFeature = !mRectEngine->intersects( feature.geometry().constGet() );
}
else
{
// check just bounding box against rect when not using intersection
skipFeature = !feature.geometry().boundingBox().intersects( mFilterRect );
if ( mRequest.spatialFilterType() == Qgis::SpatialFilterType::BoundingBox && mRequest.flags() & QgsFeatureRequest::ExactIntersect )
{
// using exact test when checking for intersection
skipFeature = !mRectEngine->intersects( feature.geometry().constGet() );
}
else
{
// check just bounding box against rect when not using intersection
skipFeature = !feature.geometry().boundingBox().intersects( mFilterRect );
}
}
}

if ( !skipFeature && mDistanceWithinEngine )
{
if ( mDistanceWithinEngine->distance( feature.geometry().constGet() ) > mRequest.distanceWithin() )
skipFeature = true;
}
}
while ( skipFeature );

Expand Down
3 changes: 2 additions & 1 deletion src/providers/virtual/qgsvirtuallayerfeatureiterator.h
Expand Up @@ -74,7 +74,8 @@ class QgsVirtualLayerFeatureIterator final: public QgsAbstractFeatureIteratorFro
QgsFeatureId mFid = 0;
QgsCoordinateTransform mTransform;
QgsRectangle mFilterRect;

QgsGeometry mDistanceWithinGeom;
std::unique_ptr< QgsGeometryEngine > mDistanceWithinEngine;
std::unique_ptr< QgsGeometryEngine > mRectEngine;
};

Expand Down

0 comments on commit 12de6e8

Please sign in to comment.