0

I am adding a number of spatial csv files as layers and want to assign a qml layer style.

import processing


# User Dialog to pick Import_QGIS folder
Folder_Path = QFileDialog.getExistingDirectory(caption="Pick the folder 'Import_QGIS'.")

# Folder_Path for uri
Folder_Path_uri = "file:///"+ Folder_Path + "/"

# Make a tuple with the filenames
import os
i=0
my_tuple=[]
for file in os.listdir(Folder_Path):
    First3Letters = file[0:2]
    try:
        int(First3Letters) < 300
        print(file)
        my_tuple.append(file)
        i+=1
    except:
        continue
iNum = int(i)
print(iNum)

for x in range(iNum):
    try:
        uri = Folder_Path_uri + my_tuple[x] + "?type=csv&delimiter=,&detectTypes=yes&xField=X_Wert_Pos&yField=Y_Wert_Pos&crs=epsg:25832&spatialIndex=no&subsetIndex=no&watchFile=no"
        print(uri)
        mem_layer = QgsVectorLayer(uri, my_tuple[x], 'delimitedtext')
        QgsProject.instance().addMapLayer(mem_layer)
        mem_layer.loadNamedStyle("C:\Layer_styles\style3.qml")
        x+=1
    except:
        break

All layers in the specific folder are added as new layers, but only with the default style. What am I missing?

3
  • 1
    maybe is the path, try using r"C:\Layer_styles\style3.qml" and after mem_layer.triggerRepaint()
    – Fran Raga
    Commented Jul 17, 2019 at 10:40
  • Thank you Fran! That solved my problem beautifully!
    – GISme
    Commented Jul 17, 2019 at 11:36
  • I put my comment as an answer.
    – Fran Raga
    Commented Jul 22, 2019 at 18:31

1 Answer 1

1

You need use this path format r"C:\Layer_styles\style3.qml" and after repaint layer,using this mem_layer.triggerRepaint()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.