@dsh-gis/dsh-opengis-skill 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +19 -0
- package/CHANGELOG.md +96 -0
- package/LICENSE +21 -21
- package/README.md +91 -30
- package/THIRD_PARTY_NOTICES.md +37 -10
- package/assets/gdal/reference/raster-tools.md +538 -538
- package/assets/gdal/reference/vector-tools.md +149 -149
- package/assets/gdal-api/reference/raster-api.md +424 -424
- package/assets/gdal-api/reference/vector-api.md +724 -724
- package/assets/geometry-api-net/reference/geometry-types.md +243 -243
- package/assets/geopandas/reference/core-api.md +221 -221
- package/assets/geotools/reference/raster-rendering.md +108 -108
- package/assets/geotools/reference/vector-io.md +149 -149
- package/assets/jts/reference/geometry-operations.md +303 -303
- package/assets/mapsui/SKILL.md +283 -283
- package/assets/opengis-all/reference/data-stages.md +608 -608
- package/assets/opengis-all/reference/workflow-cheatsheets.md +324 -324
- package/assets/opengis-utils-for-java/reference/common-tasks.md +384 -384
- package/assets/opengis-utils-for-net/reference/code-patterns.md +124 -124
- package/assets/opengis-utils-for-net/reference/util-classes.md +291 -291
- package/assets/pyqgis/reference/processing-rendering.md +292 -292
- package/assets/pyqgis/reference/vector-raster-layers.md +318 -318
- package/assets/qgis-process/SKILL.md +414 -414
- package/assets/qgis-process/reference/algorithm-cheatsheet.md +194 -194
- package/assets/shapely/reference/operations-reference.md +199 -199
- package/cordis.patch.yml +6 -3
- package/lib/catalog.d.ts +50 -0
- package/lib/catalog.js +238 -0
- package/lib/index.d.ts +47 -6
- package/lib/index.js +32 -44
- package/lib/markdown-links.d.ts +11 -0
- package/lib/markdown-links.js +103 -0
- package/lib/skill-file.js +104 -17
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +11 -5
- package/upstream.json +64 -2
|
@@ -1,292 +1,292 @@
|
|
|
1
|
-
# PyQGIS Processing, Rendering and Workflows Reference
|
|
2
|
-
|
|
3
|
-
> 这是 [../SKILL.md](../SKILL.md) 的补充文件,收录 Processing 算法调用、地图渲染/导出与典型工作流示例。
|
|
4
|
-
|
|
5
|
-
---
|
|
6
|
-
|
|
7
|
-
## Processing 算法调用
|
|
8
|
-
|
|
9
|
-
### 基本调用
|
|
10
|
-
|
|
11
|
-
```python
|
|
12
|
-
import processing
|
|
13
|
-
|
|
14
|
-
# 运行缓冲区分析
|
|
15
|
-
result = processing.run("native:buffer", {
|
|
16
|
-
'INPUT': '/data/roads.shp',
|
|
17
|
-
'DISTANCE': 50,
|
|
18
|
-
'SEGMENTS': 10,
|
|
19
|
-
'END_CAP_STYLE': 0, # 0=圆形, 1=平头, 2=方形
|
|
20
|
-
'JOIN_STYLE': 0, # 0=圆形, 1=斜角, 2=斜切
|
|
21
|
-
'MITER_LIMIT': 2,
|
|
22
|
-
'DISSOLVE': False,
|
|
23
|
-
'OUTPUT': '/data/roads_buffer.shp'
|
|
24
|
-
})
|
|
25
|
-
print(result['OUTPUT']) # 输出文件路径
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
### 带反馈对象
|
|
29
|
-
|
|
30
|
-
```python
|
|
31
|
-
from qgis.core import QgsProcessingFeedback
|
|
32
|
-
|
|
33
|
-
feedback = QgsProcessingFeedback()
|
|
34
|
-
|
|
35
|
-
result = processing.run("native:dissolve", {
|
|
36
|
-
'INPUT': '/data/polygons.shp',
|
|
37
|
-
'FIELD': ['type'],
|
|
38
|
-
'OUTPUT': '/data/dissolved.gpkg'
|
|
39
|
-
}, feedback=feedback)
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
### 使用内存输出
|
|
43
|
-
|
|
44
|
-
```python
|
|
45
|
-
# OUTPUT 设为 'memory:' 前缀可得到内存图层
|
|
46
|
-
result = processing.run("native:buffer", {
|
|
47
|
-
'INPUT': layer,
|
|
48
|
-
'DISTANCE': 100,
|
|
49
|
-
'OUTPUT': 'memory:buffered'
|
|
50
|
-
})
|
|
51
|
-
buffered_layer = result['OUTPUT'] # QgsVectorLayer(内存)
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
### 图层对象作为输入
|
|
55
|
-
|
|
56
|
-
```python
|
|
57
|
-
# 可以直接传入 QgsVectorLayer / QgsRasterLayer 对象
|
|
58
|
-
result = processing.run("native:clip", {
|
|
59
|
-
'INPUT': input_layer, # QgsVectorLayer 对象
|
|
60
|
-
'OVERLAY': clip_layer, # QgsVectorLayer 对象
|
|
61
|
-
'OUTPUT': 'memory:clipped'
|
|
62
|
-
})
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
### 常用 Processing 算法
|
|
66
|
-
|
|
67
|
-
| 算法 ID | 说明 | 关键参数 |
|
|
68
|
-
|---------|------|---------|
|
|
69
|
-
| `native:buffer` | 缓冲区 | INPUT, DISTANCE, SEGMENTS, DISSOLVE |
|
|
70
|
-
| `native:clip` | 矢量裁剪 | INPUT, OVERLAY |
|
|
71
|
-
| `native:dissolve` | 融合 | INPUT, FIELD |
|
|
72
|
-
| `native:intersection` | 交集 | INPUT, OVERLAY |
|
|
73
|
-
| `native:union` | 联合 | INPUT, OVERLAY |
|
|
74
|
-
| `native:difference` | 差集 | INPUT, OVERLAY |
|
|
75
|
-
| `native:centroids` | 质心 | INPUT |
|
|
76
|
-
| `native:reprojectlayer` | 重投影 | INPUT, TARGET_CRS |
|
|
77
|
-
| `native:mergevectorlayers` | 合并图层 | LAYERS |
|
|
78
|
-
| `native:fixgeometries` | 修复几何 | INPUT |
|
|
79
|
-
| `native:extractbyattribute` | 按属性提取 | INPUT, FIELD, OPERATOR, VALUE |
|
|
80
|
-
| `native:extractbylocation` | 按位置提取 | INPUT, INTERSECT, PREDICATE |
|
|
81
|
-
| `native:joinattributesbylocation` | 空间连接 | INPUT, JOIN, PREDICATE, METHOD |
|
|
82
|
-
| `native:fieldcalculator` | 字段计算器 | INPUT, FIELD_NAME, FORMULA |
|
|
83
|
-
| `gdal:cliprasterbyextent` | 栅格按范围裁剪 | INPUT, EXTENT |
|
|
84
|
-
| `gdal:cliprasterbymask` | 栅格按掩膜裁剪 | INPUT, MASK |
|
|
85
|
-
| `gdal:warpreproject` | 栅格重投影 | INPUT, TARGET_CRS |
|
|
86
|
-
| `gdal:merge` | 栅格合并 | INPUT |
|
|
87
|
-
| `gdal:contour` | 等值线提取 | INPUT, INTERVAL |
|
|
88
|
-
| `gdal:polygonize` | 栅格转矢量 | INPUT |
|
|
89
|
-
| `gdal:rasterize` | 矢量转栅格 | INPUT, FIELD |
|
|
90
|
-
|
|
91
|
-
### 查询可用算法
|
|
92
|
-
|
|
93
|
-
```python
|
|
94
|
-
# 在 QGIS 控制台中列出所有算法
|
|
95
|
-
for alg in QgsApplication.processingRegistry().algorithms():
|
|
96
|
-
print(alg.id(), "-", alg.displayName())
|
|
97
|
-
|
|
98
|
-
# 搜索算法
|
|
99
|
-
for alg in QgsApplication.processingRegistry().algorithms():
|
|
100
|
-
if 'buffer' in alg.id().lower():
|
|
101
|
-
print(alg.id(), alg.displayName())
|
|
102
|
-
```
|
|
103
|
-
|
|
104
|
-
---
|
|
105
|
-
|
|
106
|
-
## 地图渲染与导出
|
|
107
|
-
|
|
108
|
-
### 渲染为图片
|
|
109
|
-
|
|
110
|
-
```python
|
|
111
|
-
from qgis.core import QgsMapSettings, QgsMapRendererSequentialJob
|
|
112
|
-
from qgis.PyQt.QtCore import QSize
|
|
113
|
-
|
|
114
|
-
settings = QgsMapSettings()
|
|
115
|
-
settings.setLayers([layer])
|
|
116
|
-
settings.setDestinationCrs(layer.crs())
|
|
117
|
-
settings.setExtent(layer.extent())
|
|
118
|
-
settings.setOutputSize(QSize(1920, 1080))
|
|
119
|
-
|
|
120
|
-
job = QgsMapRendererSequentialJob(settings)
|
|
121
|
-
job.start()
|
|
122
|
-
job.waitForFinished()
|
|
123
|
-
|
|
124
|
-
image = job.renderedImage()
|
|
125
|
-
image.save("/data/map_output.png")
|
|
126
|
-
```
|
|
127
|
-
|
|
128
|
-
### 使用打印布局导出 PDF
|
|
129
|
-
|
|
130
|
-
```python
|
|
131
|
-
from qgis.core import (
|
|
132
|
-
QgsProject, QgsPrintLayout, QgsLayoutItemMap,
|
|
133
|
-
QgsLayoutExporter, QgsLayoutSize, QgsUnitTypes
|
|
134
|
-
)
|
|
135
|
-
|
|
136
|
-
project = QgsProject.instance()
|
|
137
|
-
layout = QgsPrintLayout(project)
|
|
138
|
-
layout.initializeDefaults()
|
|
139
|
-
|
|
140
|
-
# 添加地图项
|
|
141
|
-
map_item = QgsLayoutItemMap(layout)
|
|
142
|
-
map_item.setRect(20, 20, 200, 150)
|
|
143
|
-
map_item.setExtent(layer.extent())
|
|
144
|
-
map_item.setLayers([layer])
|
|
145
|
-
layout.addLayoutItem(map_item)
|
|
146
|
-
|
|
147
|
-
# 导出 PDF
|
|
148
|
-
exporter = QgsLayoutExporter(layout)
|
|
149
|
-
pdf_settings = QgsLayoutExporter.PdfExportSettings()
|
|
150
|
-
exporter.exportToPdf("/data/output.pdf", pdf_settings)
|
|
151
|
-
```
|
|
152
|
-
|
|
153
|
-
---
|
|
154
|
-
|
|
155
|
-
## 典型工作流示例
|
|
156
|
-
|
|
157
|
-
### 示例 1:矢量格式转换(SHP → GeoPackage)
|
|
158
|
-
|
|
159
|
-
```python
|
|
160
|
-
from qgis.core import QgsVectorLayer, QgsVectorFileWriter, QgsProject
|
|
161
|
-
|
|
162
|
-
layer = QgsVectorLayer("/data/input.shp", "input", "ogr")
|
|
163
|
-
|
|
164
|
-
options = QgsVectorFileWriter.SaveVectorOptions()
|
|
165
|
-
options.driverName = "GPKG"
|
|
166
|
-
options.fileEncoding = "UTF-8"
|
|
167
|
-
|
|
168
|
-
QgsVectorFileWriter.writeAsVectorFormatV3(
|
|
169
|
-
layer,
|
|
170
|
-
"/data/output.gpkg",
|
|
171
|
-
QgsProject.instance().transformContext(),
|
|
172
|
-
options
|
|
173
|
-
)
|
|
174
|
-
```
|
|
175
|
-
|
|
176
|
-
### 示例 2:批量缓冲区 + 融合
|
|
177
|
-
|
|
178
|
-
```python
|
|
179
|
-
import processing
|
|
180
|
-
|
|
181
|
-
# 缓冲区
|
|
182
|
-
result1 = processing.run("native:buffer", {
|
|
183
|
-
'INPUT': '/data/points.shp',
|
|
184
|
-
'DISTANCE': 500,
|
|
185
|
-
'SEGMENTS': 16,
|
|
186
|
-
'OUTPUT': 'memory:buffered'
|
|
187
|
-
})
|
|
188
|
-
|
|
189
|
-
# 融合
|
|
190
|
-
result2 = processing.run("native:dissolve", {
|
|
191
|
-
'INPUT': result1['OUTPUT'],
|
|
192
|
-
'OUTPUT': '/data/dissolved.gpkg'
|
|
193
|
-
})
|
|
194
|
-
```
|
|
195
|
-
|
|
196
|
-
### 示例 3:坐标系转换 + 属性过滤
|
|
197
|
-
|
|
198
|
-
```python
|
|
199
|
-
import processing
|
|
200
|
-
|
|
201
|
-
# 重投影到 CGCS2000
|
|
202
|
-
result1 = processing.run("native:reprojectlayer", {
|
|
203
|
-
'INPUT': '/data/input_wgs84.shp',
|
|
204
|
-
'TARGET_CRS': 'EPSG:4490',
|
|
205
|
-
'OUTPUT': 'memory:reprojected'
|
|
206
|
-
})
|
|
207
|
-
|
|
208
|
-
# 按属性提取
|
|
209
|
-
result2 = processing.run("native:extractbyattribute", {
|
|
210
|
-
'INPUT': result1['OUTPUT'],
|
|
211
|
-
'FIELD': 'type',
|
|
212
|
-
'OPERATOR': 0, # 0=等于
|
|
213
|
-
'VALUE': 'road',
|
|
214
|
-
'OUTPUT': '/data/roads_cgcs2000.gpkg'
|
|
215
|
-
})
|
|
216
|
-
```
|
|
217
|
-
|
|
218
|
-
### 示例 4:栅格裁剪 + 坡度分析
|
|
219
|
-
|
|
220
|
-
```python
|
|
221
|
-
import processing
|
|
222
|
-
|
|
223
|
-
# 按掩膜裁剪 DEM(栅格算法使用 TEMPORARY_OUTPUT 生成临时文件)
|
|
224
|
-
result1 = processing.run("gdal:cliprasterbymask", {
|
|
225
|
-
'INPUT': '/data/dem.tif',
|
|
226
|
-
'MASK': '/data/boundary.shp',
|
|
227
|
-
'OUTPUT': 'TEMPORARY_OUTPUT' # 栅格输出不支持 memory:,使用 TEMPORARY_OUTPUT 创建临时文件
|
|
228
|
-
})
|
|
229
|
-
|
|
230
|
-
# 坡度分析
|
|
231
|
-
result2 = processing.run("gdal:slope", {
|
|
232
|
-
'INPUT': result1['OUTPUT'],
|
|
233
|
-
'OUTPUT': '/data/slope.tif'
|
|
234
|
-
})
|
|
235
|
-
```
|
|
236
|
-
|
|
237
|
-
### 示例 5:从 CSV 创建矢量图层并导出
|
|
238
|
-
|
|
239
|
-
```python
|
|
240
|
-
from qgis.core import QgsVectorLayer, QgsVectorFileWriter, QgsProject
|
|
241
|
-
|
|
242
|
-
# 从 CSV 加载(指定经纬度字段)
|
|
243
|
-
uri = "file:///data/stations.csv?delimiter=,&xField=longitude&yField=latitude&crs=epsg:4326"
|
|
244
|
-
layer = QgsVectorLayer(uri, "stations", "delimitedtext")
|
|
245
|
-
|
|
246
|
-
if layer.isValid():
|
|
247
|
-
options = QgsVectorFileWriter.SaveVectorOptions()
|
|
248
|
-
options.driverName = "GeoJSON"
|
|
249
|
-
QgsVectorFileWriter.writeAsVectorFormatV3(
|
|
250
|
-
layer,
|
|
251
|
-
"/data/stations.geojson",
|
|
252
|
-
QgsProject.instance().transformContext(),
|
|
253
|
-
options
|
|
254
|
-
)
|
|
255
|
-
```
|
|
256
|
-
|
|
257
|
-
### 示例 6:独立脚本完整示例
|
|
258
|
-
|
|
259
|
-
```python
|
|
260
|
-
"""独立 PyQGIS 脚本:加载 SHP、缓冲区分析、导出 GeoJSON"""
|
|
261
|
-
import sys
|
|
262
|
-
from qgis.core import (
|
|
263
|
-
QgsApplication, QgsVectorLayer, QgsProject
|
|
264
|
-
)
|
|
265
|
-
import processing
|
|
266
|
-
from processing.core.Processing import Processing
|
|
267
|
-
|
|
268
|
-
# 初始化
|
|
269
|
-
qgs = QgsApplication([], False)
|
|
270
|
-
qgs.setPrefixPath("/usr", True)
|
|
271
|
-
qgs.initQgis()
|
|
272
|
-
Processing.initialize()
|
|
273
|
-
|
|
274
|
-
# 加载数据
|
|
275
|
-
layer = QgsVectorLayer("/data/buildings.shp", "buildings", "ogr")
|
|
276
|
-
assert layer.isValid(), "图层加载失败"
|
|
277
|
-
|
|
278
|
-
# 缓冲区分析
|
|
279
|
-
result = processing.run("native:buffer", {
|
|
280
|
-
'INPUT': layer,
|
|
281
|
-
'DISTANCE': 200,
|
|
282
|
-
'OUTPUT': '/data/buildings_buffer.geojson'
|
|
283
|
-
})
|
|
284
|
-
|
|
285
|
-
print(f"输出文件: {result['OUTPUT']}")
|
|
286
|
-
|
|
287
|
-
# 退出
|
|
288
|
-
qgs.exitQgis()
|
|
289
|
-
```
|
|
290
|
-
|
|
291
|
-
---
|
|
292
|
-
|
|
1
|
+
# PyQGIS Processing, Rendering and Workflows Reference
|
|
2
|
+
|
|
3
|
+
> 这是 [../SKILL.md](../SKILL.md) 的补充文件,收录 Processing 算法调用、地图渲染/导出与典型工作流示例。
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Processing 算法调用
|
|
8
|
+
|
|
9
|
+
### 基本调用
|
|
10
|
+
|
|
11
|
+
```python
|
|
12
|
+
import processing
|
|
13
|
+
|
|
14
|
+
# 运行缓冲区分析
|
|
15
|
+
result = processing.run("native:buffer", {
|
|
16
|
+
'INPUT': '/data/roads.shp',
|
|
17
|
+
'DISTANCE': 50,
|
|
18
|
+
'SEGMENTS': 10,
|
|
19
|
+
'END_CAP_STYLE': 0, # 0=圆形, 1=平头, 2=方形
|
|
20
|
+
'JOIN_STYLE': 0, # 0=圆形, 1=斜角, 2=斜切
|
|
21
|
+
'MITER_LIMIT': 2,
|
|
22
|
+
'DISSOLVE': False,
|
|
23
|
+
'OUTPUT': '/data/roads_buffer.shp'
|
|
24
|
+
})
|
|
25
|
+
print(result['OUTPUT']) # 输出文件路径
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### 带反馈对象
|
|
29
|
+
|
|
30
|
+
```python
|
|
31
|
+
from qgis.core import QgsProcessingFeedback
|
|
32
|
+
|
|
33
|
+
feedback = QgsProcessingFeedback()
|
|
34
|
+
|
|
35
|
+
result = processing.run("native:dissolve", {
|
|
36
|
+
'INPUT': '/data/polygons.shp',
|
|
37
|
+
'FIELD': ['type'],
|
|
38
|
+
'OUTPUT': '/data/dissolved.gpkg'
|
|
39
|
+
}, feedback=feedback)
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### 使用内存输出
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
# OUTPUT 设为 'memory:' 前缀可得到内存图层
|
|
46
|
+
result = processing.run("native:buffer", {
|
|
47
|
+
'INPUT': layer,
|
|
48
|
+
'DISTANCE': 100,
|
|
49
|
+
'OUTPUT': 'memory:buffered'
|
|
50
|
+
})
|
|
51
|
+
buffered_layer = result['OUTPUT'] # QgsVectorLayer(内存)
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### 图层对象作为输入
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
# 可以直接传入 QgsVectorLayer / QgsRasterLayer 对象
|
|
58
|
+
result = processing.run("native:clip", {
|
|
59
|
+
'INPUT': input_layer, # QgsVectorLayer 对象
|
|
60
|
+
'OVERLAY': clip_layer, # QgsVectorLayer 对象
|
|
61
|
+
'OUTPUT': 'memory:clipped'
|
|
62
|
+
})
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### 常用 Processing 算法
|
|
66
|
+
|
|
67
|
+
| 算法 ID | 说明 | 关键参数 |
|
|
68
|
+
|---------|------|---------|
|
|
69
|
+
| `native:buffer` | 缓冲区 | INPUT, DISTANCE, SEGMENTS, DISSOLVE |
|
|
70
|
+
| `native:clip` | 矢量裁剪 | INPUT, OVERLAY |
|
|
71
|
+
| `native:dissolve` | 融合 | INPUT, FIELD |
|
|
72
|
+
| `native:intersection` | 交集 | INPUT, OVERLAY |
|
|
73
|
+
| `native:union` | 联合 | INPUT, OVERLAY |
|
|
74
|
+
| `native:difference` | 差集 | INPUT, OVERLAY |
|
|
75
|
+
| `native:centroids` | 质心 | INPUT |
|
|
76
|
+
| `native:reprojectlayer` | 重投影 | INPUT, TARGET_CRS |
|
|
77
|
+
| `native:mergevectorlayers` | 合并图层 | LAYERS |
|
|
78
|
+
| `native:fixgeometries` | 修复几何 | INPUT |
|
|
79
|
+
| `native:extractbyattribute` | 按属性提取 | INPUT, FIELD, OPERATOR, VALUE |
|
|
80
|
+
| `native:extractbylocation` | 按位置提取 | INPUT, INTERSECT, PREDICATE |
|
|
81
|
+
| `native:joinattributesbylocation` | 空间连接 | INPUT, JOIN, PREDICATE, METHOD |
|
|
82
|
+
| `native:fieldcalculator` | 字段计算器 | INPUT, FIELD_NAME, FORMULA |
|
|
83
|
+
| `gdal:cliprasterbyextent` | 栅格按范围裁剪 | INPUT, EXTENT |
|
|
84
|
+
| `gdal:cliprasterbymask` | 栅格按掩膜裁剪 | INPUT, MASK |
|
|
85
|
+
| `gdal:warpreproject` | 栅格重投影 | INPUT, TARGET_CRS |
|
|
86
|
+
| `gdal:merge` | 栅格合并 | INPUT |
|
|
87
|
+
| `gdal:contour` | 等值线提取 | INPUT, INTERVAL |
|
|
88
|
+
| `gdal:polygonize` | 栅格转矢量 | INPUT |
|
|
89
|
+
| `gdal:rasterize` | 矢量转栅格 | INPUT, FIELD |
|
|
90
|
+
|
|
91
|
+
### 查询可用算法
|
|
92
|
+
|
|
93
|
+
```python
|
|
94
|
+
# 在 QGIS 控制台中列出所有算法
|
|
95
|
+
for alg in QgsApplication.processingRegistry().algorithms():
|
|
96
|
+
print(alg.id(), "-", alg.displayName())
|
|
97
|
+
|
|
98
|
+
# 搜索算法
|
|
99
|
+
for alg in QgsApplication.processingRegistry().algorithms():
|
|
100
|
+
if 'buffer' in alg.id().lower():
|
|
101
|
+
print(alg.id(), alg.displayName())
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
---
|
|
105
|
+
|
|
106
|
+
## 地图渲染与导出
|
|
107
|
+
|
|
108
|
+
### 渲染为图片
|
|
109
|
+
|
|
110
|
+
```python
|
|
111
|
+
from qgis.core import QgsMapSettings, QgsMapRendererSequentialJob
|
|
112
|
+
from qgis.PyQt.QtCore import QSize
|
|
113
|
+
|
|
114
|
+
settings = QgsMapSettings()
|
|
115
|
+
settings.setLayers([layer])
|
|
116
|
+
settings.setDestinationCrs(layer.crs())
|
|
117
|
+
settings.setExtent(layer.extent())
|
|
118
|
+
settings.setOutputSize(QSize(1920, 1080))
|
|
119
|
+
|
|
120
|
+
job = QgsMapRendererSequentialJob(settings)
|
|
121
|
+
job.start()
|
|
122
|
+
job.waitForFinished()
|
|
123
|
+
|
|
124
|
+
image = job.renderedImage()
|
|
125
|
+
image.save("/data/map_output.png")
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### 使用打印布局导出 PDF
|
|
129
|
+
|
|
130
|
+
```python
|
|
131
|
+
from qgis.core import (
|
|
132
|
+
QgsProject, QgsPrintLayout, QgsLayoutItemMap,
|
|
133
|
+
QgsLayoutExporter, QgsLayoutSize, QgsUnitTypes
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
project = QgsProject.instance()
|
|
137
|
+
layout = QgsPrintLayout(project)
|
|
138
|
+
layout.initializeDefaults()
|
|
139
|
+
|
|
140
|
+
# 添加地图项
|
|
141
|
+
map_item = QgsLayoutItemMap(layout)
|
|
142
|
+
map_item.setRect(20, 20, 200, 150)
|
|
143
|
+
map_item.setExtent(layer.extent())
|
|
144
|
+
map_item.setLayers([layer])
|
|
145
|
+
layout.addLayoutItem(map_item)
|
|
146
|
+
|
|
147
|
+
# 导出 PDF
|
|
148
|
+
exporter = QgsLayoutExporter(layout)
|
|
149
|
+
pdf_settings = QgsLayoutExporter.PdfExportSettings()
|
|
150
|
+
exporter.exportToPdf("/data/output.pdf", pdf_settings)
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
---
|
|
154
|
+
|
|
155
|
+
## 典型工作流示例
|
|
156
|
+
|
|
157
|
+
### 示例 1:矢量格式转换(SHP → GeoPackage)
|
|
158
|
+
|
|
159
|
+
```python
|
|
160
|
+
from qgis.core import QgsVectorLayer, QgsVectorFileWriter, QgsProject
|
|
161
|
+
|
|
162
|
+
layer = QgsVectorLayer("/data/input.shp", "input", "ogr")
|
|
163
|
+
|
|
164
|
+
options = QgsVectorFileWriter.SaveVectorOptions()
|
|
165
|
+
options.driverName = "GPKG"
|
|
166
|
+
options.fileEncoding = "UTF-8"
|
|
167
|
+
|
|
168
|
+
QgsVectorFileWriter.writeAsVectorFormatV3(
|
|
169
|
+
layer,
|
|
170
|
+
"/data/output.gpkg",
|
|
171
|
+
QgsProject.instance().transformContext(),
|
|
172
|
+
options
|
|
173
|
+
)
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### 示例 2:批量缓冲区 + 融合
|
|
177
|
+
|
|
178
|
+
```python
|
|
179
|
+
import processing
|
|
180
|
+
|
|
181
|
+
# 缓冲区
|
|
182
|
+
result1 = processing.run("native:buffer", {
|
|
183
|
+
'INPUT': '/data/points.shp',
|
|
184
|
+
'DISTANCE': 500,
|
|
185
|
+
'SEGMENTS': 16,
|
|
186
|
+
'OUTPUT': 'memory:buffered'
|
|
187
|
+
})
|
|
188
|
+
|
|
189
|
+
# 融合
|
|
190
|
+
result2 = processing.run("native:dissolve", {
|
|
191
|
+
'INPUT': result1['OUTPUT'],
|
|
192
|
+
'OUTPUT': '/data/dissolved.gpkg'
|
|
193
|
+
})
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
### 示例 3:坐标系转换 + 属性过滤
|
|
197
|
+
|
|
198
|
+
```python
|
|
199
|
+
import processing
|
|
200
|
+
|
|
201
|
+
# 重投影到 CGCS2000
|
|
202
|
+
result1 = processing.run("native:reprojectlayer", {
|
|
203
|
+
'INPUT': '/data/input_wgs84.shp',
|
|
204
|
+
'TARGET_CRS': 'EPSG:4490',
|
|
205
|
+
'OUTPUT': 'memory:reprojected'
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
# 按属性提取
|
|
209
|
+
result2 = processing.run("native:extractbyattribute", {
|
|
210
|
+
'INPUT': result1['OUTPUT'],
|
|
211
|
+
'FIELD': 'type',
|
|
212
|
+
'OPERATOR': 0, # 0=等于
|
|
213
|
+
'VALUE': 'road',
|
|
214
|
+
'OUTPUT': '/data/roads_cgcs2000.gpkg'
|
|
215
|
+
})
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
### 示例 4:栅格裁剪 + 坡度分析
|
|
219
|
+
|
|
220
|
+
```python
|
|
221
|
+
import processing
|
|
222
|
+
|
|
223
|
+
# 按掩膜裁剪 DEM(栅格算法使用 TEMPORARY_OUTPUT 生成临时文件)
|
|
224
|
+
result1 = processing.run("gdal:cliprasterbymask", {
|
|
225
|
+
'INPUT': '/data/dem.tif',
|
|
226
|
+
'MASK': '/data/boundary.shp',
|
|
227
|
+
'OUTPUT': 'TEMPORARY_OUTPUT' # 栅格输出不支持 memory:,使用 TEMPORARY_OUTPUT 创建临时文件
|
|
228
|
+
})
|
|
229
|
+
|
|
230
|
+
# 坡度分析
|
|
231
|
+
result2 = processing.run("gdal:slope", {
|
|
232
|
+
'INPUT': result1['OUTPUT'],
|
|
233
|
+
'OUTPUT': '/data/slope.tif'
|
|
234
|
+
})
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
### 示例 5:从 CSV 创建矢量图层并导出
|
|
238
|
+
|
|
239
|
+
```python
|
|
240
|
+
from qgis.core import QgsVectorLayer, QgsVectorFileWriter, QgsProject
|
|
241
|
+
|
|
242
|
+
# 从 CSV 加载(指定经纬度字段)
|
|
243
|
+
uri = "file:///data/stations.csv?delimiter=,&xField=longitude&yField=latitude&crs=epsg:4326"
|
|
244
|
+
layer = QgsVectorLayer(uri, "stations", "delimitedtext")
|
|
245
|
+
|
|
246
|
+
if layer.isValid():
|
|
247
|
+
options = QgsVectorFileWriter.SaveVectorOptions()
|
|
248
|
+
options.driverName = "GeoJSON"
|
|
249
|
+
QgsVectorFileWriter.writeAsVectorFormatV3(
|
|
250
|
+
layer,
|
|
251
|
+
"/data/stations.geojson",
|
|
252
|
+
QgsProject.instance().transformContext(),
|
|
253
|
+
options
|
|
254
|
+
)
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
### 示例 6:独立脚本完整示例
|
|
258
|
+
|
|
259
|
+
```python
|
|
260
|
+
"""独立 PyQGIS 脚本:加载 SHP、缓冲区分析、导出 GeoJSON"""
|
|
261
|
+
import sys
|
|
262
|
+
from qgis.core import (
|
|
263
|
+
QgsApplication, QgsVectorLayer, QgsProject
|
|
264
|
+
)
|
|
265
|
+
import processing
|
|
266
|
+
from processing.core.Processing import Processing
|
|
267
|
+
|
|
268
|
+
# 初始化
|
|
269
|
+
qgs = QgsApplication([], False)
|
|
270
|
+
qgs.setPrefixPath("/usr", True)
|
|
271
|
+
qgs.initQgis()
|
|
272
|
+
Processing.initialize()
|
|
273
|
+
|
|
274
|
+
# 加载数据
|
|
275
|
+
layer = QgsVectorLayer("/data/buildings.shp", "buildings", "ogr")
|
|
276
|
+
assert layer.isValid(), "图层加载失败"
|
|
277
|
+
|
|
278
|
+
# 缓冲区分析
|
|
279
|
+
result = processing.run("native:buffer", {
|
|
280
|
+
'INPUT': layer,
|
|
281
|
+
'DISTANCE': 200,
|
|
282
|
+
'OUTPUT': '/data/buildings_buffer.geojson'
|
|
283
|
+
})
|
|
284
|
+
|
|
285
|
+
print(f"输出文件: {result['OUTPUT']}")
|
|
286
|
+
|
|
287
|
+
# 退出
|
|
288
|
+
qgs.exitQgis()
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
---
|
|
292
|
+
|