Skip to content

Instantly share code, notes, and snippets.

@slarosa
Created April 21, 2017 14:18
'''
QGIS Action to select and zoom the feature from related table
when one of field pairs is string. Using LIKE in QgsExpression.
Ref: https://lists.osgeo.org/pipermail/qgis-it-user/2017-April/002413.html
'''
from qgis.utils import iface
# inserire qui il nome del layer geometrico
vl = QgsMapLayerRegistry.instance().mapLayersByName('punti')[0]
iface.setActiveLayer(vl)
cLayer = iface.mapCanvas().currentLayer()
# qui sotto i nomi dei due campi da correlare
expr = QgsExpression("id_punto LIKE '[% "punto" %]%'")
it = cLayer.getFeatures( QgsFeatureRequest( expr ) )
ids = [i.id() for i in it]
cLayer.setSelectedFeatures( ids )
# zoom alla selezione
box = cLayer.boundingBoxOfSelected()
iface.mapCanvas().setExtent(box)
iface.mapCanvas().refresh()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment