How to create a layer in QGIS using Python

Creating a layer using Python is quite simple. In the following steps, we’ll create a vector layer in memory and display it on the map.

First of all, enable the Python Console from the Plugins Menu (it is available in the QGIS main window).

A point is created as follows:

# Specify the geometry type
layer = QgsVectorLayer('Point?crs=epsg:4326', 'point' , 'memory')

# Set the provider to accept the data source
prov = layer.dataProvider()
point = QgsPoint(100,100)

# Add a new feature and assign the geometry
feat = QgsFeature()
feat.setGeometry(QgsGeometry.fromPoint(point))
prov.addFeatures([feat])

# Update extent of the layer
layer.updateExtents()

# Add the layer to the Layers panel
QgsMapLayerRegistry.instance().addMapLayers([layer])

The result is:

point

We have just created a point memory layer, defined by a specific Coordinate Reference System (in this case, ESPG:4326). Without this declaration:

?crs=epsg:4326

QGIS will prompt you to choose one.

The syntax is similar if you want to create a line or a polygon layer. A line is created with two points:

QgsGeometry.fromPolyline([QgsPoint(x1,y1),QgsPoint(x2,y2)]))

and a polygon is created with a list of points:

QgsGeometry.fromPolygon([[QgsPoint(x1,y1),QgsPoint(x2,y2), QgsPoint(x3,y3)]])

With these premises, a line is created as follows:

# Specify the geometry type
layer = QgsVectorLayer('LineString?crs=epsg:4326', 'line' , 'memory')

# Set the provider to accept the data source
prov = layer.dataProvider()
point1 = QgsPoint(100,100)
point2 = QgsPoint(200,200)

# Add a new feature and assign the geometry
feat = QgsFeature()
feat.setGeometry(QgsGeometry.fromPolyline([point1, point2]))
prov.addFeatures([feat])

# Update extent of the layer
layer.updateExtents()

# Add the layer to the Layers panel
QgsMapLayerRegistry.instance().addMapLayers([layer])

The result is:

line

A polygon is created as follows:

# Specify the geometry type
layer = QgsVectorLayer('Polygon?crs=epsg:4326', 'polygon' , 'memory')

# Set the provider to accept the data source
prov = layer.dataProvider()
points = [QgsPoint(100,100), QgsPoint(100,200), QgsPoint(200,200), QgsPoint(200,100)]

# Add a new feature and assign the geometry
feat = QgsFeature()
feat.setGeometry(QgsGeometry.fromPolygon([points]))
prov.addFeatures([feat])

# Update extent of the layer
layer.updateExtents()

# Add the layer to the Layers panel
QgsMapLayerRegistry.instance().addMapLayers([layer])

The result is:

polygon

Remember that a memory layer is created as temporary file: if you dont’ save it, it will be deleted when you close QGIS!

Note If you are working on a Python script, be sure to import the following row before anyone of the codes written above:

from qgis.core import *
Advertisements

5 thoughts on “How to create a layer in QGIS using Python

  1. Hi, I copied the codes to my QGIS Python console,and succeed in creating a new layer at Layers panel. I made the newly added layer visible and then zoom to the layer, But I can’t see line or polygon in the main window. Please tell what could be the problem? Hope for your reply. Thank you.

    Like

  2. Thank you for this really useful quick solution. I’m running QGIS 3.0.2 and had to make a few changes to the code for it to work with the new PyQGIS API, see below:

    layer = QgsVectorLayer(‘Polygon?crs=epsg:7856’, ‘polygon’ , ‘memory’)

    # Set the provider to accept the data source
    points = [QgsPointXY(100,100), QgsPointXY(100,200), QgsPointXY(200,200), QgsPointXY(200,100)]
    prov = layer.dataProvider()

    # Add a new feature and assign the geometry
    feat = QgsFeature()
    feat.setGeometry(QgsGeometry.fromPolygonXY([points]))
    prov.addFeatures([feat])

    # Update extent of the layer
    layer.updateExtents()

    # Add the layer to the Layers panel
    QgsProject.instance().addMapLayers([layer])

    Like

  3. Bonjour c’est super vous avez beaucoup avec la creation de point et de ligne maintenant je voudrais creer plusieurs points et apres les dupliquer en fonction d’un champs hauteur qui va etre dans la couche en gardant les meme cordonnees

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s