Pages: 1
- Sujet précédent - arcpy.management.SelectLayerByAttribute n'aime pas les boucles - Sujet suivant
#1 Thu 16 March 2023 16:03
- kulpinski.nicolas
- Membre
- Lieu: Marseille
- Date d'inscription: 25 Jan 2007
- Messages: 18
- Site web
arcpy.management.SelectLayerByAttribute n'aime pas les boucles
Bonjour à tous,
Afin de continuer en français le sujet sur les problèmes de mémoire traité sur le site ESRI.COM https://community.esri.com/t5/python-qu … d-p/580086 et parce qu'ils n'ont pas vraiment résolu ce problème, voici le mien.
Je dois faire une série d'itération (2209 exactement) avec une requête à chaque fois pour pouvoir tester si les données correspondant à cette requête existe ou pas et si elle existe, alors je fais un calcul sur la sélection.
Jusqu'ici rien de folichon, j'ai quand même réduit le nombre d'itération en regroupant mes requêtes par famille.
Sans regroupement par famille, mon script n'arrive pas au bout et plante après plus de 10 heures de traitement.
Avec le regroupement le script va au bout mais comme l'a constaté le sujet en anglais, il y a bien un ralentissement lorsqu'on utilise la fonction arcpy.management.SelectLayerByAttribute dans une boucle qui a pour effet de passer les requêtes de quelques milisecondes à quelques voire plusieurs longues secondes en fin de parcour du tableau.
Un script qui devrait être exécuté en 10 à 15 minutes maxi prend près de 6h à cause de ce BUG d'arcpy
Si vous avez des idées pour le contourner ?
Merci
Code:
if option_zone == "true" : env.workspace = (out_gdb_path) analyzone = "ANALYZONE" #Ajout du champ ANADENS try : arcpy.AddField_management(analyzone, "ANADENS", "TEXT", "", "", 30, "Analyse de densification", "NULLABLE", "") arcpy.AddMessage("Ajout du champ ANADENS à la couche ANALYZONE") except: arcpy.AddMessage("Ajout IMPOSSIBLE du champ ANADENS à la couche ANALYZONE") ######### ouverture et lecture du fichier f=open(analyzone_csv,'r') entete = f.readline().rstrip('\n\r')# Lit l'en-tete entete_separee = entete.split(";") ligne= f.readline() #soit f le pointeur vers votre fichier csv, while ligne!='' : # soit tant que la ligne n'est pas vide ligne = ligne.replace("\n","") ligne_separee = ligne.split(";") # entre guillemet le separateur ici un ; #couche de base tabsize = len(ligne_separee) for i in range(tabsize): if (i>0): code_base = ligne_separee [0] code_base_fam = code_base[0:2] code_evo = entete_separee [i] code_evo_fam = code_evo[0:2] code_evo_detail = code_evo[-2:] code_comp = ligne_separee [i] if (code_base_fam == code_evo_fam) : sql_fdm = "FORMDOMI = '"+code_base+"' AND FORMDOMI_1 = '"+code_evo+"'" analyzone_select = arcpy.management.SelectLayerByAttribute(analyzone, 'NEW_SELECTION', sql_fdm) arcpy.AddMessage("Selection dans ANALYZONE avec la requete "+ sql_fdm) #test pour savoir si la selection n'est pas vide if int(arcpy.GetCount_management(analyzone_select)[0]) > 0: arcpy.management.CalculateField(in_table=analyzone_select, field="ANADENS", expression="'"+code_comp+"'", expression_type="PYTHON3", code_block="", field_type="TEXT", enforce_domains="NO_ENFORCE_DOMAINS")[0] arcpy.AddMessage("Calcul du champ ANADENS avec la valeur "+code_comp+"") else : arcpy.AddMessage("La selection est vide") del analyzone_select gc.collect() else : if (code_evo_detail == '00'): sql_fdm = "FORMDOMI = '"+code_base+"' AND FORMDOMI_1 LIKE '"+code_evo_fam+"%'" analyzone_select = arcpy.management.SelectLayerByAttribute(analyzone, 'NEW_SELECTION', sql_fdm) arcpy.AddMessage("Selection de la famille "+code_evo_fam+" dans ANALYZONE avec la requete "+ sql_fdm) #test pour savoir si la selection n'est pas vide if int(arcpy.GetCount_management(analyzone_select)[0]) > 0: arcpy.management.CalculateField(in_table=analyzone_select, field="ANADENS", expression="'"+code_comp+"'", expression_type="PYTHON3", code_block="", field_type="TEXT", enforce_domains="NO_ENFORCE_DOMAINS")[0] arcpy.AddMessage("Calcul du champ ANADENS avec la valeur "+code_comp+"") else : arcpy.AddMessage("La selection est vide") del analyzone_select gc.collect() else : arcpy.AddMessage("Requête traitée dans la famille "+code_evo_fam+" précédente") ligne= f.readline() #valeur de la ligne suivante f.close()
et voici le fichier csv parcouru par le script :
Code:
xxxx;0000;0100;0101;0102;0103;0104;0105;0106;0107;0108;0109;0200;0201;0202;0203;0300;0400;0401;0402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;CD;CD;CD;CD;CD;CD;CD;CD;N;N;N;C;C;C;C;N 0900;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;C;C;C;C;C;C;C;C;C;N;N;N;N;N 0901;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;C;C;C;C;C;C;C;C;C;N;N;N;N;N 0902;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;C;C;C;C;C;C;C;C;C;N;N;N;N;N 0903;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;CD;C;C;C;C;C;C;C;C;C;N;N;N;N;N 9900;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N
Dernière modification par kulpinski.nicolas (Hier 07:54)
Hors ligne
Pages: 1
- Sujet précédent - arcpy.management.SelectLayerByAttribute n'aime pas les boucles - Sujet suivant