I don't think the docs are very clear on this. From my understanding, the Set style for vector layer algorithm does not create a new output layer. The end result is the input layer with the style file applied. I believe this is why there is no "output" option for the algorithm.
From the QGIS 2.8 docs and checking the parameters from the Python console, both mention an Output
:
import processing
processing.alghelp("qgis:setstyleforvectorlayer")
ALGORITHM: Set style for vector layer
INPUT <ParameterVector>
STYLE <ParameterFile>
OUTPUT <OutputVector>
However, when you check the Console usage, you will see that there is no parameter for Output
:
processing.runalg("qgis:setstyleforvectorlayer", input, style)
So if you want to add more than one style to a shapefile, you may need to create another set of the same algorithms within your model.
Hope this helps!
You may continue using the memory layer by loading it as an object. Before doing this, you need to know how the output from the algorithm is called, so typing these lines in the Python Console:
import processing
processing.alghelp('gdalogr:clipvectorsbypolygon')
you will see that the output's name is OUTPUT_LAYER
:
ALGORITHM: Clip vectors by polygon
INPUT_LAYER <ParameterVector>
CLIP_LAYER <ParameterVector>
OPTIONS <ParameterString>
OUTPUT_LAYER <OutputVector>
This means that you will be able to use it if you add a new variable and call it in this way:
# change 'my_algorithm' name as you want
my_algorithm = general.runalg('gdalogr:clipvectorsbypolygon', 'lokale_orte.shp', 'kleine_kreise.shp', None, None)
# change 'my_output' name as you want
my_output = processing.getObject(my_algorithm['OUTPUT_LAYER'])
By now, my_output
will be the object to use for calling the layer returned by the clip operation and you are ready to do something with it.
EDIT For getting your script working, you may try using the following code (slightly different from yours):
import sys
from qgis.core import *
import qgis.utils
qgs = QApplication([], True)
QgsApplication.setPrefixPath("/usr", True)
QgsApplication.initQgis()
# Prepare processing framework
sys.path.append('/usr/share/qgis/python/plugins') # Dir where Processing is located
from processing.core.Processing import Processing
from processing.tools import *
Processing.initialize()
# load base layer
blayer = QgsVectorLayer("path/to/the/shapefile/lokale_orte.shp", "lokale_orte", "ogr")
if not blayer.isValid():
print "Layer failed to load!"
else:
print "success: load layer"
# load clip layer
clayer = QgsVectorLayer("path/to/the/shapefile/kleine_kreise.shp", "kleine_kreise", "ogr")
if not clayer.isValid():
print "Layer failed to load!"
else:
print "success: load layer"
# run clip
# change 'my_algorithm' name as you want
my_algorithm = general.runalg('gdalogr:clipvectorsbypolygon', 'lokale_orte.shp', 'kleine_kreise.shp', None, None)
# change 'my_output' name as you want
my_output = processing.getObject(my_algorithm['OUTPUT_LAYER'])
QgsApplication.exitQgis()
qgs.exit()
Best Answer
I'm not familiar with
PyQGIS
but this can be easily accomplished usingogr
.The following script shows an example:
You can, of course, change the name of the driver (in case your layer is not a shapefile) and the geometry of your new files (in case the input vector layer file is not a polygon).
More information about
ogr
vector formats here: http://www.gdal.org/ogr_formats.html