4

I'm trying to select and zoom on selected features on standalone QGis application. Selection seems to work, console shows me list of selected items and they are exactly I expected.

ids = []
for f in kt.getFeatures():
 if f['KatastriNumber']==k_n:
     ids.append(f.id)
     print f['eraldus']
kt.setSelectedFeatures(ids)

But zooming to selection does not work. I have tried many code samples i found from internet. None of them doesnt zoom to selected. Can only zoom to entire layer with:

canvas.setExtent(kt.extent())

I tried to make a box with command:

box = kt.boundingBoxOfSelected()
canvas.setExtent(box)

This gives no result.Then I tried with command

canvas.zoomToSelected(kt)

also no result. How to tell app that some features from layer are selected?

3
  • Did you try canvas.zoomToSelected() (with no argument) already? Also, it should be f.id() (with parenthesis) to properly call the id method. You can check ck.selectedFeatureCount() to make sure you have selected features. This post tells you how to use expressions to select features and then zoom to the selection: gis.stackexchange.com/questions/91011/… Commented Jan 27, 2017 at 12:20
  • I tried canvas.zoomToSelected(). still zoomed to location last opened in project. kt.selectedFeatureCount() displays 5 Commented Jan 27, 2017 at 15:54
  • Great that you solved it. If you want, post the answer and mark it as accepted to properly close the question. Commented Jan 30, 2017 at 11:56

1 Answer 1

2

It required once more canvas refresh to zoom to right place. Final working block:

ids = []
for f in kt.getFeatures():
 if f['KatastriNumber']==k_n:
     ids.append(f.id())
print ids 
kt.setSelectedFeatures(ids)
nt= kt.selectedFeatureCount()
print "Valitud eraldusi: %d"%nt
box = kt.boundingBoxOfSelected()
kt.setSelectedFeatures([])
kt.loadNamedStyle(stiil)
canvas.setExtent(box)
canvas.refresh()
canvas.zoomScale(10000)
canvas.refresh()
canvas.show()

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.