5

I have two polygon features: epoca and result.

I would like to select the features that correspond to the number 1 feature (features A and B) and then select the corresponding features with the number 2 feature (features C and D).

enter image description here

I had done the following:

from processing.core.Processing import Processing
import processing.tools

layerResult = processing.getObject('lyr_result')
layerEpoca = processing.getObject('lyr_epoca')

processing.tools.general.runalg('qgis:selectbylocation', layerResult, layerEpoca, u'equals', 0.005,0)

layerResult.invertSelection() #I'm searching for the different polygons
selectFeatures = layerResult.selectedFeatures() #are the features selected in result

processing.tools.general.runalg('qgis:selectbylocation',layerEpoca, layerResult, u'within', 0.005,0) #here select the features in epoca all at once!

What is being done is that you are selecting features A, B and C, D at the same time. I would like to select these features at different times. Is this possible?

I had thought of something like this:

for feat in selectFeatures:
    processing.tools.general.runalg('qgis:selectbylocation',layerEpoca, selectFeatures, u'within', 0.005,0)

2 Answers 2

8

You can also do this without processing :

layer_select = QgsMapLayerRegistry.instance().mapLayersByName('lyr_result')[0]
layer_to_select = QgsMapLayerRegistry.instance().mapLayersByName('lyr_epoca')[0]

for selected_feat in layer_select.selectedFeatures():
    to_select = []
    for feat_to_select in layer_to_select.getFeatures():
        if feat_to_select.geometry().intersects(selected_feat.geometry()):
            to_select.append(feat_to_select.id())
    layer_to_select.setSelectedFeatures(to_select)
    print to_select #Or do something with the selected Features

That will select the features that intersect the selected Feature for every selected feature separately.

Here I use the print to_select line to print the list of feature's IDs that are selected for each selected result's features.

If you work on a huge dataset, consider using Index to speed up the process.

4

Please change crs=epsg:4326 to your EPSG code.

Add the QgsVectorLayer creation to fit the requirement of "qgis:selectbylocation" algorithm and create tmp layer for each selected features

# create an empty Temporary QgsVectorelayer 
tempLayer = QgsVectorLayer('Polygon?crs=epsg:4326', tmp_layer_name, 'memory')

for feat in selectFeatures: # Loop throught the selectedfeatures list

    # Create / initialize an empty feature 
    cfeature = QgsFeature()

    # add feature with attributes to tempLayer
    cfeature_Attributes=[]
    cfeature_Attributes.extend(feat.attributes())
    cfeature.setGeometry(feat.geometry())
    cfeature.setAttributes(cfeature_Attributes)         
    
    dataProvider = tempLayer.dataProvider()
    tempLayer.startEditing()
    dataProvider.addFeatures([cfeature])
    tempLayer.commitChanges()
    processing.tools.general.runalg('qgis:selectbylocation',layerEpoca, tempLayer, u'within', 0.005,0)
    ##########################################################################
    # Here do something with selected feature(s) that match selectbylocation #
    ##########################################################################
    # clean the tempLayer
    tempLayer.deleteFeature(cfeature.id())
    tempLayer.commitChanges()

tempLayer.stopEditing()
8
  • did not work. I think it's because the layerEpoca is a vector layer object and selectFeatures is a feature object. Any idea of conversion? Commented Sep 26, 2017 at 12:02
  • I edited the script to add this requirement Commented Sep 26, 2017 at 12:44
  • @HugoRoussaffa With your edit, you will select every features that intersects the selected features exactly the same way as in the question... You just replace the selected features by a temp layer but the features are exactly the same.
    – YoLecomte
    Commented Sep 26, 2017 at 15:02
  • @YoLecomte yes you totally right, big mistake ! With my method we need to create a QgsVectorLayer for each feat in selectFeatures... I will edit to match that. Thanks for your comment Commented Sep 26, 2017 at 15:12
  • @HugoRoussaffa Yep, most welcome ! But it remains a complicated way (copying feature in a temp layer and using processing) to achieve something simple that can be done without so much steps...
    – YoLecomte
    Commented Sep 26, 2017 at 15:19

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.