2

I am trying to extract co-ordinate and height data from a contour layer imported from a .shp file. I have loaded the file into QGIS and am attempting to access with Python the height values of each feature - Closest vertex Z or Interpolated Z in the picture below.

enter image description here

I am eventually aiming for something similar to what is achieved in this answer https://gis.stackexchange.com/a/41922/172876, but I can't find a way to adapt their solution to work for me. I have looked through several forum posts, but many rely on using rasters to extract z values, or are using points not contours.

I have been able to select features in the contour layer, and access their attributes using:

layer = iface.activeLayer()
for feature in layer.selectedFeatures():
    attrs = feature.attributes()
    print(attrs)

But this only accesses the attributes pictured below, and none of the attributes under the (Derived) dropdown in the picture above, which is where the Z values are.

enter image description here

How do I access and extract these using PyQGIS?

sdunnim
  • 123
  • 3
  • 1
    I would try Extract vertices tool to create a point layer, then TIN interpolation. Then you have your raster – BERA Nov 18 '20 at 12:41
  • @BERA Thanks for the suggestion, I will try that – sdunnim Nov 18 '20 at 13:11
  • @BERA I have been able to extract the vertices into a layer; I'm now trying to extract the x,y,z coordinates of each point (this is easier for me than going through the raster method), but can't get it to work. I have followed a couple of the answers from the following link, but nothing is getting printed to my console: https://gis.stackexchange.com/questions/53594/extract-coordinates-from-vector-layer-in-pyqgis – sdunnim Nov 18 '20 at 14:40
  • Are you sure your lines have z values? Or is it an attribute/ a column? – BERA Nov 18 '20 at 14:43
  • @BERA under the (Derived) dropdown there are two Z values (Closest vertex and Interpolated) for the contours (visible in the first picture), and now they are extracted to vertices each vertex has a Z value under the same dropdown. I assumed this was just a stored z value, but it may be just an attribute called 'Z' I guess... What is the difference? And how would I extract it for each vertex via PyQGIS? – sdunnim Nov 18 '20 at 14:51

1 Answers1

1

I had the same problem. I had a bunch of linear features that stored z values, with each single line having the same z value.

I was able to create a new field using the field calculator, as per the following expression.

z(line_interpolate_point($geometry, 1))

This basically generations a point a specific distance along the line (which is $geometry, the distance, I just picked 1). This will return a geometry.

z($geometry) will return the z value of that geometry - In this instance it will be the point geometry.

nr_aus
  • 3,535
  • 6
  • 26