QGIS Plugins mit Python: Unterschied zwischen den Versionen

Aus Geoinformation HSR
Wechseln zu: Navigation, Suche
K (Allgemeines)
K (Writing QGIS Plugins)
 
(19 dazwischenliegende Versionen von 2 Benutzern werden nicht angezeigt)
Zeile 1: Zeile 1:
 
'''QGIS Plugins mit Python - PyQGIS'''
 
'''QGIS Plugins mit Python - PyQGIS'''
  
Siehe auch: [[QGIS for Devs]], [[QGIS]], [[QGIS - Tipps und Tricks]], [[Python]] (QGIS nutzt Python 2.7)
+
Siehe auch: [[QGIS for Devs]], [[QGIS]], [[QGIS - Tipps und Tricks]], [[Python]] (QGIS 2 nutzt Python 2.7 QGIS 3 Python 3.5), ([https://etherpad.wikimedia.org/p/QGIS Etherpad])
  
 
== Allgemeines ==
 
== Allgemeines ==
Zeile 7: Zeile 7:
 
Wichtige Informationen:
 
Wichtige Informationen:
 
* Dokumentation/Tutorials: [http://www.qgis.org/pyqgis-cookbook/intro.html#python-applications PyQGis Cookbook] (QGIS Documentation), insbesondere [http://www.qgis.org/pyqgis-cookbook/plugins.html Kap. 'Developing Python Plugins'] (PyQGIS, QT4, Eclipse, Eclipse PyDev)
 
* Dokumentation/Tutorials: [http://www.qgis.org/pyqgis-cookbook/intro.html#python-applications PyQGis Cookbook] (QGIS Documentation), insbesondere [http://www.qgis.org/pyqgis-cookbook/plugins.html Kap. 'Developing Python Plugins'] (PyQGIS, QT4, Eclipse, Eclipse PyDev)
* Diagram about main QGIS Python API classes: http://labs.webgeodatavore.com/partage/diagramme_principal.html
+
* Diagram about main QGIS Python API classes: http://labs.webgeodatavore.com/partage/diagramme_principal.html ([https://github.com/webgeodatavore/qgis-class-diagram Sources])
* QGIS-Python-Plugin-Verzeichnis: C:\Documents and Settings\<username>\.qgis\python\plugins (Windows).
+
* QGIS-Python-Plugin-Verzeichnis: C:\Documents and Settings\<username>\.qgis2\python\plugins (Windows).
 
* QTDesigner unter Windows ist nun Bestandteil von der QT IDE [http://qt.nokia.com/products/ QTCreator].  
 
* QTDesigner unter Windows ist nun Bestandteil von der QT IDE [http://qt.nokia.com/products/ QTCreator].  
  
QGIS Plugins mit Python:
+
QGIS Plugins mit Python: Siehe [[#Writing_QGIS_Plugins]]
* Documentation zum Schreiben von PyQGIS-Plugins:
+
 
** QGIS Manual - Guide: http://qgis.org/en/documentation/manuals.html > QGIS Coding and Compilation Guide
+
Tools:
** Forum: http://forum.qgis.org/viewforum.php?f=5
+
* QGIS Python Plugin Builder: http://pyqgis.org/builder/plugin_builder.py
** Wiki-Artikel: http://www.qgis.org/wiki/Writing_Python_Plugins
+
* From the QGIS-dev mailing list: [http://osgeo-org.1560.x6.nabble.com/A-common-set-of-functions-for-QGIS-plugins-td5233908.html "A common set of functions"] by Victor Olaya, Nathan Woodrow
** Community Support: http://gis.stackexchange.com/questions/tagged/pyqgis
 
* Tutorials / Bücher:
 
** Book [http://pyqgis.com The PyQGIS Programmer's Guide]
 
** [http://blog.qgis.org/node/59 Quantum GIS Blog]
 
** [http://www.qgis.org/wiki/Writing_Python_Plugins#How_to_debug_a_plugin_using_PDB How to debug a plugin using PDB] on QGIS Wiki (for C++ on Linux see [http://www.qgis.org/wiki/How_to_debug_QGIS_Plugins How to debug QGIS pugins])
 
** ''Rapid GUI Programming with Python and Qt'', Summerfield, Mark - Gutes Buch zu PyQt 4, Python und QT wird auch erklärt.
 
* Tools:
 
** QGIS Python Plugin Builder: http://pyqgis.org/builder/plugin_builder.py
 
** From the QGIS-dev mailing list: [http://osgeo-org.1560.x6.nabble.com/A-common-set-of-functions-for-QGIS-plugins-td5233908.html "A common set of functions"] by Victor Olaya, Nathan Woodrow
 
  
 
Manuelles Installieren eines Python-Plugins myPlugin:
 
Manuelles Installieren eines Python-Plugins myPlugin:
Zeile 31: Zeile 22:
 
* Den Ordner myPlugin ins QGIS-Python-Plugin-Verzeichnis kopieren.
 
* Den Ordner myPlugin ins QGIS-Python-Plugin-Verzeichnis kopieren.
 
* QGIS (neu) starten und "Erweiterungen verwalten..." wählen. Dort sollte ein neuer Eintrag stehen => ankreuzen.
 
* QGIS (neu) starten und "Erweiterungen verwalten..." wählen. Dort sollte ein neuer Eintrag stehen => ankreuzen.
 +
 +
== Writing QGIS Plugins ==
 +
 +
Documentation zum Schreiben von PyQGIS-Plugins:
 +
* '''[https://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/ PyQGIS Developer Cookbook] revision 2020'''
 +
* '''[https://qgis.org/pyqgis/master/ QGIS API]''' (derived from [https://qgis.org/api/index.html QGIS C++ API], formerly [http://geoapis.sourcepole.com/qgispyapi/ by Pirmin Kalberer] and [http://qgis-python.kartoza.com/docs/ by Tim Sutton]).
 +
* [https://raw.githubusercontent.com/webgeodatavore/qgis-class-diagram/master/diagramme_principal.png API Overview/Poster] by Thomas Gratier
 +
 +
Dann:
 +
* QGIS Manual - Guide: http://qgis.org/en/documentation/manuals.html > QGIS Coding and Compilation Guide
 +
* Forum: http://forum.qgis.org/viewforum.php?f=5
 +
* Wiki-Artikel: http://www.qgis.org/wiki/Writing_Python_Plugins
 +
* Community Support: http://gis.stackexchange.com/questions/tagged/pyqgis
 +
* Tutorials / Bücher:
 +
** Book [http://pyqgis.com The PyQGIS Programmer's Guide]
 +
** [http://blog.qgis.org/node/59 Quantum GIS Blog]
 +
** [http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/ide_debugging.html How to debug a plugin using PDB] on QGIS Wiki (for C++ on Linux see [http://www.qgis.org/wiki/How_to_debug_QGIS_Plugins How to debug QGIS pugins])
 +
** ''Rapid GUI Programming with Python and Qt'', Summerfield, Mark - Gutes Buch zu PyQt 4, Python und QT wird auch erklärt.
 +
** Building a plugin for QGIS http://gis.ucar.edu/building-plugin-qgis
 +
 +
== Testing QGIS Plugins ==
 +
 +
* Writing Uni Tests for QGIS Plugins: http://snorf.net/blog/2014/01/04/writing-unit-tests-for-qgis-python-plugins/
 +
* Testen von Qt-GUI Komponenten: http://www.voom.net/pyqt-qtest-example
 +
* Neues Python Modul "qgis.testing" (4.2.2016): [http://www.opengis.ch/2016/02/04/increasing-the-stability-of-processing-algorithms/]
  
 
== Plattform-unabhängiges Programmieren ==
 
== Plattform-unabhängiges Programmieren ==
Zeile 164: Zeile 180:
 
; Weiterführende Informationen
 
; Weiterführende Informationen
 
: remote-debugging: http://pydev.org/manual_adv_remote_debugger.html
 
: remote-debugging: http://pydev.org/manual_adv_remote_debugger.html
 
== Testing von PyQT Apps ==
 
 
* Writing Uni Tests for QGIS Plugins: http://snorf.net/blog/2014/01/04/writing-unit-tests-for-qgis-python-plugins/
 
* Testen von Qt-GUI Komponenten: http://www.voom.net/pyqt-qtest-example
 
  
 
== How to publish a plugin ==
 
== How to publish a plugin ==
Zeile 183: Zeile 194:
  
 
Upload:
 
Upload:
* In order to upload a plugin on the official QGIS page a OSGeo ID is required. It can be created here: https://www.osgeo.org/cgi-bin/ldap_create_user.py
+
* In order to upload a plugin on the official QGIS page a OSGeo ID (user) is required (e.g. user account "geometalab"). The user account can be created here: https://www.osgeo.org/cgi-bin/ldap_create_user.py
 
* On the page https://plugins.qgis.org/ login with your OSGeo ID.
 
* On the page https://plugins.qgis.org/ login with your OSGeo ID.
 
* The plugin (ZIP archive) can be uploaded here: https://plugins.qgis.org/plugins/add/ Check experimental field if the plugin still is experimental.
 
* The plugin (ZIP archive) can be uploaded here: https://plugins.qgis.org/plugins/add/ Check experimental field if the plugin still is experimental.
 
* After uploading the plugin it won't be immediately available in the plugin list. It first has to get approved. The approval can take up to 2 weeks. Check "How to add your plugin to this repository" on https://plugins.qgis.org/ for the criteria the plugin has to meet in order to get approved.
 
* After uploading the plugin it won't be immediately available in the plugin list. It first has to get approved. The approval can take up to 2 weeks. Check "How to add your plugin to this repository" on https://plugins.qgis.org/ for the criteria the plugin has to meet in order to get approved.
  
[[Kategorie:Geoprocessing]] [[Kategorie:QGIS]] [[Kategorie:Python]] [[Kategorie:Programmieren]]
+
[[Kategorie:Geoprocessing]] [[Kategorie:QGIS]] [[Kategorie:Python]] [[Kategorie:Programmieren]] [[Kategorie:QGIS-Plugin]]

Aktuelle Version vom 1. Juni 2020, 15:19 Uhr

QGIS Plugins mit Python - PyQGIS

Siehe auch: QGIS for Devs, QGIS, QGIS - Tipps und Tricks, Python (QGIS 2 nutzt Python 2.7 QGIS 3 Python 3.5), (Etherpad)

Allgemeines

Wichtige Informationen:

QGIS Plugins mit Python: Siehe #Writing_QGIS_Plugins

Tools:

Manuelles Installieren eines Python-Plugins myPlugin:

  • Download myPlugin.zip.
  • Auspacken von myPlugin in einem temporären Verzeichnis; nun sollte ein Ordner myPlugin/ vorhanden sein.
  • Den Ordner myPlugin ins QGIS-Python-Plugin-Verzeichnis kopieren.
  • QGIS (neu) starten und "Erweiterungen verwalten..." wählen. Dort sollte ein neuer Eintrag stehen => ankreuzen.

Writing QGIS Plugins

Documentation zum Schreiben von PyQGIS-Plugins:

Dann:

Testing QGIS Plugins

Plattform-unabhängiges Programmieren

Auf was ist bei der plattform-unabhängigen Programmieren zu achten?

UX Guidelines

Siehe QGIS for Devs#UX Guidelines (GUI)

Eclipse/PyDev

Wie kann man Eclipse mit PyDev zum Entwickeln von QGIS-Plugins (QT4) aufsetzen inklusive Unit-Testing und Debugging (gem. [2] )?

Installation Eclipse

  • Download minimalstes Eclipse 3.x, sprich nur Eclipse Platform Runtime Binary, welches leider nicht direkt auf der Hauptseite verfügbar ist
  1. http://download.eclipse.org/eclipse/downloads/eclipse3x.php
  2. -> Latest Release
  3. Zur Platform Runtime Binary Sektion navigieren und entsprechenden Download starten
  4. Installation

Einrichten Eclipse

  • Installation/Einrichten des Eclipse Marketplace
  1. Eclipse starten
  2. Help->Install New Software
  3. Indigo - http://download.eclipse.org/releases/indigo als Quelle auswählen
  4. Im Filter Feld Market eingeben
  5. Marketplace Client auswählen und installieren
  • Installation PyDev
  1. Help->Eclipse Marketplace...
  2. Im Suchfeld nach pydev suchen
  3. PyDev - Python IDE for Eclipse installieren
  4. Konfiguration nach Eclipse Neustart:
  5. Window->Preferences
  6. PyDev->Interpreter - Python für Linux oder PyDev->Interpreter - Iron Python und dann Auto Config wählen
Hinweis
PyDev und den eingebauten Debugger kennen lernen: http://www.vogella.de/articles/Python/article.html
  • Optional: Sourcecontrol EGit einrichten
  1. Help->Eclipse Marketplace...
  2. Im Suchfeld nach egit suchen
  3. EGit - Git Team Provider auswählen und installieren
  • CDT
  1. Help->Install New Software
  2. Indigo - http://download.eclipse.org/releases/indigo als Quelle auswählen
  3. In der Kategorie Programming Languages C/C++ Development Tools auswählen und installieren
  • Python und QT4
  1. Folgende Pakete müssen für Linux, am Beispiel Ubuntu, installiert werden um mit qt4 und sqlite arbeiten zu können
    1. python-qt4-sql
    2. python-qt4-dev
    3. libqt4-dev
    4. libqt4-sql-sqlite
    5. qt4-dev-tools
    6. qt4-doc-html
    7. pyqt4-dev-tools
  • Installation Eclipse Plugin
  1. Download von http://qt.nokia.com/products/eclipse-integration/
Hinweis
Um die qt-Umgebung, sprich Designer etc. kennen zu lernen, ist das eingebaute Eclipse-CheatSheet sehr gut geeignet. Eclipse->Help->Cheat Sheets...
Ein Wiki mit vielen Tutorials gibts hier: http://diotavelli.net/PyQtWiki/Tutorials

Eine alternative Anleitung zur Installation gibts hier: http://popdevelop.com/2010/04/setting-up-ide-and-creating-a-cross-platform-qt-python-gui-application/

  • QGIS installieren
  1. Nach Anleitung von http://www.qgis.org/wiki/Download vorgehen
  2. Zur Entwicklung libqgis-dev zusätzlich installieren

Debugging von Python-QGIS-Plugins mit Eclipse

Warum ist das Debugging von Plugins nicht so einfach wie bei einem Python-Programm?
Grund dafür ist, dass bei Plugins ein weiterer Prozess, also nicht unser Plugin-Code, die Lebensdauer unseres Codes bestimmt. Das heisst, dass wir in den wenigsten Fällen direkt in den Programmcode des 'Host'-Prozesses eingreifen können um an der Stelle zu warten, an welcher unser Plugin zum Einsatz kommt.
Wie verbinden wir nun den Eclipse-PyDev-Debugger und ein QGIS-Plugin?
Die Lösung die PyDev hierzu anbietet, verwendet einen sogenannten Remote-Debugger. Dabei agiert Eclipse-PyDev als Server, welcher auf einem bestimmten Port (Default ist 5678) auf eine Verbindung eines entsprechend ausgerüsteten Python-Programmes wartet. Sobald die Verbindung zu Stande gekommen ist können wir wie gewohnt debuggen.

Remote-Debugger / Debug Server

Als erstes wechseln wir in die Debug Perspektive von Eclipse. Dadurch erhalten wir zwei zusätzliche Symbole im Toolbar
Toolbar Icons
, welche uns erlauben den Remote-Debugger zu Starten/Stoppen. Alternativ dazu können wir den Debug-Server im Menu Pydev->Start/End Debug Server kontrollieren. Wir starten nun den Remote-Debugger durch anklicken des entsprechenden Symbols oder im Menu Pydev->Start Debug Server. In der Konsole wird nun ausgegeben
Console output
, auf welchem Port der Debug Server auf eine Verbindung wartet. Der Port kann in den allgemeinen PyDev Einstellungen unter Debug angepasst werden.

Anpassungen im Python-Plugin Code

Damit unser Plugin mit dem Debug Server verbinden kann müssen wir den folgenden Code einbauen, am besten direkt nach den üblichen Module Imports von Python

#...

import os
from os import *
pydevPysrcPath = os.environ.get('ECLIPSE_PYSRC_PATH', '')
if pydevPysrcPath:
    if os.path.exists(pydevPysrcPath):
        import sys
        sys.path.append(pydevPysrcPath)
        import pydevd
        pydevd.settrace()
    else:
        print "Specified ECLIPSE_PYSRC_PATH [%s] does not exist" % (pydevPysrcPath)

#...

Damit haben wir die Möglichkeit den Eclipse-Debug-Server nur dann zu kontaktieren, wenn wir auch die entsprechende Umgebungsvariable ECLIPSE_PYSRC_PATH gesetzt haben. Dies funktioniert natürlich nur dann, wenn wir auch die 'Host'-Applikation aus dieser Konsole starten.

Beispielaufruf in der Konsole (Linux)
~$ export ECLIPSE_PYSRC_PATH=/opt/eclipse/plugins/org.python.pydev.debug_2.2.3.2011100616/pysrc/
~$ qgis &
Beispiel (Windows)
  • Die Umgebungsvariable ECLIPSE_PYSRC_PATH setzen und QGIS starten.
  • Dazu gibt es zwei Möglichkeiten: 1. Batchdatei mit folgendem Inhalt (dann diese Batchdatei ausführen):
set ECLIPSE_PYSRC_PATH=/opt/eclipse/plugins/org.python.pydev.debug_2.2.3.2011100616/pysrc/" 
qgis.exe
  • oder ECLIPSE_PYSRC_PATH dauerhaft als Systemvariable eintragen und QGIS mittels Doppelklick auf Icon - oder aus dem Startmenü- starten.
Weiterführende Informationen
remote-debugging: http://pydev.org/manual_adv_remote_debugger.html

How to publish a plugin

Preparation:

  • Check if all attributes in the metadata.txt file are in order:
    • The version needs to be different to the previous one in order for the upload to work.
    • If needed change the experimental flag from true to false. Experimental plugins won't be shown in the QGIS plugin list by default.
    • It is recommended to have an Icon set for the plugin. Its max. size does not seem to be specified.
  • Remove all files from the directory you want to upload that are not needed for the plugin to run(e.g. *.pyc files, files and folders from IDEs, .gitignore etc.).
  • File names can only contain ASCII characters in order for the upload to work. Characters like Umlauts in file names will cause an Error when uploading.
  • The plugin directory can't contain hyphens in its name (if it does the plugin might still work in QGIS but it will show an error message).
  • ZIP your plugin directory.

Upload:

  • In order to upload a plugin on the official QGIS page a OSGeo ID (user) is required (e.g. user account "geometalab"). The user account can be created here: https://www.osgeo.org/cgi-bin/ldap_create_user.py
  • On the page https://plugins.qgis.org/ login with your OSGeo ID.
  • The plugin (ZIP archive) can be uploaded here: https://plugins.qgis.org/plugins/add/ Check experimental field if the plugin still is experimental.
  • After uploading the plugin it won't be immediately available in the plugin list. It first has to get approved. The approval can take up to 2 weeks. Check "How to add your plugin to this repository" on https://plugins.qgis.org/ for the criteria the plugin has to meet in order to get approved.