如何在QGIS 1.9中以编程方式创建要素并将其添加到存储层?


13

我在QGIS 1.8中有一个工作正常的插件,该插件从MSAccess数据库读取数据并将其添加到一系列内存层中。过渡过程中涉及一些处理,因此我不认为仅使用QGIS直接从数据库读取是一种选择。

我想从QGIS 1.8升级到1.9(主要是因为打印作曲家的素质得到了提高)。该插件不适用于新的API。

我尝试了Google搜索中出现的各种方法。一种,修改以下代码-来自http://www.qgis.org/pyqgis-cookbook/vector.html#memory-provider,即向数据提供者添加几何形状和属性,然后更新层-以适应新的API有点,但是直到我手动进入编辑模式(类似于http://hub.qgis.org/issues/3713),属性才可见。在上述链接的回复#1中详细介绍了一种替代方法,该方法正确添加了图层和属性,但无法向图层添加要素。

鉴于这应该是一个非常简单的任务,所以我希望在座的人可以提供一个如何完成此工作的示例。(附言:我不是专业的程序员,我的大多数编码都还很粗糙-我欢迎任何指导,但请您原谅我的无知)

# Receivers = a list of lists returned from a database query

# create layer
vl = QgsVectorLayer("Point", item, "memory")
pr = vl.dataProvider()

# add fields
pr.addAttributes( [ QgsField("Rec_No", QVariant.Int), QgsField("Include",  QVariant.String), QgsField("Label",  QVariant.String), QgsField("X", QVariant.Double),
                    QgsField("Y", QVariant.Double), QgsField("Z", QVariant.Double), QgsField("Height", QVariant.Double),
                    QgsField("Project_Re", QVariant.String), QgsField("NCA", QVariant.String),
                    QgsField("DayCrit", QVariant.Int), QgsField("EveCrit", QVariant.Int), QgsField("NightCrit", QVariant.Int) ] )

for i in range(len(Receivers)):          
  # add a feature
  fet = QgsFeature()
  X = Receivers[i][3]
  Y = Receivers[i][4]
  fet.setGeometry( QgsGeometry.fromPoint(QgsPoint(X,Y)) )

  # Details = a list of results returned from a database query specific to each result in 'Receivers'

  if Receivers[i][3] != 0:
    Include = 'Yes'
  else:
    Include = 'No'

  fet.setAttributeMap( { 0 : QVariant(Receivers[i][0]), 1 : QVariant(Include), 2 : QVariant(Receivers[i][2]),
                         3 : QVariant(Receivers[i][3]), 4 : QVariant(Receivers[i][4]), 5 : QVariant(Receivers[i][5]), 6 : QVariant(Receivers[i][6]),
                         7 : QVariant(Details[0]), 8 : QVariant(Details[1]), 9 : QVariant(Details[2]), 10 : QVariant(Details[3]), 11 : QVariant(Details[4]) } )
  pr.addFeatures( [ fet ] )

# add a style
vl.loadNamedStyle('C:/OSGeo4W/apps/qgis/python/plugins/Gopher2QGIS/styles/Receiver_Style.qml')

# update layer's extent when new features have been added
# because change of extent in provider is not propagated to the layer
vl.commitChanges()
vl.updateExtents()
vl.updateFieldMap()

QgsMapLayerRegistry.instance().addMapLayer(vl)

看一下PinPoint插件。它将带有属性的功能添加到存储层,并与2.0 API一起使用。
gsherman

很好,像魅力一样。我使用此示例添加了一个来自restfull服务的点的层。QGis很棒
Peter

Answers:


8

感谢gsherman,上面的PinPoint插件示例非常完美。

据我了解,该过程是:

  1. 使用构造字符串中的属性创建图层
  2. 将上述图层添加到地图注册表中
  3. 开始在该层上进行编辑
  4. 添加功能并提交更改

这是我的代码的一部分,现在可以使用。

layer =  QgsVectorLayer(
          "Point?field=Rec_No:integer&field=Include:string(120)&field=Label:string(120)&field=X:double&field=Y:double&field=Z:double&field=Height:double&field=Project_Re:string(120)&field=NCA:string(120)&field=DayCrit:integer&field=EveCrit:integer&field=NightCrit:integer",
          item,
          "memory")
QgsMapLayerRegistry.instance().addMapLayer(layer)

# Receivers = as in the above example 'Receivers' is a list of results
for i in range(len(Receivers)):

  # add a feature
  feature = QgsFeature()

  X = Receivers[i][3]
  Y = Receivers[i][4]
  feature.setGeometry( QgsGeometry.fromPoint(QgsPoint(X,Y)) )

  # Details = as in the above example 'Details' is a list of results

  if Receivers[i][1] != 0:
    Include = 'Yes'
  else:
    Include = 'No'

  values = [ QVariant(Receivers[i][0]), QVariant(Include), QVariant(Receivers[i][2]),
                         QVariant(Receivers[i][3]), QVariant(Receivers[i][4]), QVariant(Receivers[i][5]), QVariant(Receivers[i][6]),
                         QVariant(Details[0]), QVariant(Details[1]), QVariant(Details[2]), QVariant(Details[3]), QVariant(Details[4]) ]

  feature.setAttributes(values)
  layer.startEditing()
  layer.addFeature(feature, True)
  layer.commitChanges()

6

根据Adam Bioletti的回答,对上述过程的进一步测试表明,唯一必要的要求是在进行任何更改(例如创建属性和特征)之前开始编辑存储层,然后提交更改。这可以在将图层添加到地图注册表之前完成。

这是与2.0 API一起使用的Cookbook代码的更新:

# create layer
vl = QgsVectorLayer("Point", "temporary_points", "memory")
pr = vl.dataProvider()

# changes are only possible when editing the layer
vl.startEditing()
# add fields
pr.addAttributes([QgsField("name", QVariant.String),QgsField("age", QVariant.Int),QgsField("size", QVariant.Double)])

# add a feature
fet = QgsFeature()
fet.setGeometry(QgsGeometry.fromPoint(QgsPoint(10,10)))
fet.setAttributes(["Johny", 2, 0.3])
pr.addFeatures([fet])

# commit to stop editing the layer
vl.commitChanges()

# update layer's extent when new features have been added
# because change of extent in provider is not propagated to the layer
vl.updateExtents()

# add layer to the legend
QgsMapLayerRegistry.instance().addMapLayer(vl)
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.