4

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")[0]

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?

  • If you just want to add an ID number, you don't need to select any features. Just identify those lines which lie inside the polygon :) – Joseph May 12 '17 at 15:11
  • Your right!, But for this case I need to assign different number for each individual polygon. – Losbaltica May 12 '17 at 15:14
  • So is something like first give unique ID for polygons and then do spatial join with lines and polygons to assign poly ID to lines – Losbaltica May 12 '17 at 15:16
  • I think you should edit your question to include exactly what it is you want to do. From the current question, I assumed you only wanted to add an ID field with unique values to the line layer. But you want to add an ID field to both polygon and line layers and then assign the unique value from the polygon to all lines which lie inside it? – Joseph May 12 '17 at 15:19
  • No, i what to add ID only for line layer which are inside each polygon. The method in comment is just a work around without scripting. – Losbaltica May 12 '17 at 15:21
4

You're close! Try replacing the following:

  • pol = pol.geometry() with poly = pol.geometry()
  • cLayer.setAttribute(n, "ID") with cLayer.changeAttributeValue(pt.id(), idx, n)
  • You will also have to edit the layer before committing any changes so we can use a while like with edit(cLayer):

The following works for me:

from PyQt4.QtCore import QVariant

cLayer = iface.mapCanvas().currentLayer()
bLayer = QgsMapLayerRegistry.instance().mapLayersByName("polygon example_2")[0]

cLayer.dataProvider().addAttributes( [ QgsField("ID", QVariant.Int) ] )
cLayer.updateFields()
idx = cLayer.dataProvider().fieldNameIndex( "ID")
n=1
for pt in cLayer.getFeatures():
    point = pt.geometry()
    for pol in bLayer.getFeatures():
        poly = pol.geometry()
        if poly.contains(point):
            with edit(cLayer):
                cLayer.changeAttributeValue(pt.id(), idx, n)
    n+=1

Note: Watch where you place n as it increases depending on the number of features read before it.

  • Thanks for answer! Is adding attribute but only for the last feature... – Losbaltica May 12 '17 at 15:12
  • @Losbaltica - Hmm can't replicate your issue, it seems to work fine for me... – Joseph May 12 '17 at 15:16

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.