QGIS开发之矢量图层的使用

2 篇文章 0 订阅
  1. Retrieving information about attributes
  2. Selecting features
  3. Iterating over Vector Layer
    3.1 Accessing attributes
    3.2 Iterating over selected features
    3.3 Iterating over a subset of features
  4. Modifying Vector Layers
    4.1 Add Features
    4.2 Delete Features
    4.3 Modify Features
    4.4 Adding and Removing Fields
  5. Modifying Vector Layers with an Editing Buffer
  6. Using Spatial Index
  7. Writing Vector Layers
  8. Memory Provider
  9. 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
  10. Creating Custom Renderers
  11. Further Topics

  1. 检索关于属性的信息
  2. 选择要素
  3. 遍历矢量图层
  4. 访问属性

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

4 访问属性

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值