2

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)
#----but it did not work----#
4

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 intersects the selected Feature for every selected features separetaly.

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

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

1

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

[edit] 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()
  • 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? – Jayme Muzzi Sep 26 '17 at 12:02
  • I edited the script to add this requirement – Hugo Roussaffa - GeoDatup Sep 26 '17 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 Sep 26 '17 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 – Hugo Roussaffa - GeoDatup Sep 26 '17 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 Sep 26 '17 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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