What's the difference between QgsPoint, QgsPointXY and QgsGeometry.fromPointXY(QgsPointXY()) in PyQGIS?
QgsPoint
is a point class which supports Z (3D) and M values. (What are Z and M?)x, y, z, m = 10, 10, 20, 5 QgsPoint(x, y) # 2D # <QgsPoint: Point (10 10)> QgsPoint(x, y, z) # 3D # <QgsPoint: PointZ (10 10 20)> QgsPoint(x, y, z, m) # 3D and M # <QgsPoint: PointZM (10 10 20 5)>
QgsPointXY
is a point class used for just 2D representation of a point. It doesn't support Z and M values.QgsPointXY(x, y) # <QgsPointXY: POINT(10 10)> QgsPointXY(x, y, z) # TypeError: QgsPointXY(): arguments did not match any overloaded call: # ....
QgsPoint
andQgsPointXY
objects can be converted into each other.qgs_point = QgsPoint(x, y, z, m) QgsPointXY(qgs_point) # In this case Z and M values are dropped. # <QgsPointXY: POINT(10 10)> qgs_pointxy = QgsPointXY(x, y) QgsPoint(qgs_pointxy) # <QgsPoint: Point (10 10)>
You should be aware of that a
QgsPoint
or aQgsPointXY
object is not a geometry in terms of PyQGIS.QgsGeometry.fromPointXY()
is a static method which is used for creating a point geometry in 2D and it takes aQgsPointXY
object as argument.QgsGeometry.fromPointXY(QgsPointXY(x, y)) # <QgsGeometry: Point (10 10)>
intersect
,ìntersection
etc. are methods ofQgsGeometry
. So they don't work forQgsPoint
andQgsPointXY
objects.
So, can I pass
x, y
tofromPointXY
as arguments (QgsGeometry.fromPointXY(x, y)
orQgsGeometry.fromPointXY([x, y])
)?
No. You get error.Can I pass a
QgsPoint
object tofromPointXY
?
No. You get error. But this works:QgsGeometry.fromPointXY(QgsPointXY(qgs_point))
Then why are there two seperate classes to create a point?
The geographic vector data model is based on points and most of the users use 2D vector data. Therefore, I guess, the developers must have decided to define separate classes for 2D and 3D points so that point information, which most users do not use, doesn't occupy more space in memory.
That's all I can explain. If I have provided incorrect information please correct it.
QgsPoint is derived directly from QgsAbstractGeometry
QgsPointXY it is not derived from QgsPoint, but there are many methods to switch from one to the other.
QgsPointXY
is derived from QgsReferencedPointXY
which in turn comes from QgsReferencedGeometryBase
You will find many similarities
QgsGeometry.fromPointXY()
allows you to create a geometry from a QgsPointXY. Remember that to add entities to a layer you must pass geometries to the features, this makes the process uniform, because there are many types of entities points, lines, polygons, multipoints....
The geometry
class on the other hand facilitates geoprocesses regardless of the type of entity that created it