The following line of code produces the output as a shapefile with the name I want ("MyFile") and where I want it ("path"),

path = "/Users/User1/Desktop/"
processing.runandload('qgis:saveselectedfeatures', Input, path+'MyFile.shp')

The actual new output shapefile has been saved on my Desktop with the name "MyFile".

However, the "andload" part of "runandload" places the output ON MY MAP PROJECT with the name "Selection", not "MyFile".

This becomes a problem later in my script if I generate another output file, it too gets added to the map project with the name "Selection", making it impossible to choose between them for subsequent processing.

How should my script be written to load output to the map project with the desired name?

  • 1
    Why not using setLayerName() method to rename the layer (see here)? – wiltomap Nov 9 '15 at 20:25
  • Props @wiltomap. Still seems strange the output loads without the name I assign it but this extra step is the practical workaround that achieves what I need. Much appreciated. – Waterman Nov 10 '15 at 16:39

If you don't want to rename the layer, you could use the following code to load the output that's saved directly into your map project and assign it any name you wish. No need to use runandload and the only two parameters that you may want to change would be the name and the directory in the path:

from qgis.core import QgsMapLayerRegistry, QgsVectorLayer
name = 'MyFile'
path = "/Users/User1/Desktop/" + name + '.shp'

processing.runalg('qgis:saveselectedfeatures', Input, path)
layer = QgsVectorLayer(path, name, "ogr")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.