# [QGIS-Developer] Geometry data model logic?

Raymond Nijssen r.nijssen at terglobo.nl
Wed Aug 22 05:30:16 PDT 2018

Hopefully it is just a misconception on my side, but I really don't get
the logic in the qgis geometry data model.

I tried the following script (sorry, some lines are wrapped by my email
client):

---

#points

p1 = QgsGeometry().fromWkt('point(0 0)')
print(p1) # <QgsGeometry: Point (0 0)>
print(p1.asPoint()) # <QgsPointXY: POINT(0 0)>
print(p1.centroid()) # <QgsGeometry: Point (0 0)>
print(p1.buffer(1, 1)) # <QgsGeometry: Polygon ((1 0, 0 -1, -1 0, 0 1, 1
0))>

p2 = QgsPoint(1,2)
print(p2) # <QgsPoint: Point (1 2)>
p2g = QgsGeometry(p2)
print(p2g) # <QgsGeometry: Point (1 2)>
print(p2) # <QgsPoint: Point (1 2)>
print(p2.centroid()) # <QgsPoint: Point (1 2)>
print(QgsPointXY(p2.x(), p2.y())) # <QgsPointXY: POINT(1 2)>
# The next line makes qgis crash after running it 2x !!
#print(QgsGeometry(p2)) # <QgsGeometry: Point (1 2)>

p3 = QgsPointXY(2, 0)
print(p3) # <QgsPointXY: POINT(2 0)>
print(QgsGeometry.fromPointXY(p3)) # <QgsGeometry: Point (2 0)>
print(QgsPoint(p3.x(), p3.y())) # <QgsPoint: Point (2 0)>

# lines

l1 = QgsGeometry().fromWkt('linestring((0 0, 1 1, 1 2))')
print(l1) # <QgsGeometry: LineString (0 0, 1 1, 1 2)>
print(l1.buffer(1, 1).asPolygon())
print(l1.asPolyline()) # [<QgsPointXY: POINT(0 0)>, <QgsPointXY: POINT(1
1)>, <QgsPointXY: POINT(1 0)>]
print(QgsLineString(l1.asPolyline())) # TypeError: index 0 has type
'QgsPointXY' but 'QgsPoint' is expected

---

My questions are:

- How is the relation between QgsGeometry, QgsPoint, QgsPoitXY, etc
meant to be? Is there documentation? Maybe even a schema?

- Having QgsPoint and QgsPointXY etc is not handy, but I understand they
are necessary for efficiency reasons. However, shouldn't they all have
easy typecasting functions? For example:
QgsGeometry.asPoint()
QgsGeometry.asPointXY()
QgsGeometry.asLineString()
QgsGeometry.asLineStringXY()
QgsGeometry.asPolygon()
QgsGeometry.asPolygonXY()
QgsGeometry.asMultiLineString()
QgsGeometry.asMultiLineStringXY()
etc..

QgsPoint.asGeometry()
QgsPoint.asPointXY()

QgsLineString.asGeometry()
QgsLineString.asPointXY()

etc..

- Why don't all geometry types inherit from QgsGeometry, making all the
geometry operators work? For example:
QgsPoint(1,2).buffer(3,5)

Some, IMHO, really odd things are:

- QgsGeometry.asPoint() returns a QgsPointXY

- QgsGeometry.asLineString() does not exist

- QgsGeometry.asPolyLine() returns an array of QgsPointXY's (besides,
polyline is a strange geometry type in this model)

- QgsLineString([qgsPointXY, qgsPointXY, qgsPointXY, ...]) results in an
error, while the documentation states:
QgsLineString(points: Iterable[QgsPointXY]) Construct a linestring
from list of points. This constructor is more efficient then calling

- QgsGeometry(QgsPoint) does work, but destroys my QgsPoint (and makes
qgis instable)

Hopefully somebody can explain, making this more sense to me. And I'd
like to contribute to improving this, though I'm not a cpp programmer.

Regards,
Raymond