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:
fromWkbcan also be used without issues, you only have to be aware thatfromWkbis not a static member function likefromWkt. Therefore usegeom = QgsGeometry(); geom.fromWkb(qgs_point.asWkb())instead. – CodeBard Jan 05 '24 at 19:43