1

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

7
  • 2
    Have you tried the solutions in Display buffer in a new layer - PyQGIS?
    – ahmadhanb
    Commented Nov 21, 2022 at 0:07
  • It already is a vector layer right? so isn't it the same as buff_layer in that link?
    – GeorgeC
    Commented Nov 21, 2022 at 0:14
  • 3
    The original data is a vector layer, but the output of the buffer is a type of geometry as shown in the error message. Therefore, you need to create a new empty feature layer, add the buffer geometry to the newly created feature layer, then use the data provider to append all the features as a list, and finally, add it as a layer to the map canvas. Solution 2: Using Data Provider is easy to follow.
    – ahmadhanb
    Commented Nov 21, 2022 at 0:27
  • 2
    Is it possible to share a sample from your data to try to help solve your problem using your code?
    – ahmadhanb
    Commented Nov 21, 2022 at 0:48
  • 4
    It is better to add the comment as answer to help others facing the same issue.
    – ahmadhanb
    Commented Nov 21, 2022 at 0:55

1 Answer 1

3

Just to avoid keeping the question without an answer, as commented by @GeorgeC, most of the answer of solution 2 in Display buffer in a new layer - PyQGIS? solve the problem. Specifically, adding the following code will load the buffer layer correctly:

buffer_lyr=QgsVectorLayer("Polygon?crs=epsg:28355","buffer", "memory")     
f = QgsFeature()     
f.setGeometry(buffer)     
buffer_lyr.dataProvider().addFeatures([f])     
QgsProject.instance().addMapLayer(buffer_lyr)

The full code will be like this:


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
    buffer_lyr=QgsVectorLayer("Polygon?crs=epsg:28355","buffer", "memory")     
    f = QgsFeature()     
    f.setGeometry(buffer)     
    buffer_lyr.dataProvider().addFeatures([f])     
    QgsProject.instance().addMapLayer(buffer_lyr)

Your Answer

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

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