2

I am new to both Python and QGIS.

Here's a sample of my code:

unique = layer.uniqueValues(index)
attributes = layer..dataProvider().fields().toList()
assistant_layer = QgsVectorLayer("Point", "temporary_points", "memory")
assistant_pr = assistant_layer.dataProvider()
assistant_layer.startEditing()
assistant_pr.addAttributes(attributes)
for i in unique:
    features = vector.features(layer)
    for f in features:
        idVar = f[fieldName]
        if unicode(idVar).strip() == unicode(i).strip():
            assistant_pr.addFeatures([f])
    assistant_layer.commitChanges()
    delone_triangles = processing.runalg("qgis:delaunaytriangulation", assistant_layer, None)['OUTPUT']
#rest of the code is irrelevant to the question

Basically I'm trying to create a layer that has the same set of properties as my input layer. Much in the same way as here How to create a new empty vector layer programmatically? except there the Attributes have been defined in code, I want mine to load them from another input layer. Then I want to copy a specific subset of features onto that layer. Something however seems to be wrong with my method.

The broader issue that I'm trying to solve here is that I need to feed a subset of features from a layer into an algorithm that takes a whole layer as its input (specifically "qgis:delaunaytriangulation"), so my intended solution is to create another layer and then copy specific features that fit the criteria onto it. However, whereas normally qgis:delaunaytriangulation outputs a path to a file containing the triangulation, when I do the above it returns a path to a file that doesn't exist. I figure it's because either my assistant_layer or the method of copying features onto it is faulty.

2
  • Just to aid debugging the code it might be worth adding the assistant_layer to the Map Registry after committing changes so you can inspect the attribute table? If it looks right then you'll at least know the copying process is working. If not you can share a screengrab of what's going wrong. Commented Aug 3, 2015 at 20:03
  • OK thanks. I had to make assistant_layer global to add it to map registry and making it global solved my problems. Now the code breaks somewhere way later down the line (I'm modifying an existing plugin and the code is huge). If I'm gonna need further assistance it's gonna be a matter for another thread. Thanks again.
    – cyfrostan
    Commented Aug 4, 2015 at 9:03

0

Your Answer

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