How do we add a QgsGeometry
in memory object using QgsProject.addMapLayer()
or other PyQGIS code?
ERROR
TypeError: QgsProject.addMapLayer(): argument 1 has unexpected type 'QgsGeometry' buffer <QgsGeometry: Polygon ((336097.91999999998370185 8173802.9299999997019767
CODE Snippet
line_layer = QgsProject().instance().mapLayersByName('b5_Dissolved_Roads_clip')[0]
chainage_layer = QgsProject().instance().mapLayersByName('a3_Roads_10m_Chainage')[0] #Change name to match your chainage layer
fids = []
feats = [f for f in line_layer.getFeatures()]
start_end=-1 #0 for start, -1 for end
field_to_update='End_Chainage'
print(datetime.datetime.now())
print (feats)
for feat in feats:
name = feat['Location']
print (name)
c_sel = [p for p in chainage_layer.getFeatures() if p['Location'] == name]
print (c_sel)
geom = feat.geometry().asPolyline()
end_point = QgsGeometry.fromPointXY(geom[start_end])
buffer = end_point.buffer(20, 4) #Xm buffer with 4 segments
QgsProject.instance().addMapLayer(buffer)
Running QGIS 3.26.3-Buenos Aires
Solution 2: Using Data Provider
is easy to follow.