TransWikia.com

Running a tool on subset of layer two points at a time PyQGIS

Geographic Information Systems Asked on August 16, 2021

layer = iface.activeLayer()

request = QgsFeatureRequest()
request.setLimit(2)
request.setFilterFids([1, 2])
for feature in layer.getFeatures(request):
    print([f for f in feature])

params = { '-g' : False, 'GRASS_MIN_AREA_PARAMETER' : 0.0001, 'GRASS_OUTPUT_TYPE_PARAMETER' : 0, 'GRASS_REGION_PARAMETER' : None, 'GRASS_SNAP_TOLERANCE_PARAMETER' : -1, 'GRASS_VECTOR_DSCO' : '', 'GRASS_VECTOR_EXPORT_NOCAT' : False, 'GRASS_VECTOR_LCO' : '', 'acolumn' : None, 'arc_type' : [0,1], 'input' : '/Users/uname/Downloads/scatteredroads.sqlite', 'npoints' : -1, 'output' : '/Users/uname/Downloads/out3.sqlite', 'points' : '/Users/uname/Downloads/testptssnapped.sqlite|layername=testptssnapped', 'terminal_cats' : '1-100000', 'threshold' : 50 }

#processing.runAndLoadResults("grass7:v.net.steiner", params)

What I want to do is run a processing algorithm (v.net.steiner) on two points at a time. The background is that the whole tool fails if even one point is faulty. By running the algorithm on two points at a time I can find where it fails and delete that point.

With request.setFilterFids([1, 2]) I have selected just two points, but how can I pass this subset of the layer into the tool?

Do I need to create a new layer out of this subset?

How do I do that?

One Answer

After searching other answers I was able to find this bit of code, which is a way of creating a new layer in memory.

new_layer = layer.materialize(QgsFeatureRequest().setFilterFids([1,2]))
QgsProject.instance().addMapLayer(new_layer)

Answered by Benny Henny on August 16, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP