I am new to QGIS Python scripting. I want to select some features from an input layer and save them to a memory layer for further processing. Here is my code:
##Sampled_trees=vector
##Input_field= field Sampled_trees
from qgis.core import *
from PyQt4.QtCore import *
inputTrees= processing.getObject(Sampled_trees)
inputField = inputTrees.fieldNameIndex(Input_field)
tempLayer = QgsVectorLayer("Point", "temporary_points", "memory")
processing.runalg("qgis:selectbyattribute", inputTrees, inputField,0,1,inputTrees)
processing.runalg('qgis:saveselectedfeatures', inputTrees, tempLayer)
When the last line is executed, I get the error:
TypeError: 'QgsVectorLayer' object has no attribute 'getitem'
How can I fix this issue?
-
I think this question may be a duplicate of this one: Using in-memory vector layer with QGIS processing / SEXTANTEJoseph– Joseph2016年10月05日 12:57:36 +00:00Commented Oct 5, 2016 at 12:57
-
It is a similar question. I studied that question before asking this one but that answer was not useful for me. So I had to ask again and I got the answer I needed.Flaviu Meseșan– Flaviu Meseșan2016年10月05日 13:08:20 +00:00Commented Oct 5, 2016 at 13:08
2 Answers 2
As you noted in your question you may also use memory layer to add selected features from source layer, see code below:
inputTrees= processing.getObject(Sampled_trees)
inputField = inputTrees.fieldNameIndex(Input_field)
processing.runalg("qgis:selectbyattribute", inputTrees, Input_field, 0, 1)
tempLayer = QgsVectorLayer("Point", "temporary_points", "memory")
features = inputTrees.selectedFeatures()
temp_data = tempLayer.dataProvider()
attr = inputTrees.dataProvider().fields().toList()
temp_data.addAttributes(attr)
tempLayer.updateFields()
temp_data.addFeatures(features)
QgsMapLayerRegistry.instance().addMapLayer(tempLayer)
-
This is what I needed. It is working and really useful!Flaviu Meseșan– Flaviu Meseșan2016年10月04日 13:34:32 +00:00Commented Oct 4, 2016 at 13:34
You can use None
as the output which automatically creates a temporary layer. This is mentioned in the documentation:
For output data objects, type the file path to be used to save it, just as it is done from the toolbox. If you want to save the result to a temporary file, use
None
.
So your code can be shortened to:
##Sampled_trees=vector
##Input_field= field Sampled_trees
inputTrees= processing.getObject(Sampled_trees)
inputField = inputTrees.fieldNameIndex(Input_field)
processing.runalg("qgis:selectbyattribute", inputTrees, Input_field, 0, 1)
tempLayer = processing.runalg('qgis:saveselectedfeatures', inputTrees, None)
Note that for the qgis:selectbyattribute
algorithm, you don't specify the output as you are only selecting features from the input layer.
-
Using None argument for qgis:saveselectedfeatures saves the layer to disk, e.g. C:\\Users\\userName\\AppData\\Local\\Temp\\processing68ace1dd77264cf48d1f95eb837b93d1\86円ee4684d3784c7eb27506235b395999\\OUTPUTLAYER.shp, I'm not sure this is a temporary layer.artwork21– artwork212016年10月04日 12:37:45 +00:00Commented Oct 4, 2016 at 12:37
-
Yes, the result of processing.runalg('qgis:saveselectedfeatures', inputTrees, None) was a python dictionary containing the name of the output layer and the path to that layer. That file can be imported as a temporary layer.Flaviu Meseșan– Flaviu Meseșan2016年10月04日 13:30:13 +00:00Commented Oct 4, 2016 at 13:30
-
@artwork21 - My understanding is that a temporary layer is that which is saved to disk temporarily until it is deleted either by restarting the computer or any other means; a memory layer is that which is stored in the memory used by QGIS and is deleted when QGIS is closed. I guess both layers are temporary but I'll edit the title to reflect that a memory layer was needed =)Joseph– Joseph2016年10月05日 09:10:40 +00:00Commented Oct 5, 2016 at 9:10
-
1Yes, you are right. Now I can understand the difference between the two concepts.Flaviu Meseșan– Flaviu Meseșan2016年10月05日 13:10:03 +00:00Commented Oct 5, 2016 at 13:10