31

I have looked at a few tutorials on Python scripts for QGIS. All of them create new vector and raster layers with some existing data source e.g. shapefile or GeoTIFF or PostGIS database table.

Is it possible to create a QGIS layer through a python script, where I can create/add/modify new features in the Python script, as the need arises?

It will be typically a vector layer with point data and custom symbols.

Will this be possible? Is there an example I can look at?

3 Answers 3

25

For QGIS 3

See a slightly modified answer from @MortenSickel below.


For QGIS 2

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()
3
  • Thank you for the reply, it was certainly helpful. However when using the the script i get the following error. "NameError: name 'QVariant' is not defined" I left the "addAttributes" part out and managed to put a dot on the map, so it works, thank you. I am using Ubuntu 12.04 and QGIS 1.8.0-Lisboa But what can cause the above mentioned error? Commented Jul 26, 2012 at 7:54
  • 1
    Seems that setAttributeMap is no longer a method of QgsFeature classes, so this solution doesn't work for the most recent (its 2016) QGIS.
    – Spacedman
    Commented Aug 8, 2016 at 7:25
  • 1
    Just to mention a small mistake in the code provided. Either use a editing session or use the data provider. No need to use both at the same time, you can use data provider without editing session, or the reverse, you can add fields to the QgsVectorlayer directly in a editing session.
    – etrimaille
    Commented Feb 2, 2020 at 19:57
7

In QGIS 3 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)
5

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 at 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())
   
1
  • 1
    But this is creating the empty .shpfile.If i want to save the existing shapefile to specified path how can i do that?
    – user99
    Commented Jan 12, 2015 at 10:17

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.