Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
luolingchun committed Jul 21, 2024
1 parent ed800d1 commit dd3fa7a
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 58 deletions.
14 changes: 7 additions & 7 deletions docs/11-表达式,过滤和计算值.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,17 @@ exp.evaluate(context)
下面是一个比较完整的例子,说明如何在矢量图层的上下文中使用表达式,以计算新的字段值:

```python
from qgis.PyQt.QtCore import QVariant
from qgis.PyQt.QtCore import QMetaType

# 创建矢量图层
vl = QgsVectorLayer("Point", "Companies", "memory")
pr = vl.dataProvider()
pr.addAttributes([QgsField("Name", QVariant.String),
QgsField("Employees", QVariant.Int),
QgsField("Revenue", QVariant.Double),
QgsField("Rev. per employee", QVariant.Double),
QgsField("Sum", QVariant.Double),
QgsField("Fun", QVariant.Double)])
pr.addAttributes([QgsField("Name", QMetaType.Type.QString),
QgsField("Employees", QMetaType.Type.Int),
QgsField("Revenue", QMetaType.Type.Double),
QgsField("Rev. per employee", QMetaType.Type.Double),
QgsField("Sum", QMetaType.Type.Double),
QgsField("Fun", QMetaType.Type.Double)])
vl.updateFields()

# 将数据添加到前三个字段
Expand Down
2 changes: 1 addition & 1 deletion docs/12-读取和存储设置.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ from qgis.core import (

[`value()`](https://qgis.org/pyqgis/master/core/QgsSettings.html#qgis.core.QgsSettings.value)方法的第二个参数是可选的,如果没有,则返回默认值。

通过`global_settings.ini`文件在全局设置中预配置默认值,更多详情查看[Deploying QGIS within an organization](https://docs.qgis.org/testing/en/docs/user_manual/introduction/qgis_configuration.html#deploying-organization)
通过`qgis_global_settings.ini`文件在全局设置中预配置默认值,更多详情查看[Deploying QGIS within an organization](https://docs.qgis.org/testing/en/docs/user_manual/introduction/qgis_configuration.html#deploying-organization)

- **项目设置:** 在不同项目之间有所不同,因此它们与项目文件相关联。以地图画布背景颜色或目标坐标参考系统(CRS)作为例子——白色背景和WGS84可能适用于一个项目,而黄色背景和UTM投影适用于另一个项目。

Expand Down
2 changes: 1 addition & 1 deletion docs/14-认证基础.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ assert (newAuthCfgId)
[认证方法](https://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/authentication.html#term-Authentication-Method)在认证管理器初始化时动态加载。可用的认证方法列表如下:

1. `Basic` 用户和密码验证
2. `Esri-Token` ESRI token 基础认证
2. `EsriToken` ESRI token 基础认证
3. `Identity-Cert` 身份证书认证
4. `PKI-Paths` PKI路径认证
5. `PKI-PKCS#12` PKI PKCS#12认证
Expand Down
25 changes: 16 additions & 9 deletions docs/15-任务——在后台做繁重的工作.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

```python
from qgis.core import (
QgsProcessingContext,
QgsTaskManager,
QgsTask,
QgsProcessingAlgRunnerTask,
Qgis,
QgsProcessingFeedback,
QgsApplication,
QgsMessageLog,
Qgis,
QgsApplication,
QgsMessageLog,
QgsProcessingAlgRunnerTask,
QgsProcessingContext,
QgsProcessingFeedback,
QgsProject,
QgsTask,
QgsTaskManager,
)
```

Expand Down Expand Up @@ -44,14 +45,15 @@ from qgis.core import (
pass

task = QgsTask.fromFunction('heavy function', heavyFunction,
onfinished=workdone)
on_finished=workdone)
```

- 从处理算法创建任务

```python
params = dict()
context = QgsProcessingContext()
context.setProject(QgsProject.instance())
feedback = QgsProcessingFeedback()

buffer_alg = QgsApplication.instance().processingRegistry().algorithmById('native:buffer')
Expand All @@ -62,6 +64,11 @@ from qgis.core import (

任何后台任务(无论如何创建)决不能使用任何主线程上的QObject,比如访问QgsVectorLayer, QgsProject或者执行任何GUI操作——比如创建新的部件或者与现有部件交互。只能从主线程访问或修改Qt控件。在任务启动之前,必须复制任务中使用的数据。试图从后台线程使用它们将导致崩溃。

此外,请始终确保并 [context](https://qgis.org/pyqgis/master/core/QgsProcessingContext.html#qgis.core.QgsProcessingContext) 和 [feedback](https://qgis.org/pyqgis/3.34/core/QgsProcessingFeedback.html#qgis.core.QgsProcessingFeedback) 至少与使用它们的任务一样长。如果在完成任务后,QGSTaskManager无法访问计划任务的上下文和反馈,则QGIS将崩溃。

!!! info

在调用 `QgsProcessingContext` 后不久调用 [setProject()](https://qgis.org/pyqgis/master/core/QgsProcessingContext.html#qgis.core.QgsProcessingContext.setProject) 是一种常见的模式。这允许任务及其回调函数使用大多数项目范围的设置。这在回调函数中使用空间图层时特别有价值。

可以使用[`QgsTask`](https://qgis.org/pyqgis/master/core/QgsTask.html#qgis.core.QgsTask)中的[`addSubTask()`](https://qgis.org/pyqgis/master/core/QgsTask.html#qgis.core.QgsTask.addSubTask) 函数来描述任务之间的依赖关系。当声明依赖关系时,任务管理器将自动确定如何执行这些依赖关系。只要有可能,依赖项将并行执行,以便尽快满足它们。如果取消了一个任务所依赖的任务,则相关任务也将被取消。循环依赖可能造成死锁,所以要小心。

Expand Down
2 changes: 1 addition & 1 deletion docs/16-开发Python插件.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ category=Raster

!!! 提示

如果qgisMaximumVersion为空,则在上传到[官方Python插件库](#1643)时,它将自动设置为主要版本加上.99(例如:3.99)。
如果qgisMaximumVersion为空,则在上传到[官方Python插件库](#1643-python)时,它将自动设置为主要版本加上.99(例如:3.99)。


metadata.txt示例:
Expand Down
2 changes: 2 additions & 0 deletions docs/19-网络分析库.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ from qgis.PyQt.QtGui import *
vectorLayer = QgsVectorLayer('testdata/network.gpkg|layername=network_lines', 'lines')
builder = QgsGraphBuilder(vectorLayer.sourceCrs())
director = QgsVectorLayerDirector(vectorLayer, -1, '', '', '', QgsVectorLayerDirector.DirectionBoth)
strategy = QgsNetworkDistanceStrategy()
director.addStrategy(strategy)

startPoint = QgsPointXY(1179661.925139,5419188.074362)
endPoint = QgsPointXY(1180942.970617,5420040.097560)
Expand Down
2 changes: 1 addition & 1 deletion docs/2-加载项目.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ QgsPathResolver.setPathWriter(my_processor)
```python
readflags = Qgis.ProjectReadFlags()
readflags |= Qgis.ProjectReadFlag.DontResolveLayers
project = QgsProject()
project = QgsProject().instance()
project.read('C:/Users/ClintBarton/Documents/Projects/mysweetproject.qgs', readflags)
```

Expand Down
4 changes: 2 additions & 2 deletions docs/21-PyQGIS速查表.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ layer = QgsProject.instance().mapLayersByName("layer name you like")[0]
# 5秒
layer.setAutoRefreshInterval(5000)
# 自动刷新
layer.setAutoRefreshEnabled(True)
layer.setAutoRefreshMode(Qgis.AutoRefreshMode.ReloadData)
```
**添加表单要素**

Expand Down Expand Up @@ -264,7 +264,7 @@ from qgis.core import QgsVectorLayer, QgsFeature, QgsGeometry, QgsProject
layer = QgsVectorLayer('Polygon?crs=epsg:4326', 'Mississippi', 'memory')
pr = layer.dataProvider()
poly = QgsFeature()
geom = QgsGeometry.fromWkt("POLYGON ((-88.82 34.99,-88.0934.89,-88.39 30.34,-89.57 30.18,-89.73 31,-91.63 30.99,-90.8732.37,-91.23 33.44,-90.93 34.23,-90.30 34.99,-88.82 34.99))")
geom = QgsGeometry.fromWkt("POLYGON ((-88.82 34.99,-88.09 34.89,-88.39 30.34,-89.57 30.18,-89.73 31,-91.63 30.99,-90.87 32.37,-91.23 33.44,-90.93 34.23,-90.30 34.99,-88.82 34.99))")
poly.setGeometry(geom)
pr.addFeatures([poly])
layer.updateExtents()
Expand Down
2 changes: 1 addition & 1 deletion docs/4-访问图层目录树.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ print(checked_layers)
QgsProject.instance().addMapLayer(layer1)
```

你可以轻松地在[QgsVectorLayer](QgsVectorLayer)[QgsLayerTreeLayer](https://qgis.org/pyqgis/master/core/QgsLayerTreeLayer.html#qgis.core.QgsLayerTreeLayer)之间切换:
你可以轻松地在[QgsVectorLayer](https://qgis.org/pyqgis/master/core/QgsVectorLayer.html#qgis.core.QgsVectorLayer)[QgsLayerTreeLayer](https://qgis.org/pyqgis/master/core/QgsLayerTreeLayer.html#qgis.core.QgsLayerTreeLayer)之间切换:

```python
node_layer = root.findLayer(country_layer.id())
Expand Down
52 changes: 18 additions & 34 deletions docs/6-使用矢量图层.md
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ if caps & QgsVectorDataProvider.ChangeGeometries:
这里有一些示例演示,如何使用这些编辑方法。

```python
from qgis.PyQt.QtCore import QVariant
from qgis.PyQt.QtCore import QMetaType

feat1 = feat2 = QgsFeature(layer.fields())
fid = 99
Expand All @@ -383,7 +383,7 @@ value ='My new name'
layer.changeAttributeValue(fid, fieldIndex, value)

# 添加新的字段
layer.addAttribute(QgsField("mytext", QVariant.String))
layer.addAttribute(QgsField("mytext", QMetaType.Type.QString))
# 删除字段
layer.deleteAttribute(fieldIndex)
```
Expand Down Expand Up @@ -424,12 +424,12 @@ with edit(layer):
添加字段(属性),你需要指定字段定义列表。删除字段,只需提供字段索引列表。

```python
from qgis.PyQt.QtCore import QVariant
from qgis.PyQt.QtCore import QMetaType

if caps & QgsVectorDataProvider.AddAttributes:
res = layer.dataProvider().addAttributes(
[QgsField("mytext", QVariant.String),
QgsField("myint", QVariant.Int)])
[QgsField("mytext", QMetaType.Type.QString),
QgsField("myint", QMetaType.Type.Int)])

if caps & QgsVectorDataProvider.DeleteAttributes:
res = layer.dataProvider().deleteAttributes([0])
Expand All @@ -438,7 +438,9 @@ if caps & QgsVectorDataProvider.DeleteAttributes:
```python
# 删除字段的备用方法
# 首先创建要删除的临时字段 (f1-3)
layer.dataProvider().addAttributes([QgsField("f1",QVariant.Int),QgsField("f2",QVariant.Int),QgsField("f3",QVariant.Int)])
layer.dataProvider().addAttributes([QgsField("f1", QMetaType.Type.Int),
QgsField("f2", QMetaType.Type.Int),
QgsField("f3", QMetaType.Type.Int)])
layer.updateFields()
count=layer.fields().count() # 图层字段个数
ind_list=list((count-3, count-2)) # 创建列表
Expand Down Expand Up @@ -616,7 +618,7 @@ class ESRIValueConverter(QgsVectorFileWriter.FieldValueConverter):
def fieldDefinition(self, field):
idx = self.layer.fields().indexFromName(field.name())
if idx == self.list_field_idx:
return QgsField(LIST_FIELD_NAME, QVariant.String)
return QgsField(LIST_FIELD_NAME, QMetaType.Type.QString)
else:
return self.layer.fields()[idx]

Expand All @@ -634,12 +636,12 @@ opts.fieldValueConverter = converter
### 6.7.2 直接从要素创建

```python
from qgis.PyQt.QtCore import QVariant
from qgis.PyQt.QtCore import QMetaType

# 为要素属性定义字段。需要QgsFields对象
fields = QgsFields()
fields.append(QgsField("first", QVariant.Int))
fields.append(QgsField("second", QVariant.String))
fields.append(QgsField("first", QMetaType.Type.Int))
fields.append(QgsField("second", QMetaType.Type.QString))

"""
创建一个矢量文件编写器的实例,它将创建矢量文件
Expand Down Expand Up @@ -716,7 +718,7 @@ URI还可以指定坐标参考系统、字段和索引。语法是:
以下示例代码说明了如何创建和填充内存提供者

```python
from qgis.PyQt.QtCore import QVariant
from qgis.PyQt.QtCore import QMetaType


# 创建图层
Expand All @@ -725,15 +727,15 @@ pr = vl.dataProvider()


# 添加字段
pr.addAttributes([QgsField("name", QVariant.String),
QgsField("age", QVariant.Int),
QgsField("size", QVariant.Double)])
pr.addAttributes([QgsField("name", QMetaType.Type.QString),
QgsField("age", QMetaType.Type.Int),
QgsField("size", QMetaType.Type.Double)])
vl.updateFields() # 告诉矢量图层从提供者获取更改

# 添加一个要素
fet = QgsFeature()
fet.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(10,10)))
fet.setAttributes(["Johny", 2, 0.3])
fet.setAttributes(["Johnny", 2, 0.3])
pr.addFeatures([fet])

# 在添加新要素时更新图层的范围,因为提供者中的范围更改不会传播到图层
Expand Down Expand Up @@ -847,25 +849,7 @@ layer.triggerRepaint()
```python
print(layer.renderer().symbol().symbolLayers()[0].properties())
# Prints
{'angle': '0',
'cap_style': 'square',
'color': '0,128,0,255',
'horizontal_anchor_point': '1',
'joinstyle': 'bevel',
'name': 'circle',
'offset': '0,0',
'offset_map_unit_scale': '0,0',
'offset_unit': 'MM',
'outline_color': '0,0,0,255',
'outline_style': 'solid',
'outline_width': '0',
'outline_width_map_unit_scale': '0,0',
'outline_width_unit': 'MM',
'scale_method': 'area',
'size': '2',
'size_map_unit_scale': '0,0',
'size_unit': 'MM',
'vertical_anchor_point': '1'}
{'angle': '0', 'cap_style': 'square', 'color': '255,0,0,255,rgb:1,0,0,1', 'horizontal_anchor_point': '1', 'joinstyle': 'bevel', 'name': 'square', 'offset': '0,0', 'offset_map_unit_scale': '3x:0,0,0,0,0,0', 'offset_unit': 'MM', 'outline_color': '35,35,35,255,rgb:0.13725490196078433,0.13725490196078433,0.13725490196078433,1', 'outline_style': 'solid', 'outline_width': '0', 'outline_width_map_unit_scale': '3x:0,0,0,0,0,0', 'outline_width_unit': 'MM', 'scale_method': 'diameter', 'size': '2', 'size_map_unit_scale': '3x:0,0,0,0,0,0', 'size_unit': 'MM', 'vertical_anchor_point': '1'}
```

如果要更改某些属性,这可能很有用:
Expand Down
2 changes: 1 addition & 1 deletion docs/7-几何处理.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ for f in features:
# Area (km2): 389267.8213816008
```

或者,你可能想知道两点之间的距离和方位
或者,你可能想知道两点之间的距离

```python
d = QgsDistanceArea()
Expand Down

0 comments on commit dd3fa7a

Please sign in to comment.