0

I have about 200 .xyz files I want to import into my QGIS project. I've tried using this answer https://gis.stackexchange.com/a/310415 but haven't been able to make it work. I'm getting this exec(Path('C:/Users/AppData/Local/Temp/tmp8k1lnjji.py').read_text()) as an output and no error messages so it seems like QGIS is reading my files but not doing anything with them? To be honest I have no idea what I'm doing with the Python console. What am I doing wrong?

This is the adjusted code I'm using:

import os.path, glob
from qgis.core import QgsProject, QgsVectorLayer
layers=[]
for file in glob.glob('D:/Documents/Uni/Master/Masterarbeit/Daten/2014/100*.xyz'): # Change this base path
  uri = "file:///" + file + "?delimiter=%s&xField=%s&yField=%s&useHeader=no&crs=epsg:31467" % (" ", "field_1","field_2")
  vlayer = QgsVectorLayer(uri, os.path.basename(file), "delimitedtext")
  vlayer.setFieldAlias(0,'X')
  vlayer.setFieldAlias(1,'Y')
  vlayer.setFieldAlias(2,'Z')
  layers.append(vlayer)

QgsProject.instance().addMapLayers(layers)

I'm on Windows 10 with QGIS version 3.28.5.

This is what the data looks like:

3465888.000 5910084.000 -10.431
3465888.000 5910086.000 -10.399
3465888.000 5910088.000 -10.421
3465888.000 5910090.000 -10.473
3465888.000 5910092.000 -10.536
1
  • There's no header row?
    – Bera
    Commented Apr 24, 2023 at 17:27

1 Answer 1

1

If you are open to a workaround:

List all files. Read them using csv module, create a temp layer for each xyz file and add new features.

import csv, os
xyzfolder = r"/home/bera/Desktop/GIStest/"

#List all xyz files in xyzfolder
xyzlist = [os.path.join(xyzfolder, i) for i in os.listdir(xyzfolder) if i.endswith(".xyz")]

templayers = []

#For each xyz file
for xyz in xyzlist:
    #Create a temp point z layer with the fields x, y and z
    vl = QgsVectorLayer("PointZ?crs=EPSG:31467&field=X:double&field=Y:double&field=Z:double", os.path.basename(xyz), "memory")
    dp = vl.dataProvider()
    with open(xyz, "r") as f:
        reader = csv.reader(f, delimiter=" ")
        for line in reader: #For each line
            x, y, z = [float(i) for i in line] #line is a list of string, convert them to floats
            f = QgsFeature() #Create a feature
            f.setAttributes([x, y, z]) #Set the three attributes
            f.setGeometry(QgsGeometry(QgsPoint(x,y,z))) #Set the geometry by x, y and z coordinates
            dp.addFeature(f) #Add the new feature to the layer
    templayers.append(vl) #Append the layer to the list

#QgsProject.instance().addMapLayers(templayers)
processing.runAndLoadResults("native:mergevectorlayers", {'LAYERS':templayers,'CRS':None,'OUTPUT':'TEMPORARY_OUTPUT'})

You are measuring river depths? enter image description here

2
  • 1
    I think there is a typo #List all xyz files in xyzfolder xyzlist = [os.path.join(csvfolder, i) for i in os.listdir(xyzfolder) if i.endswith(".xyz")] it i changed it to os.path.join(xyzfolder, i) It feels like QGIS is doing the right stuff now, but keeps crashing before it's finished, so I will have to try again on a different computer later. And yes, it's river depths of the Weser
    – Yivi
    Commented Apr 26, 2023 at 9:28
  • Yes you are correct. Maybe there are more layers than QGIS can handle. You could try it on a folder with less files in it. Or instead of adding all temp layers to the map, you can merge them. I've changed the two last lines of code to do that instead.
    – Bera
    Commented Apr 26, 2023 at 10:03

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.