Friday, July 6, 2018

Python QGIS Tutorial: Adding Vector Layer

Commonly adding or displaying geospatial data is the first step when starting working with GIS. Mainly there are two types of geospatial data format, vector and raster. Vector is stored in a coordinate or a list of coordinates in point, polyline or polygon geometry. Meanwhile the raster format storing the data in picture element (pixel).

This tutorial will discuss how to display vector data with python in QGIS. It will cover two methods to display vector data using QGIS interface and QGIS project instance. This tutorial is using QGIS 3.0.

Adding Vector Layer with QGIS Interface API

We are starting this tutorial with adding a vector layer in shp format using QGIS interface API. To add a vector layer with QGIS Interface API can be done with addVectorLayer method as in the following expression:
layer=iface.addVectorLayer(path:string,layer_name:string,library:string)

For example to add a street layer with the name public_streets.shp with ogr simple feature library will be:
layer=iface.addVectorLayer("F:\public_streets.shp","street","ogr")

Figure 1 shows the result when the expression was executed. The street data will be added into QGIS layer, and the data displayed in QGIS map canvas.

Adding a vector layer with qgis interface
figure 1. Adding a vector layer with qgis interface






  



Adding Vector Layer with Project Instance

The second method we will use project instance to add vector data. For this we use the following syntax expression:

QgsProject.instance().addMapLayer(layer:QgsVectorLayer) #to add a vector layer
QgsProject.instance().addMapLayers(layer list:QgsVectorLayer]) #to add a list of vector layer

From the syntax above can be seen that we can use addMapLayer method to add a single layer or addMapLayers to add more than one layer in a list.  Moreover we can also observe that the layer to be displayed must be defined as vector layer with QgsVectorLayer class. So firstly we have to define the layer using the class with the following syntax:

layer=QgsVectorLayer(path:string,layer_name:string,library:string)

The listing code below shows the example of adding layer with this method.

#Defining Layer with QgsVectorLayer
street_layer=QgsVectorLayer("F:\public_streets.shp","street","ogr")
park_layer=QgsVectorLayer("F:\park_polygons.shp","park","ogr")
crime_layer=QgsVectorLayer("F:\crime_shp_2016.shp","crime","ogr")
#Add a list of layer 
QgsProject.instance().addMapLayers([street_layer,park_layer])
#Add a single layer 
QgsProject.instance().addMapLayer(crime_layer)

Figure 2 shows the result of the code execution. Can be seen that those three layers have been added to the layer tree and displayed in QGIS map canvas.
Adding vector layer with QGIS Project instance
Figure 2. Adding vector layer with QGIS Project instance
That's all the tutorial how to add vector layer with python in QGIS. Please see other tutorials which discuss other topics in this QGIS python programming tutorial series.

2 comments :

  1. Thank you for the tutorial, really suitable for novices.

    I noticed that using the facet.addVectorLayer() to add a Shape layer, no matter what the layer name of the second parameter is set, the added layer name is the same as the shape name, and QgsProject.instance().addMapLayers() will not have this problem. Is this a bug in iface.addVectorLayer()?

    ReplyDelete
    Replies
    1. Could be. I also have the same issue.

      Delete