I'd like to select all features in the current map extent that also satisfy some other condition via "Select by Expression".

For instance I'd like to do something like this:

intersects($map_extent, $geometry) and  "name" ilike 'Jay%'

Where $map_extent is a geometry that represents the current extent of the map.

I'm using QGIS 3.4.

I've written the following custom expression function which has the desired result but I'm wondering if there is an easier way?

from qgis.core import *
from qgis.gui import *
from qgis.utils import iface

@qgsfunction(args='auto', group='Custom')
def map_extent(feature, parent):
    return QgsGeometry.fromRect(iface.mapCanvas().extent())
  • 1
    You can create a virtual layer that contains only the features within the current extent, and query this virtual layer. See steps here – JGH Jun 25 at 14:52
  • Another option is to have the attribute table to display only record within the current extent and to do the selection on the entire layer. (warning: all features matching the filter will be selected, but only the displayed one will be listed. It works ok for finding features, but will not if you export/use this selection with other tools) – JGH Jun 25 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.