Skip to main content

pyQGIS Scripting Extension

Introduction

pyQGIS in a great and powerful tool to automate GIS tasks.

But more than a scripting language it is a replication in python of the QGIS API. This is amazing but can be overwhelming for beginners.

This extension provides a set of tools to make scripting easier and userfriendly.

For example the following pyQGIS code to style a layer and set labels:

# marker style
properties = {
    'name': 'square',
    'size': 8,
    'color': '255,0,0,128',
    'outline_color': 'black',
    'outline_width': 1,
    'angle': 45
}
symbol = QgsMarkerSymbol.createSimple(properties) 4
layer.renderer().setSymbol(symbol)

# label style
settings = QgsPalLayerSettings()
format = QgsTextFormat()
format.setFont(QFont('Arial', 8)) # font with size
format.setColor(QColor('red')) # font color
buffer = QgsTextBufferSettings()
buffer.setEnabled(True)
buffer.setSize(0.50)
buffer.setColor(QColor('black'))
# create a halo around the text
format.setBuffer(buffer) # set the halo in the text format
settings.setFormat(format) # set the text format in the layer settings
settings.fieldName = "NAME" # use the NAME field as label
# label positioning with offsets
settings.placement = QgsPalLayerSettings.OverPoint
settings.xOffset = 0.0
settings.yOffset = -8.5
# enable labels on the layer
labels = QgsVectorLayerSimpleLabeling(settings)
layer.setLabelsEnabled(True)
layer.setLabeling(labels)

boils down to this:

from pyqgis_scripting_ext.core import *

pointStyle = HMarker("square", 8, 45) + HFill("0,255,0,128") + HStroke("black", 1) + HLabel("NAME", yoffset = -5) + HHalo("white", 1)
layer.set_style(pointStyle)

Also the API is a bit difficult to follow when it comes to the main geometry objects. There are QgsGeometry, QgsPointXY and the specializations of QgsAbstractGeometry like QgsPolygon, QgsLineString or QgsPoint. Here we use a single Geometry object with a common parent.

And then there is this other thing. Since pyQGIS is bound to its lowlevel API, memory management can be an issue. If you reuse the same variable name for different objects, most of the times you will experience a QGIS crash.

This is quite brutal and a huge problem for beginners. Take the following example:

p = QgsPoint(12, 46)
pg = QgsGeometry(p)

coords = [[31,11], [10,30], [20,40], [40,40]]
l = QgsLineString([ QgsPoint(p[0], p[1]) for p in coords])
lg = QgsGeometry(l)

print(pg)
print(lg)

# assume somewhere else in the code you just need to recreate 
# the QgsGeometry for whatever reason, using the previous p object
pg = QgsGeometry(p)
print(pg)

This super simple script crashes QGIS.

In this extensions an effort is made to avoid this issue by cloning/recreating objects when needed.

Clearly this library is limited against everything you can do in pyQGIS, but it is a starting point.

One last thing: we chose simplicity over efficiency, so you might experience performance issues for large datasets.

Compatibility

Compatibility table:

QGIS Version pyQGIS Scripting Extension
Prizren 0.1.1

Installation

At the time being the lib is available in pypi.

To make sure you install it using the python used by QGIS, you can run directly from the QGIS python console:

import pip
pip.main(['install', 'pyqgis-scripting-ext'])

Release files for pyqgis-scripting-ext 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyqgis-scripting-ext 0.1.1
File Size Uploaded
pyqgis-scripting-ext-0.1.1.tar.gz 481.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyqgis-scripting-ext 0.1.1
File Interpreter ABI Platform
pyqgis_scripting_ext-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 505.7 kB

Release files / pyqgis-scripting-ext-0.1.1.tar.gz

Download URL pyqgis-scripting-ext-0.1.1.tar.gz
Size 481.8 kB
Tags Source
SHA-256 checksum
How to use checksums
0eb7d27161fa3c40b973436b0032af6c23ec1a26029035c4da96d9b25a9008d4
BLAKE2b-256 checksum
How to use checksums
a04c7356fa6da79766c8ae785b13eb451b75d968d63dbd2e8a85c766a11ef0fb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release files / pyqgis_scripting_ext-0.1.1-py3-none-any.whl

Download URL pyqgis_scripting_ext-0.1.1-py3-none-any.whl
Size 23.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ffdaf7bbca6f0b81a5f5492d5317e2b609d70f95c1f4ed133c87d8591a0de46d
BLAKE2b-256 checksum
How to use checksums
7a8d58c159199072f60022081f85040dfe902928da0fe44a26744a1772a30d1d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page