How to add multiple Vector Layers and group them in QGIS with PyQGIS - Tutorial


QGIS is a great software for the processing/analysis of spatial data, Python is a clear, powerful programming lenguaje; together they can enhance the spatial analysis to solve more complex or more dedicated problems in less time. PyQGIS is the Python environment inside QGIS with a set of QGIS libraries plus the Python tools with the potential of running other powerful libraries as Pandas, Numpy or Scikit-learn. 

This tutorial shows the whole procedure to upload multiple files from a certain pattern and group them on the Layer Panel using PyQGIS commands.





This is the final PyQGIS code for the tutorial:

import os

route = "C:\Users\Saul\Documents\Ih_HurricaneTrackingQGIS\Shps"

root = QgsProject.instance().layerTreeRoot()
shapeGroup = root.addGroup("shapePointGroup")

wholelist = os.listdir(os.getcwd())

for file in wholelist:
    if "pts.shp" in file:
        if "xml" not in file:
            filename = QgsVectorLayer(fileroute,file[:-4],"ogr")


Input Files

You can download the files for this tutorial here.

Saul Montoya

Saul Montoya es Ingeniero Civil graduado de la Pontificia Universidad Católica del Perú en Lima con estudios de postgrado en Manejo e Ingeniería de Recursos Hídricos (Programa WAREM) de la Universidad de Stuttgart con mención en Ingeniería de Aguas Subterráneas y Hidroinformática.

Smiley face

Subscribe to our free e-newsletter for tutorials, articles, webminars, courses and more.