1

I have managed to cobble together two Python snippets, one to import multiple delimited files, and another to apply a style. What I would like to do is combine these into one operation, and if possible assign a group for them (creating it ideally if it doesn't exist)....

The below is what I have so far:

import os.path, glob
from qgis.core import QgsProject, QgsVectorLayer
layers=[]
for file in glob.glob('C:/PathToFiles/*.QGIS'): # Change this base path and change backslashes to forward slashes
  uri = "file:///" + file + "?delimiter=%s&xField=%s&yField=%s&useHeader=no&crs=epsg:27039" % (" ","field_8","field_9") #(Delimeter, XField, YField) #1st column in file is column 1
  vlayer = QgsVectorLayer(uri, os.path.basename(file), "delimitedtext")
  vlayer.setFieldAlias(1,'X') #these order of columns go: 0, 1, 2 etc
  vlayer.setFieldAlias(2,'Y')
  layers.append(vlayer)

QgsProject.instance().addMapLayers(layers)

And:

from qgis.core import *
def applystyle_group(name):
    root = QgsProject.instance().layerTreeRoot()
    point = root.findGroup(name) #Find Group AP
    for child in point.children():
        if isinstance(child, QgsLayerTreeLayer):
            if child.layer().type()==0:
                child.layer().loadNamedStyle('C:/PathToStyle/POINT_CODE.qml')
applystyle_group("SPS Files")

As you can see, the group I am operating in is called 'SPS Files'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.