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()

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?

  • 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 – S. Dunnim 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: gis.stackexchange.com/questions/53594/… – S. Dunnim 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? – S. Dunnim Nov 18 '20 at 14:51

Your Answer

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

Browse other questions tagged or ask your own question.