brainstorming
brainstorming
You MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).
pyqgisUse when extending QGIS with Python plugins, automating map composition, or scripting QGIS processing algorithms. PyQGIS: QGIS Python API for custom tools, processing scripts, and plugin development.
Independent GitHub snapshot · Automated classification · Original documentation governs
name: pyqgis description: "Use when extending QGIS with Python plugins, automating map composition, or scripting QGIS processing algorithms. PyQGIS: QGIS Python API for custom tools, processing scripts, and plugin development." tags:
项目地址: https://github.com/qgis/QGIS
开发者手册: https://docs.qgis.org/4.2/en/docs/pyqgis_developer_cookbook/
Python API 参考: https://qgis.org/pyqgis/4.2/
许可证: GPL-2.0+
PyQGIS 是 QGIS 桌面 GIS 软件的 Python 绑定层,对应 QGIS 源码中 python/ 目录下的绑定代码。它将 QGIS 核心 C++ 类库完整暴露给 Python,使得开发者可以:
PyQGIS 主要包含以下模块:
| 模块 | 说明 |
|---|---|
qgis.core | 核心类——图层、要素、几何、坐标系、项目、Processing 等 |
qgis.gui | GUI 组件——地图画布、地图工具、符号选择器等 |
qgis.analysis | 空间分析——插值、网络分析、栅格计算等 |
qgis.processing | Processing 算法调用入口(processing.run()) |
qgis.server | QGIS Server Python 插件接口 |
qgis.3d | 3D 地图视图相关类 |
qgis.PyQt | PyQt5/PyQt6 兼容层 |
打开 QGIS 桌面 → 菜单「Plugins → Python Console」,即可直接使用 PyQGIS:
# QGIS 控制台中无需初始化,直接使用
layer = iface.activeLayer()
print(layer.name(), layer.featureCount())
在 QGIS 外部运行 Python 脚本时,需要先初始化 QGIS 应用:
import sys
from qgis.core import QgsApplication
# 初始化 QGIS(False 表示不使用 GUI)
qgs = QgsApplication([], False)
qgs.setPrefixPath("/usr", True) # Linux; Windows 示例:"C:/OSGeo4W/apps/qgis"
qgs.initQgis()
# ===== 在此处编写 PyQGIS 代码 =====
# 退出
qgs.exitQgis()
import sys
from qgis.core import QgsApplication
import processing
from processing.core.Processing import Processing
qgs = QgsApplication([], False)
qgs.setPrefixPath("/usr", True)
qgs.initQgis()
# 初始化 Processing 框架
Processing.initialize()
# 现在可以使用 processing.run()
result = processing.run("native:buffer", {
'INPUT': '/data/points.shp',
'DISTANCE': 100,
'OUTPUT': '/data/buffered.shp'
})
qgs.exitQgis()
export QT_QPA_PLATFORM=offscreen
python3 my_pyqgis_script.py
| 类 | 模块 | 用途 |
|---|---|---|
QgsApplication | qgis.core | QGIS 应用实例,初始化/退出 |
QgsProject | qgis.core | 项目管理(单例),加载/保存项目、管理图层 |
| 类 | 模块 | 用途 |
|---|---|---|
QgsVectorLayer | qgis.core | 矢量图层(Shapefile、GeoJSON、PostGIS、内存等) |
QgsFeature | qgis.core | 单个要素(几何 + 属性) |
QgsField / QgsFields | qgis.core | 属性字段定义与集合 |
QgsFeatureRequest | qgis.core | 要素查询过滤器(空间/属性条件) |
QgsVectorFileWriter | qgis.core | 矢量数据写出(SHP、GPKG、GeoJSON 等) |
QgsVectorLayerUtils | qgis.core | 矢量图层工具方法集 |
QgsSpatialIndex | qgis.core | 空间索引,加速空间查询 |
| 类 | 模块 | 用途 |
|---|---|---|
QgsRasterLayer | qgis.core | 栅格图层(GeoTIFF、VRT、WMS 等) |
QgsRasterPipe | qgis.core | 栅格渲染管线 |
QgsRasterFileWriter | qgis.core | 栅格数据写出 |
| 类 | 模块 | 用途 |
|---|---|---|
QgsGeometry | qgis.core | 几何对象封装(含空间操作方法) |
QgsPointXY | qgis.core | 二维点坐标 |
QgsPoint | qgis.core | 三维点坐标(含 Z / M) |
QgsRectangle | qgis.core | 矩形范围(Bounding Box) |
QgsWkbTypes | qgis.core | WKB 几何类型枚举 |
QgsDistanceArea | qgis.core | 距离和面积计算(支持椭球面) |
| 类 | 模块 | 用途 |
|---|---|---|
QgsCoordinateReferenceSystem | qgis.core | 坐标参考系(CRS)对象 |
QgsCoordinateTransform | qgis.core | 坐标变换器 |
QgsCoordinateTransformContext | qgis.core | 坐标变换上下文 |
| 类 | 模块 | 用途 |
|---|---|---|
QgsExpression | qgis.core | QGIS 表达式解析与求值 |
QgsExpressionContext | qgis.core | 表达式计算上下文 |
QgsExpressionContextUtils | qgis.core | 创建预设表达式上下文的工具方法 |
| 类 | 模块 | 用途 |
|---|---|---|
processing.run() | qgis.processing | 执行 Processing 算法的核心入口 |
QgsProcessingFeedback | qgis.core | 算法执行反馈(进度、日志) |
QgsProcessingContext | qgis.core | 算法执行上下文 |
QgsProcessingAlgorithm | qgis.core | 自定义算法基类 |
QgsProcessingParameterDefinition | qgis.core | 算法参数定义基类 |
| 类 | 模块 | 用途 |
|---|---|---|
QgsMapSettings | qgis.core | 地图渲染设置(范围、CRS、图层、尺寸) |
QgsMapRendererSequentialJob | qgis.core | 顺序渲染任务 |
QgsMapRendererParallelJob | qgis.core | 并行渲染任务 |
QgsLayoutExporter | qgis.core | 打印布局导出(PDF、PNG、SVG) |
QgsPrintLayout | qgis.core | 打印布局对象 |
| Provider 名称 | 说明 | 示例 URI |
|---|---|---|
ogr | OGR 矢量(SHP、GeoJSON、GPKG、FileGDB 等) | /path/to/file.shp |
gdal | GDAL 栅格(GeoTIFF、VRT、ECW 等) | /path/to/dem.tif |
postgres | PostgreSQL / PostGIS | dbname='gis' host=localhost port=5432 table="public"."layer" (geom) |
memory | 内存图层 | Point?crs=epsg:4326&field=id:integer&field=name:string(50) |
wms | WMS / WMTS 服务 | url=https://example.com/wms&layers=dem&crs=EPSG:4326 |
wfs | WFS 服务 | url=https://example.com/wfs&typename=buildings |
delimitedtext | CSV / 分隔文本 | file:///path/to/data.csv?delimiter=,&xField=lon&yField=lat&crs=epsg:4326 |
spatialite | SpatiaLite 数据库 | dbname='/path/to/db.sqlite' table="layer" (geometry) |
virtual | 虚拟图层(SQL 查询) | SQL 表达式 |
from qgis.core import QgsProject
project = QgsProject.instance()
# 读取项目
project.read("/path/to/project.qgs")
# 获取所有图层
layers = project.mapLayers() # 返回 dict {layer_id: layer}
# 保存项目
project.write("/path/to/output.qgs")
from qgis.core import QgsProject, QgsVectorLayer
project = QgsProject.instance()
layer = QgsVectorLayer("/data/roads.shp", "roads", "ogr")
if layer.isValid():
project.addMapLayer(layer)
# 移除图层
project.removeMapLayer(layer.id())
矢量图层与栅格图层操作的完整 API 与示例见 reference/vector-raster-layers.md
from qgis.core import QgsCoordinateReferenceSystem
# 从 EPSG 代码
crs = QgsCoordinateReferenceSystem("EPSG:4326")
# 从 WKT
crs = QgsCoordinateReferenceSystem.fromWkt(wkt_string)
# 从 Proj 字符串
crs = QgsCoordinateReferenceSystem.fromProj4(proj4_string)
# 获取图层 CRS
crs = layer.crs()
print(crs.authid()) # "EPSG:4326"
print(crs.description()) # "WGS 84"
from qgis.core import QgsCoordinateTransform, QgsCoordinateReferenceSystem, QgsProject
src_crs = QgsCoordinateReferenceSystem("EPSG:4326")
dst_crs = QgsCoordinateReferenceSystem("EPSG:3857")
transform = QgsCoordinateTransform(src_crs, dst_crs, QgsProject.instance())
# 变换单个点
point = transform.transform(QgsPointXY(116.4, 39.9))
# 变换几何
geom.transform(transform)
# 反向变换
point_back = transform.transform(point, QgsCoordinateTransform.ReverseTransform)
from qgis.core import QgsExpression, QgsExpressionContext, QgsExpressionContextUtils
# 简单计算
exp = QgsExpression("1 + 2 * 3")
result = exp.evaluate() # 7
# 基于要素的表达式
exp = QgsExpression('"population" / "area"')
context = QgsExpressionContext()
context.appendScopes(QgsExpressionContextUtils.globalProjectLayerScopes(layer))
for feature in layer.getFeatures():
context.setFeature(feature)
value = exp.evaluate(context)
print(feature["name"], value)
# 表达式过滤
exp = QgsExpression('"type" = \'residential\' AND "area" > 1000')
request = QgsFeatureRequest(exp)
for feature in layer.getFeatures(request):
print(feature.attributes())
from qgis.core import QgsExpression
layer.startEditing()
# 使用表达式批量更新字段
expression = QgsExpression('"length" * "width"')
context = QgsExpressionContext()
context.appendScopes(QgsExpressionContextUtils.globalProjectLayerScopes(layer))
field_index = layer.fields().indexOf("area")
for feature in layer.getFeatures():
context.setFeature(feature)
value = expression.evaluate(context)
layer.changeAttributeValue(feature.id(), field_index, value)
layer.commitChanges()
Processing 算法调用、地图渲染与导出、典型工作流示例见 reference/processing-rendering.md
QgsApplication 初始化;QGIS 控制台中可直接使用QgsVectorLayer / QgsRasterLayer 加载数据源processing.run() 调用算法(参数统一、算法丰富);精细操作使用 QgsGeometry 方法QgsVectorFileWriter 写出矢量,QgsRasterFileWriter 写出栅格qgs.exitQgis()layer.isValid(),无效时检查路径和 Provider。native:reprojectlayer 或 QgsCoordinateTransform 进行转换。startEditing() → 操作 → commitChanges();失败可 rollBack()。'memory:name' 可得到内存图层,避免创建临时文件。QgsDistanceArea 并设置椭球体,或使用 Processing 算法指定椭球体。QT_QPA_PLATFORM=offscreen。QgsSpatialIndex 加速空间查询;使用 QgsFeatureRequest 限制返回字段和范围。"field",字符串值用单引号 'value'。# 图层加载失败
layer = QgsVectorLayer(path, name, provider)
if not layer.isValid():
print(f"图层加载失败: {layer.error().message()}")
# Processing 算法错误
try:
result = processing.run("native:buffer", params, feedback=feedback)
except Exception as e:
print(f"算法执行失败: {e}")
# 编辑提交失败
if not layer.commitChanges():
errors = layer.commitErrors()
print(f"提交失败: {errors}")
layer.rollBack()