Skip to main content

Create QGIS projects programmatically using Python

Project description

qgis-project

qgis-project

PyPI version Python versions Tests Docs License

Create QGIS projects programmatically using Python.

from qgis_project import Project, RasterLayer, RasterStyleBW

proj = Project()
proj.add_layer("dem.tif")                                    # raster, auto-detected
proj.add_layer("boundaries.geojson")                         # vector, auto-detected
proj.add_layer(RasterLayer("dem.tif", style=RasterStyleBW(vmin=0, vmax=3000)))
proj.save("output.qgz")
proj.open()     # launch QGIS for visual inspection

Installation

Install the package:

pip install qgis-project

QGIS is not on PyPI and must be available separately. The recommended approach is a dedicated conda environment:

conda env create -f environment_platform_independent.yml
conda activate qgis-env-pi
pip install qgis-project

Or install QGIS from conda-forge into an existing environment:

conda install -c conda-forge qgis
pip install qgis-project

Usage

Basic project

from qgis_project import Project

proj = Project()
proj.add_layer("dem.tif")
proj.add_layer("roads.geojson")
proj.save("my_project.qgz")
proj.exit()

Layer groups

proj.add_layer(RasterLayer("dem.tif", group="terrain"))
proj.add_layer(RasterLayer("slope.tif", group=["terrain", "derived"]))

Raster styling

Class Effect
RasterStyleBW Grayscale with contrast stretch
RasterStyleSinglePseudocolor Single-band color ramp
RasterStyleMultiBandColor Multi-band RGB/false-color composite
from qgis_project import RasterLayer, RasterStyleBW

layer = RasterLayer(
    file="dem.tif",
    name="Elevation",
    group="terrain",
    style=RasterStyleBW(vmin=0, vmax=3000),
)
proj.add_layer(layer)

If vmin/vmax are omitted they are computed from the layer data.

RasterStyleMultiBandColor requires band_idx to be a list of three band numbers [R, G, B]:

from qgis_project import RasterStyleMultiBandColor

layer = RasterLayer(
    file="rgb.tif",
    band_idx=[1, 2, 3],
    style=RasterStyleMultiBandColor(),
)
proj.add_layer(layer)

Vector styling

Class Effect
VectorStyleSingleSymbol Uniform fill/line/marker color and outline
VectorStyleCategorized One color per unique attribute value
VectorStyleGraduated Equal-interval color classes (choropleth) for a numeric attribute
from qgis_project import Layer, VectorStyleSingleSymbol

layer = Layer(
    file="regions.geojson",
    style=VectorStyleSingleSymbol(color="red", outline_color="black", outline_width=1.0),
)
proj.add_layer(layer)

VectorStyleCategorized and VectorStyleGraduated work on point, line, and polygon layers alike:

from qgis_project import VectorStyleCategorized, VectorStyleGraduated

layer = Layer("regions.geojson", style=VectorStyleCategorized(field="class", colormap="Spectral"))
layer = Layer("regions.geojson", style=VectorStyleGraduated(field="value", num_classes=5, colormap="Viridis"))

If vmin/vmax are omitted from VectorStyleGraduated, they are computed from the field's data. outline_color/outline_width on VectorStyleSingleSymbol have no effect on line layers (a line has no separate outline).

Open in QGIS

proj.open("output.qgz")      # saves and launches QGIS
proj.print_layer_tree()      # inspect the layer tree in the terminal

Development

Install with dev dependencies:

pip install -e ".[dev]"

Run unit tests (no QGIS required):

pytest -m "not qgis"

Run integration tests (QGIS environment required):

pytest -m qgis

Run the manual visual test:

python scripts/manual_test.py

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

qgis_project-0.5.0.tar.gz (1.1 MB view details)

Uploaded Source

Built Distribution

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

qgis_project-0.5.0-py3-none-any.whl (25.8 kB view details)

Uploaded Python 3

File details

Details for the file qgis_project-0.5.0.tar.gz.

File metadata

  • Download URL: qgis_project-0.5.0.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for qgis_project-0.5.0.tar.gz
Algorithm Hash digest
SHA256 789f3699e625dc09aa56ad3ac37593a01b236ca8b644263bc71c8cd7e02be185
MD5 972dc1b904069aae213cb0459eec8ee8
BLAKE2b-256 53d77afe341bafa0163ae99753eb4f92c61bf02f01d7e5453aecb6daa288d3b5

See more details on using hashes here.

Provenance

The following attestation bundles were made for qgis_project-0.5.0.tar.gz:

Publisher: publish.yml on ColinMoldenhauer/qgis-project

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file qgis_project-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: qgis_project-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 25.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for qgis_project-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6958af18833b3164d188488c8fd8474310455a286f09c55d6de3d19d84f8a902
MD5 ffb83d93730c7b29e412ead39e13ad52
BLAKE2b-256 e2c63c47567749fa93f426832ab13d13802e7d3f0c19c83ea92cf2a2802c1dbf

See more details on using hashes here.

Provenance

The following attestation bundles were made for qgis_project-0.5.0-py3-none-any.whl:

Publisher: publish.yml on ColinMoldenhauer/qgis-project

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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