- Retrieving information about attributes
- Selecting features
- Iterating over Vector Layer
3.1 Accessing attributes
3.2 Iterating over selected features
3.3 Iterating over a subset of features - Modifying Vector Layers
4.1 Add Features
4.2 Delete Features
4.3 Modify Features
4.4 Adding and Removing Fields - Modifying Vector Layers with an Editing Buffer
- Using Spatial Index
- Writing Vector Layers
- Memory Provider
- Appearance (Symbology) of Vector Layers
9.1 Single Symbol Renderer
9.2 Categorized Symbol Renderer
9.3 Graduated Symbol Renderer
9.4 Working with Symbols
9.4.1 Working with Symbol Layers
9.4.2 Creating Custom Symbol Layer Types - Creating Custom Renderers
- Further Topics
- 檢索關於屬性的信息
- 選擇要素
- 遍歷矢量圖層
- 訪問屬性
1 檢索關於屬性的信息
You can retrieve information about the fields associated with a vector layer by calling pendingFields() on a QgsVectorLayer instance:
可以通過調用QgsVectorLayer實例中的的pendingFields()來檢索與矢量圖層關聯的字段的信息:
# "layer" is a QgsVectorLayer instance
for field in layer.pendingFields():
print field.name(), field.typeName()
NOTE:Starting from QGIS 2.12 there is also a fields() in QgsVectorLayer which is an alias to pendingFields().
注意:從QGIS 2.12開始,QgsVectorLayer中還有一個fields(),它是pendingFields()的別名。
2 選擇要素
In QGIS desktop, features can be selected in different ways, the user can click on a feature, draw a rectangle on the map canvas or use an expression filter. Selected features are normally highlighted in a different color (default is yellow) to draw user’s attention on the selection. Sometimes can be useful to programmatically select features or to change the default color.
在QGIS桌面中,要素可以以不同的方式進行選擇,用戶可以點擊要素,在地圖畫布上繪製一個矩形或使用表達式過濾器。 所選功能通常以不同的顏色突出顯示(默認爲黃色),以吸引用戶對選擇的注意。 有時可以通過編程方式選擇功能或更改默認顏色。
To change the selection color you can use setSelectionColor() method of QgsMapCanvas as shown in the following example:
可以使用QgsMapCanvas中的setSelectionColor() 方法改變選擇集的顏色,代碼如下:
iface.mapCanvas().setSelectionColor( QColor("red") )
To add features to the selected features list for a given layer, you can call setSelectedFeatures() passing to it the list of features IDs:
要爲指定圖層的所選要素列表添加要素,可以調用setSelectedFeatures()將要素的ID列表傳遞給它:
# Get the active layer (must be a vector layer)
layer = iface.activeLayer()
# Get the first feature from the layer
feature = layer.getFeatures().next()
# Add this features to the selected list
layer.setSelectedFeatures([feature.id()])
To clear the selection, just pass an empty list:
若要清空選擇集,只需要將列表設爲空
layer.setSelectedFeatures([])
3 遍歷矢量圖層
Iterating over the features in a vector layer is one of the most common tasks. Below is an example of the simple basic code to perform this task and showing some information about each feature. the layer variable is assumed to have a QgsVectorLayer object
遍歷矢量圖層中的要素是最常見的需求之一。 以下是執行此任務的簡單基本代碼的示例,顯示有關每個要素的一些信息。 該圖層變量假定有一個QgsVectorLayer對象
iter = layer.getFeatures()
for feature in iter:
# retrieve every feature with its geometry and attributes
# fetch geometry
geom = feature.geometry()
print "Feature ID %d: " % feature.id()
# show some information about the feature
if geom.type() == QGis.Point:
x = geom.asPoint()
print "Point: " + str(x)
elif geom.type() == QGis.Line:
x = geom.asPolyline()
print "Line: %d points" % len(x)
elif geom.type() == QGis.Polygon:
x = geom.asPolygon()
numPts = 0
for ring in x:
numPts += len(ring)
print "Polygon: %d rings with %d points" % (len(x), numPts)
else:
print "Unknown"
# fetch attributes
attrs = feature.attributes()
# attrs is a list. It contains all the attribute values of this feature
print attrs