5

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?

5

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 Oct 4 '16 at 13:34
1

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\\86ee4684d3784c7eb27506235b395999\\OUTPUTLAYER.shp, I'm not sure this is a temporary layer. – artwork21 Oct 4 '16 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 Oct 4 '16 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 Oct 5 '16 at 9:10
  • 1
    Yes, you are right. Now I can understand the difference between the two concepts. – Flaviu Meseșan Oct 5 '16 at 13:10

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.