I'm a beginner to Qgis and Python;i need a script (don't need it to be a plugin) to get maps of selected features from a layer satisfying certain conditions;Here is my code:
from qgis.gui import *from PyQt4.QtCore import *from PyQt4.QtGui import *import timefnam ='C:/Users/User/.qgis2/Test/Output/'canvas = qgis.utils.iface.mapCanvas()#print canvas.size()Nom =QInputDialog.getText(None,"Value", "Enter text",0)fnam2 = fnam + Nom[0] + '.txt'#print fnam2outfile = open (fnam2, 'w')layer = iface.activeLayer()layer.setSelectedFeatures([])#provider =layer.dataProvider()selection=[]for f in layer.getFeatures(): n1 = f['Comune_1'] n2 = f['Comune_2'] n3 = f['Comune_3'] n4 = f['Comune_4'] n5 = f['Comune_5'] if (n1 == Nom[0]) or (n2 == Nom[0]) or (n3 == Nom[0]) or (n4 == Nom[0]) or (n5 == Nom[0]) : selection.append(f.id()) line = '%s\n' % (f['link']) unicode_line = line.encode('utf-8') outfile.write(unicode_line)layer.setSelectedFeatures(selection)#provider.select()outfile.close()mnam ='C:/Users/User/.qgis2/Test/Output/' + Nom[0] + '.tif'mnam2 ='C:/Users/User/.qgis2/Test/Output/' + Nom[0] + '.png'#print mnamcanvas.zoomToSelected()canvas.refresh()canvas.update()canvas.saveAsImage(mnam,None,'tif')#time.sleep(3)canvas.saveAsImage(mnam2,None,"PNG")#print 'Finished'My problem is that saved image(s) show the map at the moment i press "run" button in Python console, not the actual map based on the last selection;i tried to insert a time.sleep() call to delay saving, hoping it could help waiting rendering to complete before saving, but it doesn't work;i also tried recursing the whole process with a for loop, neither working;any tips on what am i doing wrong?
أكثر...
from qgis.gui import *from PyQt4.QtCore import *from PyQt4.QtGui import *import timefnam ='C:/Users/User/.qgis2/Test/Output/'canvas = qgis.utils.iface.mapCanvas()#print canvas.size()Nom =QInputDialog.getText(None,"Value", "Enter text",0)fnam2 = fnam + Nom[0] + '.txt'#print fnam2outfile = open (fnam2, 'w')layer = iface.activeLayer()layer.setSelectedFeatures([])#provider =layer.dataProvider()selection=[]for f in layer.getFeatures(): n1 = f['Comune_1'] n2 = f['Comune_2'] n3 = f['Comune_3'] n4 = f['Comune_4'] n5 = f['Comune_5'] if (n1 == Nom[0]) or (n2 == Nom[0]) or (n3 == Nom[0]) or (n4 == Nom[0]) or (n5 == Nom[0]) : selection.append(f.id()) line = '%s\n' % (f['link']) unicode_line = line.encode('utf-8') outfile.write(unicode_line)layer.setSelectedFeatures(selection)#provider.select()outfile.close()mnam ='C:/Users/User/.qgis2/Test/Output/' + Nom[0] + '.tif'mnam2 ='C:/Users/User/.qgis2/Test/Output/' + Nom[0] + '.png'#print mnamcanvas.zoomToSelected()canvas.refresh()canvas.update()canvas.saveAsImage(mnam,None,'tif')#time.sleep(3)canvas.saveAsImage(mnam2,None,"PNG")#print 'Finished'My problem is that saved image(s) show the map at the moment i press "run" button in Python console, not the actual map based on the last selection;i tried to insert a time.sleep() call to delay saving, hoping it could help waiting rendering to complete before saving, but it doesn't work;i also tried recursing the whole process with a for loop, neither working;any tips on what am i doing wrong?
أكثر...