2

I'm new in PyQgis. I can create vector layer and lines/polygons on it :

layer = QgsVectorLayer("LineString", "points", "memory")
pr = layer.dataProvider()
pt = QgsFeature()
point1 = QgsPoint(0,0)
point2 = QgsPoint(100,100)
pt.setGeometry(QgsGeometry.fromPolyline([point1,point2]))
pr.addFeatures([pt])
layer.updateExtents()

Is there a simple way, how can I visuall see that line in window?

I tried canvas, but I wasn't able to display anything on it.

2

Add the second line in my code:

layer = QgsVectorLayer("LineString", "points", "memory")
QgsMapLayerRegistry.instance().addMapLayer(layer)
pr = layer.dataProvider()
pt = QgsFeature()
point1 = QgsPoint(0,0)
point2 = QgsPoint(100,100)
pt.setGeometry(QgsGeometry.fromPolyline([point1,point2]))
pr.addFeatures([pt])
layer.updateExtents()

The result is in the next image:

enter image description here

  • Thanks, exactly what I was looking for. I had that line, but after that code I posted. – Aerov Jul 18 '15 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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