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.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'])

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.1.tar.gz (481.8 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.1-py3-none-any.whl (23.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyqgis-scripting-ext-0.1.1.tar.gz
  • Upload date:
  • Size: 481.8 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.1.tar.gz
Algorithm Hash digest
SHA256 0eb7d27161fa3c40b973436b0032af6c23ec1a26029035c4da96d9b25a9008d4
MD5 a5c2e25afcd67a77dbd6bc5ee2724999
BLAKE2b-256 a04c7356fa6da79766c8ae785b13eb451b75d968d63dbd2e8a85c766a11ef0fb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyqgis_scripting_ext-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ffdaf7bbca6f0b81a5f5492d5317e2b609d70f95c1f4ed133c87d8591a0de46d
MD5 3f0e48486b6c2421313c01f984307e82
BLAKE2b-256 7a8d58c159199072f60022081f85040dfe902928da0fe44a26744a1772a30d1d

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