QGIS for Devs: Unterschied zwischen den Versionen

Aus Geometa Lab OST
Zur Navigation springen Zur Suche springen
K (Instruktion zu IDE hinzugefügt)
(update whole giswiki)
Zeile 1: Zeile 1:
QGIS for Devs
See also:
See also:
* [[QGIS Plugins mit Python]], [[Eigene GIS-Fachapplikationen mit QGIS 2 erstellen]], [[QGIS]], [[Python]]
* [http://dev.ifs.hsr.ch/ Developer Wiki for Python Programming for (Q)GIS ('python4gis')]
* [http://dev.ifs.hsr.ch/ Developer Wiki for Python Programming for (Q)GIS ('python4gis')]


== Setting up a QGIS IDE ==
== Setting up a QGIS IDE ==
The [https://code.visualstudio.com/ Visual Studio Code IDE] is a free, and lightweight editor with advanced debugging tools, including extensions tailored for Python and QGIS development. It also supports version control and code formatting, making plugin development more efficient.


There are several ways to set up a QGIS plugin in an IDE, but we recommend the following approach, as this is the method we have successfully used ourselves.
There is the [https://plugins.qgis.org/plugins/plugin_reloader/ Plugin Reloader] plugin to easily apply your code changes without restarting QGIS.
 
This greatly speeds up debugging and iteration, making development smoother.
Before setting up the plugin development environment, ensure you have the following installed:
 
* Git (for cloning repositories)
* VSCode (as the development environment)


In QGIS, install the [https://plugins.qgis.org/plugins/plugin_reloader/ Plugin Reloader] plugin to easily apply your code changes without restarting QGIS.


The development cycle:
The plugin development cycle:
* Modify the Python files of the AIAMAS plugin.
* Modify the Python files of the AIAMAS plugin.
* Save changes in VSCode.
* Save changes in VSCode.
Zeile 20: Zeile 18:




Developers can debug their QGIS plugin using different methods:


* print() for basic console output,
== Manual installation of a python plugin ==
* [https://qgis.org/pyqgis/3.34/core/QgsMessageLog.html qgsMessageLog.logMessage()] to log messages inside QGIS,
 
* [https://github.com/gruns/icecream icecream (ic())] for more advanced debugging with variable inspection.
Die manuelle Installation eines Python-Plugins, das sich noch nicht in einem Repository befindet, passiert wie folgt:
 
Clone das Repository oder erstelle ein Plugin an einem beliebigen Ort.
Plugins are stored in QGIS by default in the folder python\plugins. This folder is located within the user-specific QGIS profile directory.
 
On Windows, the full path is typically:
``C:\Users\Username\AppData\Roaming\QGIS\QGIS3\profiles\default\python\plugins``
 
Here, ``Username`` represents your Windows username. The AppData\Roaming folder is hidden by default, so you may need to enable "Show hidden files" in Windows Explorer to access it. You could also write ``%appdata%`` in the explorer search bar, and then go back once to be in Appdata.
In den default\python\plugins folder gehören alle Plugins.
 
Erstelle nun eine Verknüpfung zum erstellten oder geklonten Plugin an diesem Ort. Die Verknüfung soll aber nur den Ordner mit dem Code darin enthalten.
Also nicht das ganze Repository sondern nur den Code. Dataien wie das README.md hat in der Verknüfung und im default\python\plugins folder nichts verloren
 
Nach einem Neustart von QGIS findet man das Plugin unter Erweiterungen -> Manage and Install Plugins... -> installed.
 
 


Some tipps:
== Writing plugins ==
* Docs...?


== Writing Python plugins ==
Documentation zum Schreiben von PyQGIS-Plugins:


There's a plugin interface for C++ and for [[Python]]:
**python**:
* QGIS Plugins with Python: See [[QGIS Plugins mit Python#Writing QGIS Plugins]]
https://download.osgeo.org/qgis/doc/manual/qgis-1.1.0_coding-compilation_guide_en.pdf#page=35&zoom=100,-26,120
* QGIS Plugins with C++: PyQGIS Developer Cookbook: http://www.qgis.org/pyqgis-cookbook/
https://www.zimmi.cz/posts/2017/qgis-plugin-development-getting-started/
[https://docs.qgis.org/3.34/en/docs/pyqgis_developer_cookbook/plugins/plugins.html#getting-started Structuring Python Plugins]


Some details about metadata.txt:
**c++**:
* If "hasProcessingProvider" exists, this should also be mentioned in the "about".
* QGIS Plugins with C++: https://download.osgeo.org/qgis/doc/manual/qgis-1.1.0_coding-compilation_guide_en.pdf#page=9&zoom=100,-26,120
* "hasProcessingProvider" (yes or no). If processing tool / processing tool then this should be in description and about and processing should appear in "tags".
* The licence must be GPL 2 or 3. Does the "LICENSE" file exist in the repo? If not, add GPL 3 from the template.
* "homepage" should point to the "real" website or at least to the repo "README.md" (not just to the repo, and README.md should not be called "readme.md")


ChaGPT can help. Try the following propt:
Other:
* [https://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/ PyQGIS Developer Cookbook]
* [https://qgis.org/pyqgis/master/ QGIS API]
* [https://raw.githubusercontent.com/webgeodatavore/qgis-class-diagram/master/diagramme_principal.png API Overview/Poster] by Thomas Gratier


Analyse the attributes of the metadata.txt file of a QGIS plugin (especially description and about) and give feedback on clarity, accuracy and consistency of content. The most important metadata.txt attributes are name, description, about, category, tags. The category attribute can only contain one of the following values: Raster, Vector, Database, Mesh, Web. Further specifications are contained here: https://docs.qgis.org/3.34/en/docs/pyqgis_developer_cookbook/plugins/plugins.html#metadata-txt. Output only the improvement suggestions.
* QGIS Manual - Guide: http://qgis.org/en/documentation/manuals.html > QGIS Coding and Compilation Guide
* Forum: https://qgisforum.com/
* Wiki-Artikel: http://www.qgis.org/wiki/Writing_Python_Plugins
* Community Support: http://gis.stackexchange.com/questions/tagged/pyqgis


Some helpful QGIS plugins:
* ??? - well written plugin
* Plugin Reloader - helper plugin
* QPIP - for dependencies.


See also:
* QGIS Python diagram classes by Thomas Gratier: https://github.com/webgeodatavore/qgis-class-diagram
* Tutorials:
** 2017, https://www.zimmi.cz/posts/2017/qgis-plugin-development-getting-started/
** 2014, Getting started writing QGIS Python plugins, by Peter Wells, Lutra: http://www.lutraconsulting.co.uk/blog/2014/10/17/getting-started-writing-qgis-python-plugins/
** 2014, Getting started writing QGIS 2.x plugins by Anita Graser, underdark: http://anitagraser.com/2014/04/26/getting-started-writing-qgis-2-x-plugins/
* QGIS Plugin Workshops
** 2014, Dhakal, Archana K.C, Shrestha and Timilsina: http://de.slideshare.net/Qust04/workshop-with-python-qgis
** 2013, by Victor Olaya: https://github.com/volaya/qgis-plugin-workshop
** 2013(?), by Nathan Woodrow: http://nathanw2.github.io/dmsnz-pyqgis-workshop/
** 2016, by Martin Dobias, Lutra: http://www.sigte.udg.edu/jornadassiglibre2016/wp-content/uploads/2016/01/girona-workshop-2016.pdf
** qgis2to3 https://github.com/opengisch/qgis2to3


== Python Console ==
== Python Console ==


In QGIS you can open a console using "Menu > Plugins" or "Ctrl-Alt-P". Then this window should show:
In QGIS you can open a console using "Menu > Plugins" or "Ctrl-Alt-P". Then this window should show:
 
```
  1 Python Console  
1 Python Console  
  2 Use iface to access QGIS API interface or Type help(iface) for more info.
2 Use iface to access QGIS API interface or Type help(iface) for more info.
  3 _
3 _
```


Hint: If you do the following from the Python console:
Hint: If you do the following from the Python console:
  geom = iface.activeLayer().selectedFeatures()[0].geometry()  
```
geom = iface.activeLayer().selectedFeatures()[0].geometry()
```


As soon as you do anything with QgsGeometry object, QGIS crashes!  
As soon as you do anything with QgsGeometry object, QGIS crashes!  
This is a long standing unresolved [http://hub.qgis.org/issues/777 issue 777] with python bindings. This works:
This is a long standing unresolved [http://hub.qgis.org/issues/777 issue 777] with python bindings. This works:
  feat = iface.activeLayer().selectedFeatures()[0]
```
  geom = QgsGeometry(feat.geometry())
feat = iface.activeLayer().selectedFeatures()[0]
 
geom = QgsGeometry(feat.geometry())
```
Test:
Test:
* geom.type(), geom.length(), geom.asPolyline() crashes if the line has three vertices, with only two vertices it returns an empty array.
* geom.type(), geom.length(), geom.asPolyline() crashes if the line has three vertices, with only two vertices it returns an empty array.
Zeile 85: Zeile 91:




== Manual installation of a Python plugin ==


Die manuelle Installation eines Python-Plugins, das sich noch nicht in einem Repository befindet, passiert wie folgt:
* Plugins befinden sich in QGIS standardmässig im Ordner .qgis2\python\plugins. Der Ordner .qgis2 wiederum befindet sich im Homeordner. Dieser ist zum Beispiel unter Windows C:\Users\Benutzername.
* Dorthin gehören alle Programm- und Daten-Dateien.
* Nach einem Neustart von QGIS kann man das Plugin unter Erweiterungen -> Plugins aktivieren, falls es nicht schon ist.




== Guidelines ==
== Debugging von QGIS-Plugins ==
Developers can debug their QGIS plugin using different methods:
 
* print() for basic console output,
* [https://qgis.org/pyqgis/3.34/core/QgsMessageLog.html qgsMessageLog.logMessage()] to log messages inside QGIS,
* [https://github.com/gruns/icecream icecream (ic())] for more advanced debugging with variable inspection.
 
 
We recommend using IceCream (ic()) over print() for debugging.
It displays variables and expressions with their values, formats data structures, and highlights output. Typing is faster, and it can optionally show the filename, line number, and function context.


Her are some recommendations and guidelines:


Regarding In/Output:
* A "File open..." dialog should open 1. in the user directory the first time it's used, 2. in the directory last used, 3. and this directory last used should be stored in the "Qt User Database".
* Prefer Memory Layer, then GeoPackage over Shapefile, when there's a need to store or export results.


These are the User Interface Design (UX/GUI) Guidelines for QGIS (click-based! e.g. for desktops and laptops):
# You can find the (rather small) HIG (Human Interface Guidelines) in chap. 5 of "Developers guide for QGIS"  https://github.com/qgis/QGIS/blob/master/CODING . See also starting on line 1441 of https://github.com/qgis/QGIS/blob/master/doc/CODING.t2t . HTML version here: http://htmlpreview.github.io/?https://raw.github.com/qgis/QGIS/master/doc/CODING.html#toc60 .
# In addition there is this (rather large)  (Graphical) User Interface (UI/GUI) Design with Qt: http://qt-project.org/doc/qt-4.8/qt-gui-concepts.html .
# Finally, there exist the “Windows 8 App UX Guidelines”. This is only “good to know” and can be used in cases of doubt or where the two Guidelines (QGIS+Qt) don’t apply


User Interface Design (UX/GUI) Guidelines (touch-based! e.g. for tablets):
* QGIS and the UX Guidelines for QGIS are based in a click-based UX paradigm; tablets need touch-based paradigm.
* “Windows 8 App UX Guidelines”: http://msdn.microsoft.com/en-us/library/windows/apps/hh465424.aspx .
* See e.g. the project [[BLUgis]], an application for rescue services.


== Testing Python Plugins ==
== Testing Plugins ==


* Configuration for Debugging and Unit Testing of PyQGIS: See [[QGIS Plugins mit Python]].
* Configuration for Debugging and Unit Testing of PyQGIS: See [[QGIS Plugins mit Python]].


== Publication of a Python Plugin ==


See '[[QGIS_Plugins_mit_Python#How_to_publish_a_plugin|How to publish a plugin]]'.
 
 
 
== Publication of a Plugin ==
[https://docs.qgis.org/3.34/en/docs/pyqgis_developer_cookbook/plugins/releasing.html Releasing your plugin].
 
A ``metadata.txt`` file is crucial because it contains descriptive information about the plugin. It helps users and qgis understand and manage data efficiently.
 
* If "hasProcessingProvider" exists, this should also be mentioned in the "about".
* "hasProcessingProvider" (yes or no). If processing tool / processing tool then this should be in description and about and processing should appear in "tags".
* The licence must be GPL 2 or 3. Does the "LICENSE" file exist in the repo? If not, add GPL 3 from the template.
* "homepage" should point to the "real" website or at least to the repo "README.md" (not just to the repo, and README.md should not be called "readme.md")
 
More details about a [https://www.giswiki.ch/QGIS_Plugins_mit_Python metadata.txt].
 
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 under sign-in: https://www.osgeo.org/
* 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.
 
 
 


== Weblinks ==
== Weblinks ==
Zeile 123: Zeile 152:
* Wiki: http://wiki.qgis.org/  
* Wiki: http://wiki.qgis.org/  
* Blog: http://blog.qgis.org
* Blog: http://blog.qgis.org
* Bug tracker: https://trac.osgeo.org/qgis/




Zeile 129: Zeile 157:
[[Kategorie:GIS]]
[[Kategorie:GIS]]
[[Kategorie:Programmierung]]
[[Kategorie:Programmierung]]
[[Kategorie:QGIS-Plugin]]

Version vom 21. Januar 2025, 15:43 Uhr

QGIS for Devs

See also:


Setting up a QGIS IDE

The Visual Studio Code IDE is a free, and lightweight editor with advanced debugging tools, including extensions tailored for Python and QGIS development. It also supports version control and code formatting, making plugin development more efficient.

There is the Plugin Reloader plugin to easily apply your code changes without restarting QGIS. This greatly speeds up debugging and iteration, making development smoother.


The plugin development cycle:

  • Modify the Python files of the AIAMAS plugin.
  • Save changes in VSCode.
  • Use the Plugin Reloader to apply changes instantly.


Manual installation of a python plugin

Die manuelle Installation eines Python-Plugins, das sich noch nicht in einem Repository befindet, passiert wie folgt:

Clone das Repository oder erstelle ein Plugin an einem beliebigen Ort. Plugins are stored in QGIS by default in the folder python\plugins. This folder is located within the user-specific QGIS profile directory.

On Windows, the full path is typically: ``C:\Users\Username\AppData\Roaming\QGIS\QGIS3\profiles\default\python\plugins``

Here, ``Username`` represents your Windows username. The AppData\Roaming folder is hidden by default, so you may need to enable "Show hidden files" in Windows Explorer to access it. You could also write ``%appdata%`` in the explorer search bar, and then go back once to be in Appdata. In den default\python\plugins folder gehören alle Plugins.

Erstelle nun eine Verknüpfung zum erstellten oder geklonten Plugin an diesem Ort. Die Verknüfung soll aber nur den Ordner mit dem Code darin enthalten. Also nicht das ganze Repository sondern nur den Code. Dataien wie das README.md hat in der Verknüfung und im default\python\plugins folder nichts verloren

Nach einem Neustart von QGIS findet man das Plugin unter Erweiterungen -> Manage and Install Plugins... -> installed.


Writing plugins

Documentation zum Schreiben von PyQGIS-Plugins:

    • python**:

https://download.osgeo.org/qgis/doc/manual/qgis-1.1.0_coding-compilation_guide_en.pdf#page=35&zoom=100,-26,120 https://www.zimmi.cz/posts/2017/qgis-plugin-development-getting-started/ Structuring Python Plugins

Other:


Python Console

In QGIS you can open a console using "Menu > Plugins" or "Ctrl-Alt-P". Then this window should show: ``` 1 Python Console 2 Use iface to access QGIS API interface or Type help(iface) for more info. 3 _ ```

Hint: If you do the following from the Python console: ``` geom = iface.activeLayer().selectedFeatures()[0].geometry() ```

As soon as you do anything with QgsGeometry object, QGIS crashes! This is a long standing unresolved issue 777 with python bindings. This works:

```
feat = iface.activeLayer().selectedFeatures()[0]
geom = QgsGeometry(feat.geometry())

```

Test:

  • geom.type(), geom.length(), geom.asPolyline() crashes if the line has three vertices, with only two vertices it returns an empty array.

See also e.g. Python Scripting (PyQGIS) on qgistutorials.com.



Debugging von QGIS-Plugins

Developers can debug their QGIS plugin using different methods:


We recommend using IceCream (ic()) over print() for debugging. It displays variables and expressions with their values, formats data structures, and highlights output. Typing is faster, and it can optionally show the filename, line number, and function context.



Testing Plugins



Publication of a Plugin

Releasing your plugin.

A ``metadata.txt`` file is crucial because it contains descriptive information about the plugin. It helps users and qgis understand and manage data efficiently.

  • If "hasProcessingProvider" exists, this should also be mentioned in the "about".
  • "hasProcessingProvider" (yes or no). If processing tool / processing tool then this should be in description and about and processing should appear in "tags".
  • The licence must be GPL 2 or 3. Does the "LICENSE" file exist in the repo? If not, add GPL 3 from the template.
  • "homepage" should point to the "real" website or at least to the repo "README.md" (not just to the repo, and README.md should not be called "readme.md")

More details about a metadata.txt.

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 under sign-in: https://www.osgeo.org/
  • 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.



Weblinks