Map on canvas renders after image saving
We are searching data for your request:
Upon completion, a link will appear to access the found materials.
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 time fnam ='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 + '.txt' #print fnam2 outfile = 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) or (n2 == Nom) or (n3 == Nom) or (n4 == Nom) or (n5 == Nom) : selection.append(f.id()) line = '%s ' % (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 + '.tif' mnam2 ='C:/Users/User/.qgis2/Test/Output/' + Nom + '.png">
now professionals or village administrators may ask for orthophotos of a specific village; running my script i wish:
- have a list of orthophotos of that specific village (this works fine with txt file i create);
- give a map showing the arrangement of requested orthophotos as a complementary aid.
Now, if i run the script selecting, say, "Potenza", i eventually can see in Qgis the updated map with desired selection, but saved images show the whole region:(I mean "Potenza.png">
(tif images are identical to png);
(When i'll have a working script selected "rectangles" will show image name, and i won't use inputdialog but i'll get village names from the first shapefile and run it only once for all villages).
I don't understand your problem.
1) You create a text file with the selected features id
layer = iface.activeLayer() # you select all the features with nom in one of the 5 fields selection = [f.id() for f in layer.getFeatures() if nom in [f['Comune_1'],f['Comune_2'],f['Comune_3'],f['Comune_4'],f['Comune_5']]] # then you write in a text file the link field of the selected features with open(newfile, 'w') as outfile: for f in layer.getFeatures(): if f.id() in selection: # the default format is unicode outfile.write('%s ' % (f['link']))
2) After you save the canvas showing the selected features
# select the features layer.setSelectedFeatures(selection) canvas.zoomToSelected() # save the files canvas.saveAsImage(mnam,None,'TIF') canvas.saveAsImage(mnam2,None,"PNG")
And it works without problem.