7

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?

Taras
  • 32,823
  • 4
  • 66
  • 137
BERA
  • 72,339
  • 13
  • 72
  • 161

1 Answers1

8

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

Taras
  • 32,823
  • 4
  • 66
  • 137
Kalak
  • 3,848
  • 8
  • 26