QGIS for Devs: Unterschied zwischen den Versionen

Aus Geometa Lab OST
Zur Navigation springen Zur Suche springen
(update syntax)
(fix space, lists and code)
Zeile 2: Zeile 2:
See also:
See also:
* [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')]


<h2> Setting up a QGIS IDE </h2>
<h2> Setting up a QGIS IDE </h2>
Zeile 9: Zeile 10:
This greatly speeds up debugging and iteration, making development smoother.
This greatly speeds up debugging and iteration, making development smoother.


<h3> plugin development cycle </h3>:
<h3> plugin development cycle: </h3>
<ol>
<li>Modify the Python files of the AIAMAS plugin.</li>
 
<li>Save changes in VSCode.</li>


1. Modify the Python files of the AIAMAS plugin.
<li>Use the Plugin Reloader to apply changes instantly.</li>
2. Save changes in VSCode.
</ol>
3. Use the Plugin Reloader to apply changes instantly.


<h2> Manual installation of a python plugin </h2>
<h2> Manual installation of a python plugin </h2>


If a Python plugin is not available in the official repository, it can be manually installed as follows:
If a Python plugin is not available in the official repository, it can be manually installed as follows:
<ol>
<li>Clone the repository or create a new plugin in any directory.
<li>QGIS stores plugins by default in the <code>python/plugins</code> folder within the user-specific QGIS profile directory.


1. Clone the repository or create a new plugin in any directory.
<strong> On Windows, the full path is typically </strong>:
2. QGIS stores plugins by default in the <code>python/plugins</code> folder within the user-specific QGIS profile directory.
<code>C:\Users\Username\AppData\Roaming\QGIS\QGIS3\profiles\default\python\plugins</code></li>
 
<strong> On Windows, the full path is typically </strong>:
<code>C:\Users\Username\AppData\Roaming\QGIS\QGIS3\profiles\default\python\plugins</code>
 
Replace <code>Username</code> with your actual Windows username.


3. The <code>AppData\Roaming</code> folder is hidden by default. To access it:
Replace <code>Username</code> with your actual Windows username.


* Enable "Show hidden files" in Windows Explorer.
<li>The <code>AppData\Roaming</code> folder is hidden by default. To access it:
* 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.
* Enable "Show hidden files" in Windows Explorer.
* Alternatively, type <code>%appdata%</code> in the Explorer search bar, navigate back one level to AppData.</li>


4. Create a shortcut to the cloned or created plugin in this directory, but only include the <strong>code folder</strong>. Not the entire repository. Files like<code>README.md</code> should not be included in this shortcut.
All the QGIS plugins are stored inside the <code>default/python/plugins</code> folder.


<li>Create a shortcut to the cloned or created plugin in this directory, but only include the <strong>code folder</strong>. Not the entire repository. Files like<code>README.md</code> should not be included in this shortcut.</li>
</ol>
After restarting QGIS, the plugin will now appear under <strong>Extensions > Manage and Install Plugins > Installed.</strong>
After restarting QGIS, the plugin will now appear under <strong>Extensions > Manage and Install Plugins > Installed.</strong>


Zeile 63: Zeile 67:


To open the Python console in QGIS:
To open the Python console in QGIS:
<ol>
<li>Navigate to <strong>Menu > Plugins</strong></li>


* Navigate to <strong>Menu > Plugins</strong>
<li>Use the shortcut <strong>Ctrl+Alt+P</strong></li>
</ol>


* Use the shortcut <strong>Ctrl+Alt+P</strong>
<strong>Expected Console Output:</strong>


<strong>Expected Console Output:</strong>
<pre>
<code>
Python Console
Python Console
Use iface to access QGIS API interface or Type help(iface) for more info.
Use iface to access QGIS API interface or Type help(iface) for more info.
_
_
</code>
</pre>


<strong>Avoiding Crashes with QgsGeometry</strong>
<strong>Avoiding Crashes with QgsGeometry</strong>


If using:
If using:
<pre><code class="language-python">
<pre>
geom = iface.activeLayer().selectedFeatures()[0].geometry()
geom = iface.activeLayer().selectedFeatures()[0].geometry()
</code></pre>
</pre>


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


<pre><code class="language-python">
<pre>
  feat = iface.activeLayer().selectedFeatures()[0]
  feat = iface.activeLayer().selectedFeatures()[0]
  geom = QgsGeometry(feat.geometry())
  geom = QgsGeometry(feat.geometry())
   
   
</code></pre>
</pre>
   
   
Testing:
Testing:
<pre><code class="language-python">
<pre>
  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.
</code></pre>
</pre>


See also e.g.  [http://www.qgistutorials.com/en/ Python Scripting (PyQGIS)] on qgistutorials.com.
See also e.g.  [http://www.qgistutorials.com/en/ Python Scripting (PyQGIS)] on qgistutorials.com.
Zeile 112: Zeile 118:


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


<h2> Publication of a Plugin </h2>
<h2> Publication of a Plugin </h2>
Zeile 119: Zeile 126:
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.
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.


<h3> Requirements: </h3>h3>
<h3> Requirements: </h3>


* If <code>hasProcessingProvider</code> exists, it should be mentioned in the "about" section.
* If <code>hasProcessingProvider</code> exists, it should be mentioned in the "about" section.
Zeile 128: Zeile 135:
More details about a [https://www.giswiki.ch/QGIS_Plugins_mit_Python metadata.txt].
More details about a [https://www.giswiki.ch/QGIS_Plugins_mit_Python metadata.txt].


Pre-Upload Checklist:
<h3>Pre-Upload Checklist: </h3>
* Ensure all attributes in <code>metadata.txt</code> are correct.
* Ensure all attributes in <code>metadata.txt</code> are correct.


Zeile 145: Zeile 152:
* <strong>ZIP</strong> the plugin directory before upload.
* <strong>ZIP</strong> the plugin directory before upload.


<strong>Upload:</strong>
<h3>Upload:</h3>
In order to upload a plugin on the official QGIS page a OSGeo ID (user) is required (e.g. user account "geometalab").
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/
<ol>
2. On the page https://plugins.qgis.org/ login with your OSGeo ID.
<li>The user account can be created here under sign-in: https://www.osgeo.org/</li>
3. The plugin (ZIP archive) can be uploaded here: https://plugins.qgis.org/plugins/add/  
<li>On the page https://plugins.qgis.org/ login with your OSGeo ID.</li>
Check experimental field if the plugin still is experimental.
<li>The plugin (ZIP archive) can be uploaded here: https://plugins.qgis.org/plugins/add/  
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 [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.
Check experimental field if the plugin still is experimental.</li>
 
<li>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.</li>
</ol>
<h2> Weblinks </h2>
<h2> Weblinks </h2>



Version vom 22. Januar 2025, 12:19 Uhr

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.

plugin development cycle:

  1. Modify the Python files of the AIAMAS plugin.
  2. Save changes in VSCode.
  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
  3. Replace Username with your actual Windows username.
  4. 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.
  5. All the QGIS plugins are stored inside the default/python/plugins folder.
  6. 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: 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

C++:

Other:

Using the QGIS Python Console

To open the Python console in QGIS:

  1. Navigate to Menu > Plugins
  2. 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) on qgistutorials.com.

Debugging 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 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.

Upload:

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 https://plugins.qgis.org/ login with your OSGeo ID.
  3. The plugin (ZIP archive) can be uploaded here: https://plugins.qgis.org/plugins/add/ 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