Try something like this below
geom = None
for feat in selectFeatures:
if geom == None:
geom = feat.geometry()
else:
geom = geom.combine(feat.geometry())
Use iface.actionZoomToSelected().trigger()
in your code for zoom to selected feature in QGIS
change the database data using your information
Code
from qgis.core import *
uri = QgsDataSourceURI()
uri.setConnection ("localhost", "5432", "OSM", "postgres", "postgres")
uri.setDataSource("public", 'grassland', "geom", "")
layer1 = QgsVectorLayer ( uri.uri(), "grassland", "postgres")
QgsMapLayerRegistry.instance().addMapLayer(layer1, True)
crs = QgsCoordinateReferenceSystem(4326)
iface.mapCanvas().mapSettings().setDestinationCrs(crs)
expressionCur = 'cat = 10'
it = layer1.getFeatures(QgsFeatureRequest().setFilterExpression(expressionCur))
layer1.setSelectedFeatures ( [ f.id() for f in it ])
iface.actionZoomToSelected().trigger()
UPDATE
You are right, to use it in the console you must use some signal, like for example
from qgis.core import *
def Zoom_filter():
iface.mapCanvas().mapCanvasRefreshed.disconnect(Zoom_filter)
expressionCur = 'cat = 10'
for lyr in QgsMapLayerRegistry.instance().mapLayers().values():
if lyr.name() == "grassland":
it = lyr.getFeatures(QgsFeatureRequest().setFilterExpression(expressionCur))
lyr.setSelectedFeatures ( [ f.id() for f in it ])
iface.actionZoomToSelected().trigger()
break
uri = QgsDataSourceURI()
uri.setConnection ("localhost", "5432", "OSM", "postgres", "postgres")
uri.setDataSource("public", 'grassland', "geom", "")
layer1 = QgsVectorLayer ( uri.uri(), "grassland", "postgres")
crs = QgsCoordinateReferenceSystem(4326)
iface.mapCanvas().mapSettings().setDestinationCrs(crs)
QgsMapLayerRegistry.instance().addMapLayer(layer1, True)
iface.mapCanvas().mapCanvasRefreshed.connect(Zoom_filter)
Best Answer
You need to set the extents of the map canvas to the extents of the selections: