#1 Wed 25 March 2020 15:07
- Floflo49fb
- Participant assidu
- Lieu: Montpellier
- Date d'inscription: 29 Aug 2009
- Messages: 250
- Site web
QGIS 3: Charger un plugin en python
Bonjour à tous,
J'utilise un script python en ligne de commande pour exécuter des exports de carte. Celui-ci fonctionne parfaitement toutefois j'ai un petit souci car mon projet utilise le plugin refFunctions qui n'est pas pris en compte.
Est-ce que vous avez une idée de comment charger et activer un plugin QGIS directement depuis un script python?
Mon code actuel
Code:
import sys
import os
os.environ['QT_QPA_PLATFORM']='offscreen'
from qgis.core import QgsApplication, QgsProject, QgsLayoutExporter, QgsLayoutRenderContext
def export_atlas(qgs_project_path, layout_name, outputs_folder):
# Open existing project
project = QgsProject.instance()
project.read(qgs_project_path)
print('Project in ' + project.fileName() + ' loaded successfully')
# Open prepared layout that as atlas enabled and set
layout = project.layoutManager().layoutByName(layout_name)
myAtlas = layout.atlas()
myAtlasMap = myAtlas.layout()
# Export atlas
exporter = QgsLayoutExporter(layout)
image_settings = QgsLayoutExporter(myAtlasMap).ImageExportSettings()
image_settings.dpi = 300
image_settings.generateWorldFile = 1
context = QgsLayoutRenderContext(layout)
context.setFlag(context.FlagAntialiasing, True)
image_settings.flags = context.flags()
exporter.exportToImage(myAtlas,outputs_folder, 'tiff', image_settings)
def main():
# Start a QGIS application without GUI
qgs = QgsApplication([], False)
qgs.initQgis()
project_path = '/home/PROJET.qgs'
output_folder = '/home/export/'
layout_name = 'ATLAS'
print('Starter atlas export')
export_atlas(project_path, layout_name, output_folder)
# Close the QGIS application
qgs.exitQgis()
if __name__ == "__main__":
main()Dernière modification par Floflo49fb (Thu 26 March 2020 09:53)
Florian Boret
Dream it, Make it, Share it
Hors ligne
#2 Thu 26 March 2020 16:57
- Floflo49fb
- Participant assidu
- Lieu: Montpellier
- Date d'inscription: 29 Aug 2009
- Messages: 250
- Site web
Re: QGIS 3: Charger un plugin en python
Pour vous tenir au courant, j'ai finalement trouvé une fonction interne à QGIS m'évitant de passer par le plugin refFunctions.
Mais je pense qu'il y avait un truc à creuser autour de ça :
Code:
complementos=['refFunctions']
for elemento in complementos:
if elemento not in plugins:
zip_ref = zipfile.ZipFile('/home/plugins/'+elemento+'.zip', 'r')
zip_ref.extractall('/usr/share/qgis/python/plugins')
zip_ref.close()
loadPlugin(elemento)
startPlugin(elemento)
#reloadPlugin(elemento)
try:
QSettings().setValue('/PythonPlugins/refFunctions','true')
except:
passSource : https://github.com/diezrabanos/QGIS_adm … SIGMENA.py
Florian Boret
Dream it, Make it, Share it
Hors ligne


