3

I want to convert a QgsPoint() to a QgsGeometry(), respectively a QgsGeometry() to a QgsPoint().

Note that I am talking about QgsPoint(), not QgsPointXY()!

QgsGeometry() does offer a .asPoint() method, but this method does not return a QgsPoint(), but a QgsPointXY(), which is quite confusing by the way. The .fromPoint() method was added in v3.34, for older versions there only is a .fromPointXY() method.

How can I do the conversion in QGIS 3.28?

Link to the docs:

2 Answers 2

3

TLTR;

from qgis.core import QgsPoint, QgsGeometry

# QgsPoint -> QgsGeometry
point_as_geom = QgsGeometry(QgsPoint(10, 20, 5, 1))
# QgsGeometry -> QgsPoint
geom_as_point = point_as_geom.constGet()

After reading this topic: Difference between QgsPoint, QgsPointXY and QgsGeometry.fromPointXY() in PyQGIS, one could think of the following approach:

from qgis.core import QgsPoint, QgsPointXY, QgsGeometry

point = QgsPoint(10, 20, 5, 1) # <QgsPoint: PointZM (10 20 5 1)>
point_as_geom = QgsGeometry.fromPointXY(QgsPointXY(point)) # <QgsGeometry: Point (10 20)>

But, it leads to missing some vital Z and M coordinates!

However, there is another solution, that was provided in this thread: Z value is lost when converting QgsGeometry to PointZ with PyQGIS. Simply pass your PointZM to the QgsGeometry() constructor.

from qgis.core import QgsPoint, QgsGeometry

point = QgsPoint(10, 20, 5, 1) # <QgsPoint: PointZM (10 20 5 1)>
point_as_geom = QgsGeometry(point) # <QgsGeometry: PointZM (10 20 5 1)>

And if one want to return, apply either get() or constGet() method that results in point (achieved with the power of QgsAbstractGeometry class):

from qgis.core import QgsPoint, QgsGeometry

point = QgsPoint(10, 20, 5, 1) # <QgsPoint: PointZM (10 20 5 1)>
point_as_geom = QgsGeometry(point) # <QgsGeometry: PointZM (10 20 5 1)>
geom_as_point = point_as_geom.constGet() # <QgsPoint: PointZM (10 20 5 1)>

In all cases, the WKB-type will be the PointZM:

from qgis.core import QgsPoint, QgsGeometry, QgsWkbTypes

point = QgsPoint(10, 20, 5, 1)
print(QgsWkbTypes.displayString(point.wkbType()))
point_as_geom = QgsGeometry(point)
print(QgsWkbTypes.displayString(point_as_geom.wkbType()))
geom_as_point = point_as_geom.constGet()
print(QgsWkbTypes.displayString(geom_as_point.wkbType()))

References:

1
  • 1
    It might be worth putting the two key expressions: QgsPoint to QgsGeometry: geom = QgsGeometry(QgsPoint(10,20,5,1)) and QgsGeometry to QgsPoint: geom.constGet() at the top of the answer. The discussion is useful, but I almost wrote my own answer because I didn't see the key steps Commented Feb 28 at 21:06
2

Indeed there is something missing and confusing, but luckily you can do both using other methods and workarounds.

To convert a QgsPoint() to a QgsGeometry() you can use WKT as an intermediate step. First convert the point to wkt by using .asWkt(), then to a geometry by using .fromWkt():

qgs_point = QgsPoint(10,20,5,1) # create or get example point
print(qgs_point) # <QgsPoint: PointZM (10 20 5 1)>
qgs_point_as_qgs_geometry = QgsGeometry.fromWkt(qgs_point.asWkt())
print(qgs_point_as_qgs_geometry) # <QgsGeometry: PointZM (10 20 5 1)>

To convert a QgsGeometry() to a QgsPoint() you can use the vertices iterator .vertices().next(), as this returns an QgsPoint():

qgs_geometry = QgsGeometry.fromWkt('PointZM (50 30 2 8)') # create or get example geometry
print(qgs_geometry) # <QgsGeometry: PointZM (50 30 2 8)>
qgs_geometry_as_qgs_point = qgs_geometry.vertices().next()
print(qgs_geometry_as_qgs_point) # <QgsPoint: PointZM (50 30 2 8)>
1
  • 1
    concerning your final note: fromWkb can also be used without issues, you only have to be aware that fromWkb is not a static member function like fromWkt. Therefore use geom = QgsGeometry(); geom.fromWkb(qgs_point.asWkb()) instead.
    – CodeBard
    Commented Jan 5 at 19:43

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.