How to create a new empty vector layer programmatically?
This does not work any longer in qgis3. See a slightly modified answer below.
Have a look at the Memory provider as described in PyQGIS Cookbook.
Memory provider is intended to be used mainly by plugin or 3rd party app developers. It does not store data on disk, allowing developers to use it as a fast backend for some temporary layers.
# To avoid 'QVariant' is not defined error from PyQt4.QtCore import * # create layer vl = QgsVectorLayer("Point", "temporary_points", "memory") pr = vl.dataProvider() # Enter editing mode 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.setAttributeMap( { 0 : QVariant("Johny"), 1 : QVariant(20), 2 : QVariant(0.3) } ) pr.addFeatures( [ fet ] ) # Commit changes vl.commitChanges()
If you want to save a new vector layer (.shp) in your Hard Disk you have to use QgsVectorFileWriter(). It takes several parameters, the simplest way is to use the parameters from the project you are using. Have a look to this site.
cLayer = qgis.utils.iface.mapCanvas().currentLayer()
provider = cLayer.dataProvider()
writer = QgsVectorFileWriter( "output_path_and_name.shp", provider.encoding(), provider.fields(),QGis.WKBPolygon, provider.crs() )
The accepted answer does not work any longer in QGIS3
Now one need to do
from qgis.core import QgsVectorLayer, QgsFeature, QgsField, QgsGeometry, QgsPointXY, QgsField, QgsProject
vl = QgsVectorLayer("Point", "temporary_points", "memory")
pr = vl.dataProvider()
# Enter editing mode
vl.startEditing()
# add fields
pr.addAttributes( [ QgsField("name", QVariant.String),
QgsField("age", QVariant.Int),
QgsField("size", QVariant.Double) ] )
# add a feature
# To just create the layer and add features later, delete the four lines from here until Commit changes
fet = QgsFeature()
fet.setGeometry( QgsGeometry.fromPointXY(QgsPointXY(15,60)) )
fet.setAttributes(["Johny",20,0.3])
pr.addFeatures( [ fet ] )
# Commit changes
vl.commitChanges()
# Show in project
QgsProject.instance().addMapLayer(vl)