8

I want to create a PointZ with a Z value, then construct a geometry, and then go back to a PointZ but the Z value is lost because .asPoint() is returning a PointXY.

p = QgsPoint(1,2,3)
print(p)
#<QgsPoint: PointZ (1 2 3)>

g = QgsGeometry(p)
print(g)
#<QgsGeometry: PointZ (1 2 3)>

p2 = g.asPoint()
print(p2)
#<QgsPointXY: POINT(1 2)> #The Z is lost

How can I create a PointZ from the geometry?

0

1 Answer 1

9

What you want is:

p = QgsPoint(1,2,3)
print(p)
#<QgsPoint: PointZ (1 2 3)>

g = QgsGeometry(p)
print(g)
#<QgsGeometry: PointZ (1 2 3)>

p2 = g.constGet()
print(p2)
#<QgsPoint: PointZ (1 2 3)> 

QgsPoint is the underlying geometry contained inside the QgsGeometry it does not need to be recreated from WKT or anything else, just accessed.

NB: .constGet() gets a non-modifiable point if you want to change the point, you need to use .get().

1
  • 1
    I didn't know this solution, +1 and I think it's the best answer. Commented Jun 14, 2023 at 7:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.