#1 Sun 19 August 2018 14:13
- laurent00
- Participant assidu
- Date d'inscription: 6 Aug 2007
- Messages: 402
QGIS 2.18: macro workflow
rebonjour
avec ce même forum j'ai réussi à construire une maro pour que certaines de mes couches s'enregistrent en format CVS lors de toutes les fermetures de QGIS 2.18
Code:
import os from qgis.core import QgsMapLayerRegistry, QgsVectorFileWriter from datetime import datetime layer_name1 = u'tourettes' layer_name2 = u'alignements' folder = u'/home/laurent/Documents/2. Ingall/IGHAZER/atlas/alignements/workflow' encoding = 'utf-8' suffix = datetime.now().strftime('%Y-%m-%d-%H-%M') def closeProject(): registry = QgsMapLayerRegistry.instance() layer1 = registry.mapLayersByName(layer_name1)[0] filename1 = layer_name1 filename1 = os.path.join(folder, filename1 + '-' + suffix + '.csv') QgsVectorFileWriter.writeAsVectorFormat(layer1, filename1, encoding, None, driverName='CSV',layerOptions=['GEOMETRY=AS_WKT']) registry = QgsMapLayerRegistry.instance() layer2 = registry.mapLayersByName(layer_name2)[0] filename2 = layer_name2 filename2 = os.path.join(folder, filename2 + '-' + suffix + '.csv') QgsVectorFileWriter.writeAsVectorFormat(layer2, filename2, encoding, None, driverName='CSV',layerOptions=['GEOMETRY=AS_WKT'])
mais il semble que celle-çi ne fonctionne plus et à l'ouverture de mon projet j'ai cette alerte
Code:
Une erreur est survenue lors de l'exécution du code suivant: qgis.utils.reloadProjectMacros() traceback.print_exception() failed Version de Python : Version de QGIS : 3.2.1-Bonn 'Bonn', 1edf372 Chemin vers Python :
merci d'une aide
LJ
Hors ligne
#2 Mon 27 August 2018 12:18
Re: QGIS 2.18: macro workflow
Bonjour,
Si vous réécrivez votre code dans la console python, cela fonctionne-t-il ?
Code:
import os from qgis.core import QgsMapLayerRegistry, QgsVectorFileWriter from datetime import datetime layer_name1 = u'tourettes' layer_name2 = u'alignements' folder = u'/home/laurent/Documents/2. Ingall/IGHAZER/atlas/alignements/workflow' encoding = 'utf-8' suffix = datetime.now().strftime('%Y-%m-%d-%H-%M') def closeProject(): registry = QgsMapLayerRegistry.instance() layer1 = registry.mapLayersByName(layer_name1)[0] filename1 = layer_name1 filename1 = os.path.join(folder, filename1 + '-' + suffix + '.csv') QgsVectorFileWriter.writeAsVectorFormat(layer1, filename1, encoding, None, driverName='CSV',layerOptions=['GEOMETRY=AS_WKT']) registry = QgsMapLayerRegistry.instance() layer2 = registry.mapLayersByName(layer_name2)[0] filename2 = layer_name2 filename2 = os.path.join(folder, filename2 + '-' + suffix + '.csv') QgsVectorFileWriter.writeAsVectorFormat(layer2, filename2, encoding, None, driverName='CSV',layerOptions=['GEOMETRY=AS_WKT']) closeProject() # on exécute la commande
Personnellement, j'ai testé votre code, et étrangement, pour moi aussi, il n'y a pas d'écriture de fichier sous QGIS 2.18
J'ai testé sous QGIS 3, avec le code suivant, et ça fonctionne, par contre :
Code:
import os from qgis.core import QgsVectorFileWriter, QgsProject, QgsCoordinateReferenceSystem from datetime import datetime layer_name1 = u'points' folder = 'C:/temp/points/' encoding = 'utf-8' suffix = datetime.now().strftime('%Y-%m-%d-%H-%M') def closeProject(): layer1 = QgsProject.instance().mapLayersByName(layer_name1)[0] filename1 = layer_name1 filename1 = os.path.join(folder, filename1 + '-' + suffix + '.csv') QgsVectorFileWriter.writeAsVectorFormat(layer1, filename1, encoding, QgsCoordinateReferenceSystem(2154), driverName='CSV',layerOptions=['GEOMETRY=AS_WKT'])
geodata au cerema et petits billets en géomatique
Hors ligne
#3 Mon 27 August 2018 14:17
- laurent00
- Participant assidu
- Date d'inscription: 6 Aug 2007
- Messages: 402
Re: QGIS 2.18: macro workflow
bon je regarde mieux ce soir
de mémoire sous 2.18 il fallait réouvrir 2 fois QGIS pour que le fichier se crée ...
LJ
Hors ligne
#4 Mon 27 August 2018 18:35
- laurent00
- Participant assidu
- Date d'inscription: 6 Aug 2007
- Messages: 402
Re: QGIS 2.18: macro workflow
en utilisant la console voici ce que cela ramène
Code:
Console Python Utilisez iface pour accéder à l'interface de l'API QGIS ou tapez help(iface) pour plus d'informations import os from qgis.core import QgsMapLayerRegistry, QgsVectorFileWriter Traceback (most recent call last): File "/usr/lib/python3.6/code.py", line 91, in runcode exec(code, self.locals) File "<input>", line 1, in <module> ImportError: cannot import name 'QgsMapLayerRegistry' from datetime import datetime layer_name1 = u'tourettes' layer_name2 = u'alignements' folder = u'/home/laurent/Documents/2. Ingall/IGHAZER/atlas/alignements/workflow' encoding = 'utf-8' suffix = datetime.now().strftime('%Y-%m-%d-%H-%M') def closeProject(): registry = QgsMapLayerRegistry.instance() layer1 = registry.mapLayersByName(layer_name1)[0] filename1 = layer_name1 filename1 = os.path.join(folder, filename1 + '-' + suffix + '.csv') QgsVectorFileWriter.writeAsVectorFormat(layer1, filename1, encoding, None, driverName='CSV',layerOptions=['GEOMETRY=AS_WKT']) registry = QgsMapLayerRegistry.instance() Traceback (most recent call last): File "/usr/lib/python3.6/code.py", line 64, in runsource code = self.compile(source, filename, symbol) File "/usr/lib/python3.6/codeop.py", line 168, in __call__ return _maybe_compile(self.compiler, source, filename, symbol) File "/usr/lib/python3.6/codeop.py", line 99, in _maybe_compile raise err1 File "/usr/lib/python3.6/codeop.py", line 87, in _maybe_compile code1 = compiler(source + "\n", filename, symbol) File "/usr/lib/python3.6/codeop.py", line 133, in __call__ codeob = compile(source, filename, symbol, self.flags, 1) File "<input>", line 8 registry = QgsMapLayerRegistry.instance() ^ IndentationError: unindent does not match any outer indentation level layer2 = registry.mapLayersByName(layer_name2)[0] Traceback (most recent call last): File "/usr/lib/python3.6/code.py", line 64, in runsource code = self.compile(source, filename, symbol) File "/usr/lib/python3.6/codeop.py", line 168, in __call__ return _maybe_compile(self.compiler, source, filename, symbol) File "/usr/lib/python3.6/codeop.py", line 99, in _maybe_compile raise err1 File "/usr/lib/python3.6/codeop.py", line 87, in _maybe_compile code1 = compiler(source + "\n", filename, symbol) File "/usr/lib/python3.6/codeop.py", line 133, in __call__ codeob = compile(source, filename, symbol, self.flags, 1) File "<input>", line 1 layer2 = registry.mapLayersByName(layer_name2)[0] ^ IndentationError: unexpected indent filename2 = layer_name2 Traceback (most recent call last): File "/usr/lib/python3.6/code.py", line 64, in runsource code = self.compile(source, filename, symbol) File "/usr/lib/python3.6/codeop.py", line 168, in __call__ return _maybe_compile(self.compiler, source, filename, symbol) File "/usr/lib/python3.6/codeop.py", line 99, in _maybe_compile raise err1 File "/usr/lib/python3.6/codeop.py", line 87, in _maybe_compile code1 = compiler(source + "\n", filename, symbol) File "/usr/lib/python3.6/codeop.py", line 133, in __call__ codeob = compile(source, filename, symbol, self.flags, 1) File "<input>", line 1 filename2 = layer_name2 ^ IndentationError: unexpected indent filename2 = os.path.join(folder, filename2 + '-' + suffix + '.csv') Traceback (most recent call last): File "/usr/lib/python3.6/code.py", line 64, in runsource code = self.compile(source, filename, symbol) File "/usr/lib/python3.6/codeop.py", line 168, in __call__ return _maybe_compile(self.compiler, source, filename, symbol) File "/usr/lib/python3.6/codeop.py", line 99, in _maybe_compile raise err1 File "/usr/lib/python3.6/codeop.py", line 87, in _maybe_compile code1 = compiler(source + "\n", filename, symbol) File "/usr/lib/python3.6/codeop.py", line 133, in __call__ codeob = compile(source, filename, symbol, self.flags, 1) File "<input>", line 1 filename2 = os.path.join(folder, filename2 + '-' + suffix + '.csv') ^ IndentationError: unexpected indent QgsVectorFileWriter.writeAsVectorFormat(layer2, filename2, encoding, None, driverName='CSV',layerOptions=['GEOMETRY=AS_WKT']) Traceback (most recent call last): File "/usr/lib/python3.6/code.py", line 64, in runsource code = self.compile(source, filename, symbol) File "/usr/lib/python3.6/codeop.py", line 168, in __call__ return _maybe_compile(self.compiler, source, filename, symbol) File "/usr/lib/python3.6/codeop.py", line 99, in _maybe_compile raise err1 File "/usr/lib/python3.6/codeop.py", line 87, in _maybe_compile code1 = compiler(source + "\n", filename, symbol) File "/usr/lib/python3.6/codeop.py", line 133, in __call__ codeob = compile(source, filename, symbol, self.flags, 1) File "<input>", line 1 QgsVectorFileWriter.writeAsVectorFormat(layer2, filename2, encoding, None, driverName='CSV',layerOptions=['GEOMETRY=AS_WKT']) ^ IndentationError: unexpected indent
en testant ton code qui marche l'erreur n'apparaît plus mais le fichier n'est pas créée et dans la console j'ai
Code:
Console Python Utilisez iface pour accéder à l'interface de l'API QGIS ou tapez help(iface) pour plus d'informations import os from qgis.core import QgsVectorFileWriter, QgsProject, QgsCoordinateReferenceSystem from datetime import datetime layer_name1 = u'alignements' folder = u'/home/laurent/Documents/2. Ingall/IGHAZER/atlas/alignements/workflow' encoding = 'utf-8' suffix = datetime.now().strftime('%Y-%m-%d-%H-%M') def closeProject(): layer1 = QgsProject.instance().mapLayersByName(layer_name1)[0] filename1 = layer_name1 filename1 = os.path.join(folder, filename1 + '-' + suffix + '.csv') QgsVectorFileWriter.writeAsVectorFormat(layer1, filename1, encoding, QgsCoordinateReferenceSystem(2154), driverName='CSV',layerOptions=['GEOMETRY=AS_WKT']) closeProject() Traceback (most recent call last): File "/usr/lib/python3.6/code.py", line 64, in runsource code = self.compile(source, filename, symbol) File "/usr/lib/python3.6/codeop.py", line 168, in __call__ return _maybe_compile(self.compiler, source, filename, symbol) File "/usr/lib/python3.6/codeop.py", line 99, in _maybe_compile raise err1 File "/usr/lib/python3.6/codeop.py", line 87, in _maybe_compile code1 = compiler(source + "\n", filename, symbol) File "/usr/lib/python3.6/codeop.py", line 133, in __call__ codeob = compile(source, filename, symbol, self.flags, 1) File "<input>", line 6 closeProject() ^ SyntaxError: invalid syntax
est-ce un problème de ma version pyhton avec QGIS3.2 ?
LJ
Dernière modification par laurent00 (Mon 27 August 2018 18:36)
Hors ligne
#5 Tue 28 August 2018 09:20
Re: QGIS 2.18: macro workflow
Bonjour Laurent,
c'est un problème d'indentation python. Il peut y avoir des séparateurs tab et whitespace mélangés, ce que python n'aime pas.
Le mieux est d'utiliser un éditeur de texte avec une option "tabify / untabify" pour être sûr. J'ai créé une demande de fonctionnalité pour avoir ça dans les zones d'édition python de QGIS, c'est un piège assez récurrent!
Régis
Hors ligne