2

I have tried both GRASS v.drape and ThToolBox 2D->3D to attach raster z values to line vertices. I can't tell whether this worked or not since I don't seem to be able to extract the values using python. Perhaps I'm not doing it correctly in python.

layers = QgsProject.instance().mapLayersByName('Lines with Z')
layer = layers[0]

for f in layer.selectedFeatures():
    geom = f.geometry().asPolyline()   
    first_vertex = QgsPoint(geom[0])   
    print(first_vertex.x())
    print(first_vertex.z())

Returns:

551628.914954415

nan

Either z values are not being assigned or I'm not retrieving them correctly. What I am trying to do, which I have not found a tool for, is generate a 2D profile of lines with z on the x-axis and distance on the y-axis with each line starting at 0 and also extract min and max z values for each line along the way.

2

I think the problem is how you are accessing your feature geometry. Try using the constGet() method to access the feature geometry like this:

geom = f.geometry().constGet()
first_vertex = QgsPoint(geom[0])
print(first_vertex.z())

This should return a valid z value. By the way, I would recommend using the QGIS algorithm 'Drape (set z-value from raster)' under Vector geometry in the processing toolbox to set your z values.

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.