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.
Tutorial
Code
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") os.chdir(route) wholelist = os.listdir(os.getcwd()) for file in wholelist: if "pts.shp" in file: if "xml" not in file: #print(file) fileroute=route+'\\'+file print(fileroute) filename = QgsVectorLayer(fileroute,file[:-4],"ogr") QgsMapLayerRegistry.instance().addMapLayer(filename,False) shapeGroup.insertChildNode(1,QgsLayerTreeLayer(filename))
Input Files
You can download the files for this tutorial here.