Index de l'article

Temporary layers

Add a temporary layers

point_vector = QgsVectorLayer("Point", "my_points", "memory")
project.addMapLayer(point_vector)

Add fields

from qgis.PyQt.QtCore import QVariant
...
pr = point_vector.dataProvider()
pr.addAttributes([QgsField("id", QVariant.Int), QgsField("name", QVariant.String)])
point_vector.updateFields()

Add a point

f = QgsFeature()
f.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(7.7633340, 48.5785590)))
f.setAttributes([8, 'My 8'])
pr.addFeature(f)
point_vector.updateExtents()
project.addMapLayer(point_vector)
iface.mapCanvas().refresh()