My inputs: line shapefile with features inside and polygon shapefile with other features inside. What I am trying to achieve is to select from line feature every feature which is inside each polygon: if yes, it is added to the new column unique number.
So far I managed to create this script:
from PyQt4.QtCore import QVariant cLayer = iface.mapCanvas().currentLayer() bLayer = QgsMapLayerRegistry.instance().mapLayersByName("Mean coordinates") cLayer.dataProvider().addAttributes( [ QgsField("ID", QVariant.Int) ] ) cLayer.updateFields() idx = cLayer.dataProvider().fieldNameIndex( "ID") selection =  n=1 for pt in cLayer.getFeatures(): point = pt.geometry() for pol in bLayer.getFeatures(): pol = pol.geometry() if pol.contains(point): cLayer.setAttribute(n, "ID") n+=1
It creates ID field, but my ID column is not updating. I am also not sure if my "select by location" query for each individual polygons works. Can you help me?