Skip to content
Permalink
Browse files

Don't allow selection of invisible features due to temporal filters

Fixes #40818
  • Loading branch information
nyalldawson committed Jan 4, 2021
1 parent 7edb765 commit 67d57541eef0d48058b26c1915ae556c7706ad81
Showing with 49 additions and 2 deletions.
  1. +48 −2 src/app/qgsmaptoolselectutils.cpp
  2. +1 −0 src/app/qgsmaptoolselectutils.h
@@ -33,6 +33,7 @@ email : jpalmer at linz dot govt dot nz
#include "qgsproject.h"
#include "qgsexpressioncontextutils.h"
#include "qgsmessagelog.h"
#include "qgsvectorlayertemporalproperties.h"

#include <QMouseEvent>
#include <QApplication>
@@ -257,6 +258,17 @@ QgsFeatureIds QgsMapToolSelectUtils::getMatchingFeatures( QgsMapCanvas *canvas,
r->startRender( context, vlayer->fields() );
}

QString temporalFilter;
if ( canvas->mapSettings().isTemporal() )
{
if ( !vlayer->temporalProperties()->isVisibleInTemporalRange( canvas->temporalRange() ) )
return newSelectedFeatures;

QgsVectorLayerTemporalContext temporalContext;
temporalContext.setLayer( vlayer );
temporalFilter = qobject_cast< const QgsVectorLayerTemporalProperties * >( vlayer->temporalProperties() )->createFilterString( temporalContext, canvas->temporalRange() );
}

QgsFeatureRequest request;
request.setFilterRect( selectGeomTrans.boundingBox() );
request.setFlags( QgsFeatureRequest::ExactIntersect );
@@ -265,6 +277,17 @@ QgsFeatureIds QgsMapToolSelectUtils::getMatchingFeatures( QgsMapCanvas *canvas,
else
request.setNoAttributes();

if ( !temporalFilter.isEmpty() )
request.setFilterExpression( temporalFilter );
if ( r )
{
const QString filterExpression = r->filter( vlayer->fields() );
if ( !filterExpression.isEmpty() )
{
request.combineFilterExpression( filterExpression );
}
}

QgsFeatureIterator fit = vlayer->getFeatures( request );

QgsFeature f;
@@ -365,7 +388,8 @@ QgsMapToolSelectUtils::QgsMapToolSelectMenuActions::QgsMapToolSelectMenuActions(
QgsMapToolSelectUtils::QgsMapToolSelectMenuActions::~QgsMapToolSelectMenuActions()
{
removeHighlight();
mJobData->isCanceled = true;
if ( mJobData )
mJobData->isCanceled = true;
if ( mFutureWatcher )
mFutureWatcher->waitForFinished();
}
@@ -384,13 +408,26 @@ void QgsMapToolSelectUtils::QgsMapToolSelectMenuActions::populateMenu( QMenu *me

void QgsMapToolSelectUtils::QgsMapToolSelectMenuActions::startFeatureSearch()
{
QString temporalFilter;
if ( mCanvas->mapSettings().isTemporal() )
{
if ( !mVectorLayer->temporalProperties()->isVisibleInTemporalRange( mCanvas->temporalRange() ) )
return;

QgsVectorLayerTemporalContext temporalContext;
temporalContext.setLayer( mVectorLayer );
temporalFilter = qobject_cast< const QgsVectorLayerTemporalProperties * >( mVectorLayer->temporalProperties() )->createFilterString( temporalContext, mCanvas->temporalRange() );
}

mJobData = std::make_shared<DataForSearchingJob>();
mJobData->isCanceled = false;
mJobData->source.reset( new QgsVectorLayerFeatureSource( mVectorLayer ) );
mJobData->selectGeometry = mSelectGeometry;
mJobData->context = QgsRenderContext::fromMapSettings( mCanvas->mapSettings() );
mJobData->filterString = temporalFilter;
mJobData->ct = QgsCoordinateTransform( mCanvas->mapSettings().destinationCrs(), mVectorLayer->crs(), mJobData->context.transformContext() );
mJobData->featureRenderer.reset( mVectorLayer->renderer()->clone() );

mJobData->context.expressionContext() << QgsExpressionContextUtils::layerScope( mVectorLayer );
mJobData->selectBehavior = mBehavior;
if ( mBehavior != QgsVectorLayer::SetSelection )
@@ -411,7 +448,6 @@ QgsFeatureIds QgsMapToolSelectUtils::QgsMapToolSelectMenuActions::search( std::s
if ( ! transformSelectGeometry( data->selectGeometry, selectGeomTrans, data->ct ) )
return newSelectedFeatures;


// make sure the selection geometry is valid, or intersection tests won't work correctly...
if ( !selectGeomTrans.isGeosValid( ) )
{
@@ -434,8 +470,18 @@ QgsFeatureIds QgsMapToolSelectUtils::QgsMapToolSelectMenuActions::search( std::s
request.setFilterRect( selectGeomTrans.boundingBox() );
request.setFlags( QgsFeatureRequest::ExactIntersect );

if ( !data->filterString.isEmpty() )
request.setFilterExpression( data->filterString );

if ( r )
{
request.setSubsetOfAttributes( r->usedAttributes( data->context ), data->source->fields() );
const QString filterExpression = r->filter( data->source->fields() );
if ( !filterExpression.isEmpty() )
{
request.combineFilterExpression( filterExpression );
}
}

QgsFeatureIterator fit = data->source->getFeatures( request );

@@ -189,6 +189,7 @@ namespace QgsMapToolSelectUtils
QgsCoordinateTransform ct;
QgsRenderContext context;
std::unique_ptr<QgsFeatureRenderer> featureRenderer;
QString filterString;
QgsVectorLayer::SelectBehavior selectBehavior;
QgsFeatureIds existingSelection;
};

0 comments on commit 67d5754

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