[GIS] How to access shapefile m-values with pyqgis

linear-referencingpyqgispythonqgis

How can I access (read/write) m values of a line Shapefile using pyqgis?

I know how to do it with arcpy cursors (http://resources.arcgis.com/en/help/main/10.1/index.html#/Reading_geometries/002z0000001t000000/ ) but I don't know how to do it with pyqgis.

Is it possible with the QGIS python library?

Best Answer

Assuming you have a QgsGeometry object (eg the geometry returned when calling QgsFeature.geometry()), you can access it's raw geometry info by calling QgsGeometry.geometry() in QGIS 2.x or QgsGeometry.constGet() in QGIS 3 respectively. This returns the relevant QgsAbstractGeometry subclass for the geometry type. These subclasses have methods for reading points from the geometry as QgsPointV2 objects (QgsPoint in QGIS 3), which you can then directly retrieve the z or m values from. Sometimes (as is the case with QgsLineStringV2 you can even directly access the z/m values from the subclass itself.

Here's an example for QGIS 2.x:

g = feature.geometry()
line = g.geometry() #line returns a QgsLineStringV2 object
m = line.mAt(0) # m value of first vertex
z = line.zAt(0) # z value of first vertex

And another example for QGIS 3:

g = feature.geometry()
line = g.constGet() #line returns a QgsLineString object
m = line.mAt(0) # m value of first vertex
z = line.zAt(0) # z value of first vertex

An important thing to note is that only very recent gdal versions support m values, and older versions read in m values as z values. This will affect your results if using a file based format (eg shapefiles) in QGIS.