Skip to main content

A simple package that tries to make pyQGIS a bit more userfriendly to non programmers.

Project description

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

Installation

At the time being the lib is available in pypi. You can install it with pip:

pip install pyqgis-scripting-ext

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

import subprocess
subprocess.run(["pip", "install", "pyqgis-scripting-ext"])

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyqgis-scripting-ext-0.1.0.tar.gz (481.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyqgis_scripting_ext-0.1.0-py3-none-any.whl (23.9 kB view details)

Uploaded Python 3

File details

Details for the file pyqgis-scripting-ext-0.1.0.tar.gz.

File metadata

  • Download URL: pyqgis-scripting-ext-0.1.0.tar.gz
  • Upload date:
  • Size: 481.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for pyqgis-scripting-ext-0.1.0.tar.gz
Algorithm Hash digest
SHA256 12ea32f9e1327f676d213e250f8498ed4b3e3c3edd756847995951be7c47ef86
MD5 83294e246a8c1c8942333a1d626700ec
BLAKE2b-256 27e92f4716cedb327905f2167fedeaa36b86e4b9301a09c9d072d73c690369e3

See more details on using hashes here.

File details

Details for the file pyqgis_scripting_ext-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pyqgis_scripting_ext-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b8b00e2a35eed90750ce0347401d1fcfcd97190cdf8c995f74883d2b3c46e7ce
MD5 6faac17ac22b6991a5f0e95e103ca7e9
BLAKE2b-256 df25651db9a49b160809d0cddf55948911331be09614cfa909d8a66c6b99a3e1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page