2

I would like to know how to add each object obtained as an entity in the same layer; Not by creating a memory layer at each time in this example.

rb = QgsRubberBand(qgis.utils.iface.mapCanvas())
  rb.setColor(Qt.red)

  geom = QgsGeometry.fromPolyline(p)

for pnt in p:
    rb.addPoint(pnt)

v_layer = QgsVectorLayer("LineString", "cable", "memory")
QgsMapLayerRegistry.instance().addMapLayers([v_layer])
pr = v_layer.dataProvider()
v_layer.startEditing()
v_layer.addAttribute(QgsField("id", QVariant.String))
v_layer.addAttribute(QgsField("type", QVariant.String))

seg = QgsFeature()
geom = rb.asGeometry()
seg.setGeometry(geom)
pr.addFeatures( [seg] )
v_layer.commitChanges()
2
  • What are you exactly trying to add to your memory layer (Point, Linestring??)
    – YoLecomte
    Commented Jul 4, 2017 at 9:24
  • I am trying to add a Linestring. each linestrin added must be a feature in my memomry layer
    – hounto
    Commented Jul 4, 2017 at 9:29

1 Answer 1

2

You just have to get your layer before adding your line feature. In your code you create a new layer each time you run your script.

Your can try the following code:

#create your memory layer if it doesn't exists or get it if it exists
v_layer = None
for layer in QgsMapLayerRegistry.instance().mapLayers().values():
   if layer.name() == 'cable':
        v_layer = layer
if v_layer is None: 
    v_layer = QgsVectorLayer("LineString", "cable", "memory")
    v_layer.addAttribute(QgsField("id", QVariant.String))
    v_layer.addAttribute(QgsField("type", QVariant.String))
    QgsMapLayerRegistry.instance().addMapLayers([v_layer])

pr = v_layer.dataProvider()
v_layer.startEditing()


#then add your line
seg = QgsFeature()
seg.setGeometry(geom)
pr.addFeatures( [seg] )

#and save your changes
v_layer.commitChanges()

So if you have a layer named 'cable' load into your project the line will be add in, but i f you don't have such a layer it will be create and add to the project

12
  • Sorry i'm new in python : what do you call "for line in your_lines : ........................." what do you mean by line and your_lines? tks
    – hounto
    Commented Jul 4, 2017 at 9:47
  • how do you create your line? Where do they come from? Please Edit your question to give more information about what you are trying to do, especially give some context, it may help us to find the appropriate solution to your need.
    – YoLecomte
    Commented Jul 4, 2017 at 9:55
  • I generate my line using a function to calculate the shortest path between 2 points.
    – hounto
    Commented Jul 4, 2017 at 10:00
  • if tree[idStop] == -1: print "Path not found" else: p = [] curPos = idStop while curPos != idStart: pnt = graph.vertex(graph.arc(tree[curPos]).inVertex()).point() p.append(pnt) curPos = graph.arc(tree[curPos]).outVertex() p.append(tStart) rb = QgsRubberBand(qgis.utils.iface.mapCanvas()) rb.setColor(Qt.red)
    – hounto
    Commented Jul 4, 2017 at 10:07
  • Ok so you want to add a new line in your layer each time you run your script. The goal is not to add several features in one time. It's more clear now so i will edit my answer.
    – YoLecomte
    Commented Jul 4, 2017 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.