2

The following PyQGIS doesn't seem to work to load my label style (from a QML style file) for a vector layer created from WKT LINESTRINGs. The vector layer does display but not the line label styles.

After running the script, I would expect the labels to be displayed in QGIS but they are not. Maybe I am wrong but I would expect the layer labels to be set to "Single Labels" Here is the script for QGIS 3.28.1:

uriLines = 'file:///Users/Current%20Work/test%20lines.txt?type=csv&delimiter=;%7C&quote=&escape=&maxFields=10000&detectTypes=yes&wktField=Geom&crs=EPSG:4326&spatialIndex=no&subsetIndex=no&watchFile=no'
vLinesLayer = QgsVectorLayer(uriLines, 'Lines', 'delimitedtext')
if not vLinesLayer.isValid():
 print("Lines layer not loaded")
vLinesLayer.loadNamedStyle('/Users/Current\ Work/test\ style.qml')
QgsProject.instance().addMapLayer(vLinesLayer)
vLinesLayer.triggerRepaint()
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Dec 4, 2022 at 22:37

1 Answer 1

2

You do not need to escape the space character with a backslash in the quoted string defining your filename.

Use this instead:

uriLines = 'file:///Users/Current%20Work/test%20lines.txt?type=csv&delimiter=;%7C&quote=&escape=&maxFields=10000&detectTypes=yes&wktField=Geom&crs=EPSG:4326&spatialIndex=no&subsetIndex=no&watchFile=no'
vLinesLayer = QgsVectorLayer(uriLines, 'Lines', 'delimitedtext')
if not vLinesLayer.isValid():
 print ("Lines layer not loaded")
vLinesLayer.loadNamedStyle(r'/Users/Current Work/test style.qml')
QgsProject.instance().addMapLayer(vLinesLayer)
vLinesLayer.triggerRepaint()

But prefer using file names without any special characters, such a spaces, in their full path.

Also make sure your .qml file is correctly formatted.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
answered Dec 4, 2022 at 22:58
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.