Skip to content

Short intro to using python in qgis #236

@pvgenuchten

Description

@pvgenuchten

Explain that both shapely and qgis python are a wrapper on geos, so method are similar, not equal

Guide users to python window in qgis

layer = QgsProject.instance().mapLayersByName("roads")[0]
feat = next(layer.getFeatures())

geom = feat.geometry()
print(geom.area())
print(geom.length())

geom.intersects(other)
geom.buffer(10, 8)
geom.union(other)

You can convert between QGIS and Shapely via WKB:

from shapely import from_wkb

qgs_geom = feature.geometry()
shapely_geom = from_wkb(bytes(qgs_geom.asWkb()))

Or even install qgis in the python environment

conda create -n qgis
conda activate qgis
conda install -c conda-forge qgis jupyterlab

from qgis.core import QgsApplication

QgsApplication.setPrefixPath("/usr", True) # path depends on installation
qgs = QgsApplication([], False)
qgs.initQgis()

from qgis.core import QgsVectorLayer

layer = QgsVectorLayer("roads.gpkg", "roads", "ogr")
print(layer.featureCount())

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions