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?

Joseph
76.7k8 gold badges173 silver badges286 bronze badges
asked Oct 3, 2016 at 15:07
2
  • I think this question may be a duplicate of this one: Using in-memory vector layer with QGIS processing / SEXTANTE Commented 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. Commented Oct 5, 2016 at 13:08

2 Answers 2

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)
answered Oct 4, 2016 at 13:07
1
  • This is what I needed. It is working and really useful! Commented Oct 4, 2016 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.

answered Oct 4, 2016 at 9:31
4
  • 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. Commented 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. Commented 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 =) Commented Oct 5, 2016 at 9:10
  • 1
    Yes, you are right. Now I can understand the difference between the two concepts. Commented Oct 5, 2016 at 13:10

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.