1

When I run the script below in Python Terminal at QGIS 2.18.17 (Linux Mint 19) it always crash after doing 73 iterations at the 2nd for loop from the script. Could anyone explain why this happens and could suggest how can I fix it?

For now, the way that I found to deal with this problem is to close QGIS, open it and run the script again from the item that it stopped. But when you have more than 2000 itens in a list of a for loop this can be quite annoying.

import processing
import os
import pandas as pd
wd = "/home/tiago/Documentos/PRIM_Mineracao/mata_atlantica/up_centroides/split"
lc = list()
for file in os.listdir(wd):
    if file.endswith(".shp"):
        lc.append (wd+"/"+file)
drenagem = QgsVectorLayer('/home/tiago/Documentos/PRIM_Mineracao/mata_atlantica/redes_drenagem/rio_7.shp', '','ogr')
fim = QgsVectorLayer('/home/tiago/Documentos/PRIM_Mineracao/mata_atlantica/fozes/foz_1.shp', '','ogr')
ext = drenagem.extent()
xmin = ext.xMinimum()
xmax = ext.xMaximum()
ymin = ext.yMinimum()
ymax = ext.yMaximum()
coords = "%f,%f,%f,%f" %(xmin, xmax, ymin, ymax)
ups = "/home/tiago/Documentos/PRIM_Mineracao/mata_atlantica/up/up_mata.shp"
pasta_intermediarios = "/home/tiago/Documentos/PRIM_Mineracao/mata_atlantica/v_net_distance"
p = 1
## 2nd FOR LOOP ##
for pto in lc:
    nome = os.path.basename(pto)
    nome = nome.replace(".shp","")
    nome = nome.replace("up_centroide_diss_COD_","")
    v_dist= pasta_intermediarios+"/v_dist_"+nome+".shp"
    disol= pasta_intermediarios+"/v_disol_"+nome+".shp"
    explo= pasta_intermediarios+"/v_explo_"+nome+".shp"
    points= pasta_intermediarios+"/v_points_"+nome+".shp"
    inter = pasta_intermediarios+"/v_inter_"+nome+".shp"
    processing.runalg("grass7:v.net.distance",drenagem,pto,fim,50,2,"","",0,"","",None,None,None,False,coords,-1,0.0001,0,v_dist)
    processing.runalg("qgis:dissolve",v_dist,True,None,disol)
    processing.runalg("qgis:explodelines",disol,explo)
    processing.runalg("saga:convertlinestopoints",explo,True,0.004,points)
    processing.runalg("qgis:intersection",points,ups,False,inter)
    inter_shp = QgsVectorLayer(inter,'',"ogr")
    list_up = list()
    for feature in inter_shp.getFeatures():
        up = int(feature["UP_UC"])
        if up not in list_up:
            list_up.append (up)
    jusante = list_up[:]
    del jusante[0]
    jusante.append(0)
    df = pd.DataFrame(jusante,list_up)
    nome = os.path.basename(pto)
    nome = nome.replace(".shp",".csv")
    nome = nome.replace("up_centroide_diss_COD_","")
    df.to_csv('/home/tiago/Documentos/PRIM_Mineracao/mata_atlantica/tabelas_jusante/'+nome)
    path = os.path.dirname(v_dist)
    files_to_del = []
    for r, d, f in os.walk(path):
        for file in f:
            files_to_del.append(os.path.join(r, file))
    len(files_to_del)
    for file in files_to_del:
        os.remove(file)
    os.remove(pto)
    print p, "de", len(lc)
    print nome,"done"
    p = p+1

This is the message that I get.

Unable to execute algorithm
Wrong parameter value: /home/tiago/Documentos/PRIM_Mineracao/mata_atlantica/v_net_distance/v_dist_UP_764949.shp
Unable to execute algorithm
Wrong parameter value: /home/tiago/Documentos/PRIM_Mineracao/mata_atlantica/v_net_distance/v_disol_UP_764949.shp
Unable to execute algorithm
Wrong parameter value: /home/tiago/Documentos/PRIM_Mineracao/mata_atlantica/v_net_distance/v_explo_UP_764949.shp
Unable to execute algorithm
Wrong parameter value: /home/tiago/Documentos/PRIM_Mineracao/mata_atlantica/v_net_distance/v_points_UP_764949.shp
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/tmp/tmp2K40DP.py", line 42, in <module>
    del jusante[0]
IndexError: list assignment index out of range
  • Looks like inter_shp.getFeatures() returns nothing for one of your items - empty intersection in earlier step? – smiller Jun 9 at 15:22
  • But there is an intersection, because when I close Qgis after this error, reopen it, the script runs from the item where it had stopped normally. @smiller. – tiago Jun 9 at 20:13
  • Please include the result of diagnostic print statements in your code. My traceback: jusante[0] doesn't exist. jusante is copy of list_up. list_up created by inter_shp.getFeatures(). inter_shp is a QgsVectorLayer of inter, which is from the intersection process. – smiller Jun 10 at 13:18
1

After taking a look at a similar question:

How to close open files after processing.runlag() command (Qgis: Error No 24)

And at this site:

https://docs.qgis.org/2.18/en/docs/user_manual/preamble/features.html#python-console

I could manage it setting the number of files opened by process using ulimit command at Linux terminal before opening Qgis at the same terminal.

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.