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).
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----#
Answer
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.
No comments:
Post a Comment