0

The following script has the purpose to upload all .xyz files into QGIS that are within the folder xyz_data. That's the script:

from qgis.core import QgsVectorLayer, QgsProject
import glob, os

dir = "C:/Users/user/Desktop/xyz_data"

os.chdir(dir)

for file in glob.glob("*.xyz"):
    uri = "file://" + dir + file + "?delimiter=%s&xField=%s&yField=%s" % (" ", "field_1", "field_2")
    name = file.replace('.xyz', '')
    lyr = QgsVectorLayer(uri, name, "delimitedtext")
    QgsProject().instance().addMapLayer(lyr)

When I run the command in the python console the names of the files appear in the QGIS layerlist but there is no data and I get an attention sign next to the layer. There is no attribute table I could open nor can I zoom to the layer

enter image description here

Hovering over the attention sign gives me the following error message:

No available Layer! Layer data source was not found.

Apparently it has something to do with the uri. However, I don't see any errors in the code. The directory is correct, I went to the folder and copy & pasted the url.

Perhaps there is something wrong with writing + file +?

Perhaps I don't get the whole filename as a string?

The delimiter is correct as well. The xField and yField names are correct as well I think. The file itself already starts with the x-coordinate and doesn't have a line called xField, however when I upload each file manually, QGIS represents the columns as "field_1" and so on.

Does anyone see the error?

3

You need to append a forward slash in your dir so that it looks like:

dir = "C:/Users/user/Desktop/xyz_data/"

And as you figured out, you need three forward slashes when defining the file url:

"file:///..."
| improve this answer | |

Your Answer

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

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