4

I have used following code for selecting the features and save it as image.

layer = self.iface.activeLayer();
layer.setSelectedFeatures([])
count =layer.featureCount()
lstChalta = []
lstFeatures = processing.features(layer)
for elem in lstFeatures:
   geom = elem.geometry()
   attr = elem.attributes()
   layer.setSelectedFeatures([elem.id()])
   self.iface.mapCanvas().zoomToSelected( layer )
   box = layer.boundingBoxOfSelected()
   self.iface.mapCanvas().setExtent(box)
   self.iface.mapCanvas().refresh()
   self.iface.mapCanvas().refresh()
   cno= str(attr[layer.fieldNameIndex('cno')])
   path = 'D:\Test'+ str(cno) + '.png'
   self.iface.mapCanvas().saveAsImage(path)
   #QMessageBox.information(None, "DEBUG:", str(test))

Whenever I run the script, it is selecting only the last feature. However, if I uncomment message box line it is working fine and selecting each and every feature one by one. In addition to that it is saving the image properly.

I want to work the script without showing message.

1 Answer 1

1

I don't know if exists a better method for this question but, I found out this procedure:

How to programmatically close a QMessagebox without clicking ok or X?

where it was programmatically implemented a custom QMessageBox for automatically closing after a certain time. I tried out other methods (involving time python library) with your code but it was necessary a delay equivalent to a QMessagebox for a successfully result.

I slightly modified your code (for adapting to my system) and the proposal in the above link to use a QMessageBox without OK button.

The next code:

class CustomMessageBox(QMessageBox):

    def __init__(self, *__args):
        QMessageBox.__init__(self)
        self.timeout = 0
        self.autoclose = False
        self.currentTime = 0

    def showEvent(self, QShowEvent):
        self.currentTime = 0
        if self.autoclose:
            self.startTimer(1000)

    def timerEvent(self, *args, **kwargs):
        self.currentTime += 1
        if self.currentTime >= self.timeout:
            self.done(0)

    @staticmethod
    def showWithTimeout(timeoutSeconds, 
                        message, 
                        title, 
                        icon = QMessageBox.Information, 
                        buttons = QMessageBox.NoButton):

        w = CustomMessageBox()
        w.autoclose = True
        w.timeout = timeoutSeconds
        w.setText(message)
        w.setWindowTitle(title)
        w.setIcon(icon)
        w.setStandardButtons(buttons)
        w.exec_()

import processing
from PyQt4.QtGui import QMessageBox

layer = iface.activeLayer();
layer.setSelectedFeatures([])
count =layer.featureCount()
lstChalta = []

lstFeatures = processing.features(layer)

for elem in lstFeatures:
    geom = elem.geometry()
    attr = elem.attributes()
    layer.setSelectedFeatures([elem.id()])
    iface.mapCanvas().zoomToSelected( layer )
    box = layer.boundingBoxOfSelected()
    iface.mapCanvas().setExtent(box)
    iface.mapCanvas().refresh()
    cno= str(attr[layer.fieldNameIndex('cno')])
    path = '/home/zeito/Desktop/folder/Test'+ str(cno) + '.png'

    CustomMessageBox.showWithTimeout(.5, 
                                     'feature: ' + cno, 
                                     "Information", 
                                     icon = QMessageBox.Information)

    iface.mapCanvas().saveAsImage(path)

it was running with a vector layer where I created your 'cno' field; as it can be observed at the next image:

enter image description here

I took a screenshot during the automatic execution of my code (see next image for feature 3).

enter image description here

After running the code, the target folder had the png images as I expected.

enter image description here

1
  • @xunik: it works perfectly for me.... Commented Dec 9, 2015 at 4:48

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.