I am facing an issue for the first time and can not manage to find out what's wrong?

I am trying to write a function that takes in input the line ID, finds the node at the end of the line. Then looks for the lines at the origin of the found node.

Def find(id):    
    canvas = qgis.utils.iface.mapCanvas()

    lyrs = dict((k.name(), i) for (i, k) in enumerate(canvas.layers()))
    LayerC = canvas.layer(lyrs['Lines'])
    LayerB = canvas.layer(lyrs['Nodes'])
    for line in LayerC.getFeatures():
       if line.id() == id:
          geom1 = line.geometry()
          xy_line = geom1.asPolyline()
          xy_O = xy_line[0]
          xy_E = xy_line[len(xy_line) - 1]

          listnodes = LayerB.getFeatures()
          for featN in listnodes:
            geomN = featN.geometry()
            xy_N = geomN.asPoint()

            if xy_N == xy_E:
                for line2 in LayerC.getFeatures():
                     geom2 = line2.geometry()
                     xy_line2 = geom2.asPolyline()
                     xy_O2 = xy_line2[0]
                     xy_E2 = xy_line2[len(xy_line2) - 1]

                     if xy_N == xy_O2:
        return list

The code crashes when iterating for the second time on LayerC. I am running this on Windows x64 with the version 2.14.3

Any suggestions ?

  • What do you mean by crash? Any python error or exception? Can you port a picture of your lines and nodes to better understand what you try to do – Hugo Roussaffa - GeoDatup Oct 1 '17 at 11:34
  • Hi, no error. The code stops running and Qgis freezes. – Nekkini Oct 1 '17 at 13:15
  • How do you develop your script? Are you using an exernal IDE as PyDev and the debug plugin of qgis ? Or are you developing as old style under python console ? – Hugo Roussaffa - GeoDatup Oct 1 '17 at 14:07
  • I use notepad++ and save as .py file. The objective is to build a plugin but first I test it from the python console – Nekkini Oct 1 '17 at 19:14
  • 1
    Yes I already tried that with no success. Finally, I decided to stock my attributes in a multidimensional list and iterate over it. – Nekkini Oct 1 '17 at 22:35

Your Answer

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

Browse other questions tagged or ask your own question.