[Qgis-user] set Z component of PointZ from field

Andreas Plesch andreasplesch at netscape.net
Tue Sep 3 10:06:33 PDT 2019


Hello, I would like to modify the z coordinate of a PointZ layer using a field.

In qgis 2.18 I could do that with a field function, and using .setZ on
the feature as a side effect of a field calculator invocation.

The field function looked like this:

@qgsfunction(args='auto', group='Custom')
def setZtoValue(newZ, feature, parent):
 """ This function sets the Z coordinate to the provided value and
returns the new point. """
 point = feature.geometry().geometry()
 point.setZ(newZ)
 return feature.geometry()

However, in qgis 3.4 I cannot figure out the equivalent field function
since the API changed. Here is what I have:

def setZtoValue(newZ, feature, parent):
  """ This function sets the Z coordinate to the provided value and
returns the new point. """
  verts = feature.geometry().constGet().vertices()
  for v in verts:
    v.setZ(newZ)
  return feature.geometry()

[never mind the formatting]

This does something but then at the end I get this error:

An error occurred while evaluating the calculation string: 'NoneType'
object has no attribute 'vertices'

and the z component is not updated.

There are other ways to load in 3d points as 3d geometry but the flow
of loading the map points with an elevation/depth field, and then just
populating the z coordinate with the field value may  suggest itself
first.

I know one can construct wkt to import, or perhaps generate new
geometry in a script and replace the existing geometry, but just
updating the the existing z would seem most straightforward. Is it
possible ?

Any feedback much appreciated, -Andreas






--
Andreas Plesch
Waltham, MA 02453


More information about the Qgis-user mailing list