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

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s