8

In QGIS, I'm trying to select all the features that are visible in my map extent but I can't find a solution with "select by expression" or something else.

I will use your tips inside modeler to make a buffer of 5 meters around my polygons, but only on those which are visible in my map extent.

1
  • 1
    I tried within ($geometry, @map_extent), but unfortunately, it does not work. But the answer by @JGH should solve your problem.
    – Babel
    Commented Sep 9, 2021 at 15:47

1 Answer 1

11

The trick is to create a function that access QGIS graphical interface, and that is piped to the query of the select by expression.

  1. Open a function editor (from anywhere, including from field calculator) and create a new function that reads the canvas extent and returns it as a geometry.
from qgis.core import *
from qgis.gui import *
from qgis.utils import iface

@qgsfunction(args='auto', group='Custom')
def currentExtent(feature, parent):
    return QgsGeometry.fromRect(iface.mapCanvas().extent())
  1. Open the select by expression and use intersects($geometry,currentExtent()) (or within(..) for entirely contained polygons)

The same function can be used in a virtual layer as shown here, that you could eventually modify to compute the buffer.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.