I am trying to add elevation to a 2D point in a QGIS 3 plugin, preferably in a concise and straightforward way. Not finding this easy.
I think part of the problem is that in QGIS 3,
fromPoint() has been renamed to
fromPointXY(). I am not very clear on how to create 3D point geometry.
I have tried a number of variations without finding one which works and have not been able to find a suitable example online.
Basically what I want to achieve is
PointXY + elevation -> Point, which I would expect to be something like this:
z = 1.0 # or whatever for l in selectedLayers: # for each selected layer if l.geometryType() == QgsWkbTypes.PointGeometry: # point layer l.startEditing() feat = l.getFeatures() for f in feat: # each feature # Get existing geometry... geom = f.geometry() pt = geom.asPoint() # returns PointXY # Replace with 3D geometry... pt3D = QgsPoint(pt.x(), pt.y(), z) geom = QgsGeometry.fromPointXY(pt3D) # error, because pt is 3D f.setGeometry(geom) l.updateFeature(f) l.commitChanges()
How do I do this?
For the benefit of others this is now resolved using the answer(s) below.
I am now adding the elevation like this:
f.setGeometry( QgsPoint(pt.x(), pt.y(), z) ) # x, y, z
It turns out that my problem was not so much adding the elevation, as knowing when I had successfully added it.