16

I am using PyQGIS. What is the difference between:

  1. QgsGeometry.fromPointXY(QgsPointXY(x, y))
  2. QgsPointXY(x, y)
  3. QgsPoint(x, y)

When I print these three options, I get:

print(QgsGeometry.fromPointXY(QgsPointXY(x1, y1)))
<QgsGeometry: Point (59777 -266560)>
print(QgsPointXY(x1, y1))
<QgsPointXY: POINT(59777 -266560)>
print(QgsPoint(x1, y1))
<QgsPoint: Point (59777 -266560)>

(x1,y1) are specific coordinates that I used as a test.

I don't understand the difference between these objects, and I need to know because there are some methods that work only in one or two of them. For example, the intersect method does not work for QgsPoint (at least it didn't work when I tried to check which feature the point intersected with).

1

2 Answers 2

19
  • 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 and QgsPointXY 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 a QgsPointXY 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 a QgsPointXY object as argument.

    QgsGeometry.fromPointXY(QgsPointXY(x, y))
    # <QgsGeometry: Point (10 10)>
    
  • intersect, ìntersection etc. are methods of QgsGeometry. So they don't work for QgsPoint and QgsPointXY objects.


  • So, can I pass x, y to fromPointXY as arguments (QgsGeometry.fromPointXY(x, y) or QgsGeometry.fromPointXY([x, y]))?
    No. You get error.

  • Can I pass a QgsPoint object to fromPointXY?
    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.

0
4

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

3
  • 1
    This information is wrong. QgsPoint is capable of storing Z and M values. QgsPointXY can only handle 2D information. There is nothing about "old" and "new" to it. QgsPoint is based on QgsAbstractGeometry and can be put into a QgsGeometry. Both have their use cases. There is nothing about "old" and "new" to it. Dec 2, 2020 at 15:08
  • 1
    Sorry for being that direct. Dec 2, 2020 at 15:09
  • 1
    On the contrary Master, thank you for clarifying, we are here to learn and rectify :) My beginnings with PyQGis were watching your Colombia course and Olaya's course, both by youtube
    – Luis Perez
    Dec 3, 2020 at 1:53

Your Answer

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

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