QGIS for Devs: Unterschied zwischen den Versionen

Aus Geometa Lab OST
Zur Navigation springen Zur Suche springen
 
(37 dazwischenliegende Versionen von 3 Benutzern werden nicht angezeigt)
Zeile 1: Zeile 1:
See also:
== Setting up a QGIS workspace==
* [[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')]


== Python Console ==
=== IDE ===


In QGIS you can open a console using "Menu > Plugins" or "Ctrl-Alt-P". Then this window should show:
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.


  1 Python Console
PyCharm is a Python-focused alternative that is easier to configure than VSCode.  
  2 Use iface to access QGIS API interface or Type help(iface) for more info.
The Community Edition is free. It includes a powerful built-in debugger.
  3 _


Hint: If you do the following from the Python console:
=== QGIS Extensions ===
  g=iface.activeLayer().selectedFeatures()[0].geometry()
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:
  feat = iface.activeLayer().selectedFeatures()[0]
  g = QgsGeometry(feat.geometry())


Test:
There is the [https://plugins.qgis.org/plugins/plugin_reloader/ Plugin Reloader] plugin to easily apply your code changes without restarting QGIS.
* g.type(), g.length(), g.asPolyline() crashes if the line has three vertices, with only two vertices it returns an empty array.
This greatly speeds up debugging and iteration, making development smoother.


=== plugin development cycle ===


== Writing Python plugins ==
# Modify the Python files of the AIAMAS plugin.
# Save changes in VSCode or PyCharm.
# Use the Plugin Reloader to apply changes instantly.


Es gibt eine Plugin-Schnittstelle für C++ und für [[Python]]:
== Manual installation of a python plugin ==
* QGIS Plugins mit Python: Siehe [[QGIS Plugins mit Python#Writing QGIS Plugins]]
* QGIS Plugins mit C++: PyQGIS Developer Cookbook: http://www.qgis.org/pyqgis-cookbook/


Siehe auch:
If a Python plugin is not available in the official repository, it can be manually installed as follows:
* Getting started writing QGIS 2.x plugins http://anitagraser.com/2014/04/26/getting-started-writing-qgis-2-x-plugins/
* QGIS Plugin Workshop by Victor Olaya: https://github.com/volaya/qgis-plugin-workshop


== Manual installation of a Python plugin ==
# Clone the repository or create a new plugin in any directory.
# QGIS stores plugins by default in the <code>python/plugins</code> folder within the user-specific QGIS profile directory.


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 ==
''' On Windows, the full path is typically: '''
<code>C:\Users\Username\AppData\Roaming\QGIS\QGIS3\profiles\default\python\plugins</code>


Her are some recommendations and guidelines:
Replace <code>Username</code> with your actual Windows username.


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):
# The <code>AppData\Roaming</code> folder is hidden by default. To access it:
# 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 .
* Enable "Show hidden files" in Windows Explorer.
# 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 .
* Alternatively, type <code>%appdata%</code> in the Explorer search bar, navigate back one level to AppData. All the QGIS plugins are stored inside the <code>default/python/plugins</code> folder.
# 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
# Create a shortcut to the cloned or created plugin in this directory, but only include the '''code folder'''. Not the entire repository. Files like<code>README.md</code> should not be included in this shortcut.


User Interface Design (UX/GUI) Guidelines (touch-based! e.g. for tablets):
After restarting QGIS, the plugin will now appear under '''Extensions > Manage and Install Plugins > Installed.'''
* 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 ==
== Writing plugins ==


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


== Publication of a Python Plugin ==
'''Python:'''


See '[[QGIS_Plugins_mit_Python#How_to_publish_a_plugin|How to publish a plugin]]'.
* [https://download.osgeo.org/qgis/doc/manual/qgis-1.1.0_coding-compilation_guide_en.pdf# page=35&zoom=100,-26,120 Writing a QGIS Plugin in Python]
* [https://www.zimmi.cz/posts/2017/qgis-plugin-development-getting-started/ Getting started with a QGIS Plugin]
* [https://docs.qgis.org/3.34/en/docs/pyqgis_developer_cookbook/plugins/plugins.html# getting-started Structuring Python Plugins]
* [https://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/ PyQGIS Developer Cookbook]
 
'''C++:'''
* [https://download.osgeo.org/qgis/doc/manual/qgis-1.1.0_coding-compilation_guide_en.pdf# page=9&zoom=100,-26,120 QGIS Plugins with C++ Manual]
 
'''Other:'''
* [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]
 
* [http://qgis.org/en/documentation/manuals.html QGIS Manual - Guide] > QGIS Coding and Compilation Guide
* [https://qgisforum.com/ Forum]
* [http://www.qgis.org/wiki/Writing_Python_Plugins Wiki-Artikel]
* [http://gis.stackexchange.com/questions/tagged/pyqgis Community Support]
 
== Using the QGIS Python Console ==
 
To open the Python console in QGIS:
 
# Navigate to '''Menu > Plugins'''
 
# Use the shortcut '''Ctrl+Alt+P'''
 
 
'''Expected Console Output:'''
 
Python Console
Use iface to access QGIS API interface or Type help(iface) for more info.
_
 
'''Avoiding Crashes with QgsGeometry'''
 
If using:
 
geom = iface.activeLayer().selectedFeatures()[0].geometry()
 
 
QGIS may crash due to a long-standing unresolved issue 777 with Python bindings.
Instead, use:
 
feat = iface.activeLayer().selectedFeatures()[0]
geom = QgsGeometry(feat.geometry())
 
Testing:
<code>geom.type(), geom.length(), geom.asPolyline()</code> crashes if the line has three vertices, with only two vertices it returns an empty array.
 
See also e.g.  [http://www.qgistutorials.com/en/ Python Scripting (PyQGIS)].
 
== Debugging QGIS-Plugins ==
 
=== Debugging Methods ===
 
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 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.
 
=== Remote Debugging with PyCharm(Debug Server) ===
 
If running QGIS externally, enable remote debugging:
 
* Install the PyCharm Debugger:
pip install pydevd-pycharm
* Add the following to the plugin code:
import pydevd_pycharm
pydevd_pycharm.settrace('localhost', port=5678, stdoutToServer=True, stderrToServer=True, suspend=False)
* In PyCharm, go to '''Run > Edit Configurations > Python Remote Debug'''
* Set '''Host: localhost, Port: 5678''', then click '''Start Debugging'''
 
 
== Testing Plugins ==
 
* Writing Uni Tests for QGIS Plugins: http://snorf.net/blog/2014/01/04/writing-unit-tests-for-qgis-python-plugins/
* Configuration for '''Debugging and Unit Testing of PyQGIS''': See [[QGIS Plugins mit Python]].
* Testing Qt-GUI components: http://www.voom.net/pyqt-qtest-example
 
Using PyCharm simplifies QGIS plugin development with testing features
 
 
== Publication of a Plugin ==
 
[https://docs.qgis.org/3.34/en/docs/pyqgis_developer_cookbook/plugins/releasing.html Releasing your plugin]
 
A <code>metadata.txt</code> file is essential as it contains descriptive information about the plugin. This helps users and QGIS manage and understand plugin details effectively.
 
=== Requirements ===
 
* If <code>hasProcessingProvider</code> exists, it should be mentioned in the "about" section.
* Processing tools should be described in <code>description</code>, <code>about</code>, and added as a <code>tag</code>.
* The licence must be '''GPL 2 or 3'''. Does the <code>LICENSE</code> file exist in the repo? If not, add '''GPL 3''' from the template.
* The <code>homepage</code> must point to the '''actual''' website or at least the repo’s <code>README.md</code> (not just the repo itself).
 
More details about a [https://www.giswiki.ch/QGIS_Plugins_mit_Python metadata.txt].
 
===Pre-Upload Checklist ===
* Ensure all attributes in <code>metadata.txt</code> are correct.
 
* Increment the '''plugin version''' before uploading.
 
* Change '''experimental flag''' if needed (set to <code>false</code> to appear in the default QGIS Plugin list).
 
* Set an '''icon''' (no strict size limit specified).
 
* Remove unnecessary files (<code>*.pyc</code>, <code>.gitignore</code>, IDE-specific files, etc.).
 
* Use only '''ASCII characters''' in filenames (Umlauts will cause errors).
 
* Plugin directory names cannot contain '''hyphens'''.
 
* '''ZIP''' the plugin directory before upload.
 
===Uploading===
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/accounts/login/ login] with your OSGeo ID.
# The plugin (ZIP archive) can be uploaded [https://plugins.qgis.org/plugins/add/ here]. 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 [https://plugins.qgis.org/ How to add your plugin to this repository] for the criteria the plugin has to meet in order to get approved.


== Weblinks ==
== Weblinks ==
* Homepage: http://www.qgis.org/ ([http://wiki.qgis.org/qgiswiki/Project_Organigram Project-Team QGIS])
 
* Wiki: http://wiki.qgis.org/  
* [http://www.qgis.org/ QGIS Homepage] ([http://wiki.qgis.org/qgiswiki/Project_Organigram Project-Team QGIS])
* Blog: http://blog.qgis.org
* [http://wiki.qgis.org/ QGIS Wiki]
* Bug tracker: https://trac.osgeo.org/qgis/
* [http://blog.qgis.org QGIS Blog]




[[Kategorie:QGIS]]
[[Category:QGIS]]
[[Kategorie:GIS]]
[[Category:GIS]]
[[Kategorie:Programmierung]]
[[Category:Programmierung]]
[[Category:QGIS-Plugin]]

Aktuelle Version vom 28. Januar 2025, 12:48 Uhr

Setting up a QGIS workspace

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.

PyCharm is a Python-focused alternative that is easier to configure than VSCode. The Community Edition is free. It includes a powerful built-in debugger.

QGIS Extensions

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.

plugin development cycle

  1. Modify the Python files of the AIAMAS plugin.
  2. Save changes in VSCode or PyCharm.
  3. Use the Plugin Reloader to apply changes instantly.

Manual installation of a python plugin

If a Python plugin is not available in the official repository, it can be manually installed as follows:

  1. Clone the repository or create a new plugin in any directory.
  2. QGIS stores plugins by default in the python/plugins folder 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

Replace Username with your actual Windows username.


  1. The AppData\Roaming folder is hidden by default. To access it:
  • Enable "Show hidden files" in Windows Explorer.
  • Alternatively, type %appdata% in the Explorer search bar, navigate back one level to AppData. All the QGIS plugins are stored inside the default/python/plugins folder.
  1. Create a shortcut to the cloned or created plugin in this directory, but only include the code folder. Not the entire repository. Files likeREADME.md should not be included in this shortcut.

After restarting QGIS, the plugin will now appear under Extensions > Manage and Install Plugins > Installed.

Writing plugins

Documentation for Writing PyQGIS Plugins

Python:

C++:

Other:

Using the QGIS Python Console

To open the Python console in QGIS:

  1. Navigate to Menu > Plugins
  1. Use the shortcut Ctrl+Alt+P


Expected Console Output:

Python Console
Use iface to access QGIS API interface or Type help(iface) for more info.
_

Avoiding Crashes with QgsGeometry

If using:

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


QGIS may crash due to a long-standing unresolved issue 777 with Python bindings. Instead, use:

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


Testing: 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).

Debugging QGIS-Plugins

Debugging Methods

Developers can debug their QGIS plugin using different methods:

  • print() for basic console output

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.

Remote Debugging with PyCharm(Debug Server)

If running QGIS externally, enable remote debugging:

  • Install the PyCharm Debugger:
pip install pydevd-pycharm	
  • Add the following to the plugin code:
import pydevd_pycharm
pydevd_pycharm.settrace('localhost', port=5678, stdoutToServer=True, stderrToServer=True, suspend=False)
  • In PyCharm, go to Run > Edit Configurations > Python Remote Debug
  • Set Host: localhost, Port: 5678, then click Start Debugging


Testing Plugins

Using PyCharm simplifies QGIS plugin development with testing features


Publication of a Plugin

Releasing your plugin

A metadata.txt file is essential as it contains descriptive information about the plugin. This helps users and QGIS manage and understand plugin details effectively.

Requirements

  • If hasProcessingProvider exists, it should be mentioned in the "about" section.
  • Processing tools should be described in description, about, and added as a tag.
  • The licence must be GPL 2 or 3. Does the LICENSE file exist in the repo? If not, add GPL 3 from the template.
  • The homepage must point to the actual website or at least the repo’s README.md (not just the repo itself).

More details about a metadata.txt.

Pre-Upload Checklist

  • Ensure all attributes in metadata.txt are correct.
  • Increment the plugin version before uploading.
  • Change experimental flag if needed (set to false to appear in the default QGIS Plugin list).
  • Set an icon (no strict size limit specified).
  • Remove unnecessary files (*.pyc, .gitignore, IDE-specific files, etc.).
  • Use only ASCII characters in filenames (Umlauts will cause errors).
  • Plugin directory names cannot contain hyphens.
  • ZIP the plugin directory before upload.

Uploading

In order to upload a plugin on the official QGIS page a OSGeo ID (user) is required (e.g. user account "geometalab").

  1. The user account can be created here under sign-in: https://www.osgeo.org/
  2. On the page login with your OSGeo ID.
  3. The plugin (ZIP archive) can be uploaded here. Check experimental field if the plugin still is experimental.
  4. 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 for the criteria the plugin has to meet in order to get approved.

Weblinks