0

I what to create many lines with this code:

v_layer = QgsVectorLayer("LineString", "cable", "memory")
pr = v_layer.dataProvider()
seg = QgsFeature()
seg.setGeometry(QgsGeometry.fromPolyline([line_start, geomPoint]))
pr.addFeatures( [ seg ] )
v_layer.updateExtents()
QgsMapLayerRegistry.instance().addMapLayers([v_layer])

Buth this create one layer for each of this code.

How can I create all in one layer.

1 Answer 1

1

you need to add all the geometries before the addMapLayers

v_layer = QgsVectorLayer("LineString", "cable", "memory")
pr = v_layer.dataProvider()
# first element
seg = QgsFeature()
seg.setGeometry(QgsGeometry.fromPolyline([line_start, geomPoint]))
pr.addFeatures( [ seg ] )
v_layer.updateExtents()
# second element
seg = QgsFeature()
seg.setGeometry(QgsGeometry.fromPolyline([geomPoint, newgeomPoint]))
pr.addFeatures( [ seg ] )
v_layer.updateExtents()
# etc
......
QgsMapLayerRegistry.instance().addMapLayers([v_layer])

so use a for loop

1
  • @user1573901 Please upvote useful answers and accept answers that solve your problem (by clicking the tick mark on the left).
    – underdark
    Commented May 21, 2013 at 11:41

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.