I would like to create a model point dataset in QGIS 3.0.3 with Python in the QGIS Python Console to do some testing. Specifically, I want to create a point dataset that has a feature with an empty geometry, and, another feature with a NULL geometry.
What I have tried is:
layer = QgsVectorLayer('Point?crs=epsg:4326&field=id:string&field=type:string','ModelPoints',"memory")
pr = layer.dataProvider()
points = {}
points['Pt1'] = ['(1,1)', QgsPointXY(1,1)]
points['Pt2'] = ['(2,1)', QgsPointXY(1,1)]
points['Pt3'] = ['(4,1)', QgsPointXY(1,1)]
points['Pt4'] = ['empty', QgsPointXY()]
points['Pt5'] = ['null', None]
for id, point in points.items():
pt = QgsFeature()
pt.setAttributes([id,point[0]])
if point[1] is not None:
pt.setGeometry(QgsGeometry.fromPointXY(point[1]))
print(pt.geometry().asWkt())
pr.addFeature(pt)
layer.updateExtents()
QgsProject.instance().addMapLayer(layer)
In this code, the empty point geometry is intended to be created by:
pt.setGeometry(QgsGeometry.fromPointXY(QgsPointXY())
However, the result is:
pt.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(0,0))
So, my code is making a point with the vertex (0,0), but, whereas I need a point (or a geometry) with no vertices.
The approaches g = QgsGeometry.fromWkt('POINT')
and g = QgsGeometry.fromWkt('POINT EMPTY')
both result in a NULL geometry as shown by using print(g.isNull())
.
POINT EMPTY
.