Thursday 3 October 2019

pyqgis - What are the 'output_layer' Parameter Options for QGIS tool "saveselectedfeatures"?


After a few comments and my own consideration I've completely Edited the content (and title) of this post.


What are valid "output_layer" parameter options when scripting with the "saveselectedfeatures" tool?


I'm writing a script that uses the tool as follows:


processing.runandload('qgis:saveselectedfeatures', Input, 'Output.shp')

I also tried removing the shapefile extension and just putting 'Output' as the final parameter. Still errors and no resulting output.


Don't worry about "Input", it's a perfectly valid layer with selected features.



PARTIAL SOLUTION


@Joseph provided the partial solution of replacing " 'Output.shp' " with " None " (no quotations).


processing.runandload('qgis:saveselectedfeatures', Input, None)

This results in successful execution with a temporary output layer called "Solution" popping up in the Layers Window.


BUT...


I call this a partial solution because I find it hard to believe that the designer(s) of this tool didn't also intend to give users the option to choose the name of their output file (as I originally tried to do) via the final parameter ('output_layer'), as described in the tool's Help Tab.


FINAL THOUGHTS


The tool's Help tab offers the following instruction:


enter image description here



enter image description here


But it seems this is not quite as "helpful" as it could be. It seems like a few short and simple examples of acceptable values for the 2nd parameter would avoid any confusion.


Something like:


Output file options:  temporary layer, Shapefile, etc...

2nd Parameter ('output_layer') options:
" 'User_chosen_name' " (or " 'User_chosen_name.shp' "), OR "None" (produces auto-named temporary layer).

I'm a relatively new user of QGIS.



Answer




Turns out (on a Mac at least) you have to put the full path plus the name for your file. For example, the following code places the output shapefile "MyFile" on my desktop.


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

Seems obvious in hindsight but again better documentation in the Help tab would have been a simple way to avoid confusion.


Still need to figure out why the output gets loaded onto map project as "Selection" and not with the name I provided ("MyFile"), but I've already asked that question in another post ("How to write QGIS script to load Output file to Map Project with Desired Name?")


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...