I'm using Python on QGIS to automatize some processes and it's raising an error.
here's a sample of my data :mydata
Here's my python code:
"""
1- boucle d'indentifier les tronçons dans un dossier(fichier shp gpkg)
2- création des input et output
3- soustration d'un tronçon du tracé
4- creation d'un buffer pour le tronçon
5- création d'un buffer pour le fichier résultant de la soustraction
6- vérification d'intersection entre le buffer du tronçon et le bufffer du fichier issu de la soustraction
7- ajustement de la largeur du buffer afin d'éviter l'intersection
"""
import os
import processing
from osgeo import ogr
#setting folders
TronconInputdirectory = 'C:/local_file/RHIN_LIBRE_PROJ/py_BAS/02-resultat/tronçons/'
ProfilOutputdirectory = 'C:/local_file/RHIN_LIBRE_PROJ/py_BAS/02-resultat/profil/'
demfile='C:/local_file/RHIN_LIBRE_PROJ/MNT/Affluents_Sauer_RhinNord_MNT_1m_L93_LiDAR_2018.gpkg'
trace='C:/local_file/RHIN_LIBRE_PROJ/py_BAS/01-donnée/trace/trace.gpkg'
traceWithoutTroncondirectory='C:/local_file/RHIN_LIBRE_PROJ/py_BAS/02-resultat/traceWithoutTronçon/'
BufferWithoutTronconOutputdirectory= 'C:/local_file/RHIN_LIBRE_PROJ/py_BAS/02-resultat/buffer/'
bufferTronconOutputdirectory='C:/local_file/RHIN_LIBRE_PROJ/py_BAS/02-resultat/bufferTroncon/'
# looking for gpkg et shp file
for filename in os.listdir(TronconInputdirectory):
if filename.endswith(".gpkg") or filename.endswith(".shp"):
troncon = (os.path.join(TronconInputdirectory, filename))
#set ouput name
name = filename
traceWithoutTroncon= traceWithoutTroncondirectory+'traceWithout'+(name)
BufferTraceWithoutTroncon = BufferWithoutTronconOutputdirectory+'Buffer_without'+(name)
bufferTroncon= bufferTronconOutputdirectory +'bufffer_'+ (name)
intersection = False #init intersection
largeur = 1 # setting fist largeur value
while intersection is False :
########## buffering tronçon
processing.run("native:buffer",\
{'INPUT':troncon,\
'DISTANCE': largeur,\
'SEGMENTS':5,\
'END_CAP_STYLE':0,\
'JOIN_STYLE':0,\
'MITER_LIMIT':1,\
'DISSOLVE':False,\
'OUTPUT':bufferTroncon})
##########removing tronçon from tracé
processing.run("native:difference",\
{'INPUT':trace,\
'OVERLAY':troncon,\
'OUTPUT':traceWithoutTroncon})
##########buffering traceWithoutTroncon
processing.run("native:buffer",\
{'INPUT':traceWithoutTroncon,\
'DISTANCE': largeur,\
'SEGMENTS':5,\
'END_CAP_STYLE':0,\
'JOIN_STYLE':0,\
'MITER_LIMIT':1,\
'DISSOLVE':False,\
'OUTPUT':BufferTraceWithoutTroncon})
driver = ogr.GetDriverByName("gpkg") #new
srcShp1 = driver.Open(BufferTraceWithoutTroncon)#new
srcLay1 = srcShp1.GetLayer()
for feat1 in srcLay1:
geom1 = feat1.GetGeometryRef()
driver = ogr.GetDriverByName("gpkg") #new
srcShp2 = driver.Open(bufferTroncon)#new
srcLay2 = srcShp2.GetLayer()
for feat2 in srcLay2:
geom2 = feat2.GetGeometryRef()
intersection = geom2.Intersects(geom1)
if intersection is False:
print("No")
largeur= largeur+1
else:
print("no")
And this is the error:
Traceback (most recent call last):
File "C:\PROGRA~1\QGIS3~1.8\apps\Python37\lib\code.py", line 90, in runcode
exec(code, self.locals)
File "<input>", line 1, in <module>
File "<string>", line 41, in <module>
File "C:/PROGRA~1/QGIS3~1.8/apps/qgis/./python/plugins\processing\tools\general.py", line 106, in run
return Processing.runAlgorithm(algOrName, parameters, onFinish, feedback, context)
File "C:/PROGRA~1/QGIS3~1.8/apps/qgis/./python/plugins\processing\core\Processing.py", line 181, in runAlgorithm
raise QgsProcessingException(msg)
_core.QgsProcessingException: Il y a eu des erreurs lors de l’exécution de l’algorithme.