5

I am trying to write a python script that iterates over the features of a layer, selects a features and feeds into processing.runalg(). I am pretty sure the selection is going on. However, the processing.runalg("qgis:xxx") bit ocurrs oblivious to the selection! I know there are plenty of similar questions and answers, but I have tried their suggestions and haven't been successful. I though maybe if someone sees the code maybe could help me resolve this.

Here is the code (indents in the for loops might be messed up from formatting):

inter_shpmemory = iface.addVectorLayer(main_directory + "\\" + 
intersect_shpname,"Zone layer",labtype)
specieslayer_mem = iface.addVectorLayer(main_directory + "\\" + 
bd_shpname,"GBIF layer",labtype)

###Create a folder to safe all stuff
newfolder = main_directory + "\\" + "Zonal_GBIF"

if not os.path.exists(newfolder):
    os.makedirs(newfolder)


it = specieslayer_mem.getFeatures( QgsFeatureRequest() )
inputLayerIds = [i.id() for i in it]
#selects all of the input feature and one feature from the clip layer
specieslayer_mem.setSelectedFeatures( inputLayerIds )###is this even 
#needed?



features = inter_shpmemory.getFeatures()

for feature in features:
            indbd_name = feature['simp_name']
            inter_shpmemory.setSelectedFeatures([feature.id()])
            print "Feature ID %d: " % feature.id()
            print indbd_name
            processing.runalg('qgis:clip', specieslayer_mem, inter_shpmemory, newfolder + '\\' + indbd_name +'.shp')  
            inter_shpmemory.setSelectedFeatures([])

specieslayer_mem.setSelectedFeatures([])

I can't get to do it what I want.

0

2 Answers 2

2

The problem can be that, you have to save the selection first. I guess the 'qgis:clip' does not care with the selection. Add this line too, and reload the saved layer with an other line:

QgsVectorFileWriter.writeAsVectorFormat( layer, path + 'admpoly/' + name + ".shp", "system", crs, "ESRI Shapefile", 1)

1
  • Thanks pnz. I will try this. I had considered that as a potential solution, but I first wanted to check with more knowledgeable people.
    – SebasCaTi
    Commented Apr 22, 2017 at 14:38
2

Instead of making lists, if you want to select all features in a layer, you could use:

layer.selectAll()

Conversely, if you want to remove all selections, you could use:

layer.removeSelection()

So in order to get your code to work, you could try using:

inter_shpmemory = iface.addVectorLayer(main_directory + "/" + 
intersect_shpname,"Zone layer",labtype)
specieslayer_mem = iface.addVectorLayer(main_directory + "/" + 
bd_shpname,"GBIF layer",labtype)

###Create a folder to safe all stuff
newfolder = main_directory + "/" + "Zonal_GBIF"

if not os.path.exists(newfolder):
    os.makedirs(newfolder)

specieslayer_mem.selectAll()

features = inter_shpmemory.getFeatures()
for feature in features:
    indbd_name = feature['simp_name']
    inter_shpmemory.setSelectedFeatures([feature.id()])
    print "Feature ID %d: " % feature.id()
    print indbd_name
    processing.runalg('qgis:clip', specieslayer_mem, inter_shpmemory, newfolder + '/' + indbd_name +'.shp')  
    inter_shpmemory.removeSelection()

specieslayer_mem.removeSelection()

Note that the "Use only selected features" option must be enabled in:

Processing > Options > General > Use only selected features
2
  • 1
    Joseph: I just gave it another try. I set up the Processing options and improved the selection code. Similar as the situation I had, the clip is happens, however it does not respond to the selection. In that respecti, PNZ's solution seems to work better than the one you provided. Thanks anyway!
    – SebasCaTi
    Commented May 4, 2017 at 18:47
  • @SebasCaTi - Most welcome, glad you got it to work :)
    – Joseph
    Commented May 5, 2017 at 8:58

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.