Bug report #15319
QgsMapCanvas().zoomToCanvas() not working in script
Status: | Closed | ||
---|---|---|---|
Priority: | Normal | ||
Assignee: | - | ||
Category: | Map Canvas | ||
Affected QGIS version: | 2.14.3 | Regression?: | No |
Operating System: | Windows | Easy fix?: | No |
Pull Request or Patch supplied: | No | Resolution: | |
Crashes QGIS or corrupts data: | No | Copied to github as #: | 23251 |
Description
Apologies in advance if it turns out I'm just doing something wrong - I have posted on GIS stackexchange but not had any responses.
the code below works as expected when typed in line-for-line in to the python console, but when I import the function from a script and run it it doesn't zoom to the selected feature. the feature here is a from point-based layer from a PostGres database.
def find_acc(layer, expression, scale = 2500): #find_acc("accidents", "\\"Police_ref\\" = '0111XH30035'", 2500) lay = core.QgsMapLayerRegistry.instance().mapLayersByName(layer)[0] canvas = QgsMapCanvas() request = core.QgsFeatureRequest().setFilterExpression(expression) iter = lay.getFeatures(request) selection = [f for f in iter] if len(selection) == 1: print "Found accident" accident = selection[0] else: raise IOError, "More than 1 accident found" lay.setSelectedFeatures([accident.id()]) # appears to work up until here - feature is highlighted on the map canvas.zoomToSelected()
(the formatting above isn't correct but that's not the issue, it's fine in my script)