1

I want to create a temporary point layer from a list of points. The point list is:

arr_error = [[547162.6286000004, 187063.95659999922]]

vl = QgsVectorLayer('Point', 'Error_point', 'memory')
    pr = vl.dataProvider()
    pr.addAttributes([QgsField("Name", QVariant.String)])
    vl.updateFields()
    for x in arr_error:
        
        fet = QgsFeature()
        pt = QgsPointXY(x[0],x[1])
        
        fet.setGeometry(QgsGeometry.fromPointXY(pt))
        fet.setAttributes(['Error'])
        pr.addFeatures([fet])
    vl.updateExtents()
    QgsProject.instance().addMapLayers([vl])

This creates a layer, feature is added to layer. But geometry is not being set. What mistake i am doing in the code??

1 Answer 1

1

You need to define a Coordinate Reference System for your layer. You can do this when you define your QgsVectorLayer object like this:

vl = QgsVectorLayer('Point?crs=EPSG:3857', 'Error_point', 'memory')

Or subsequently like this:

vl = QgsVectorLayer('Point', 'Error_point', 'memory')
vl.setCrs(QgsCoordinateReferenceSystem('EPSG:3857'))

Note: I just used EPSG:3857 for this example, to show the syntax. Of course, you should use whatever CRS your point coordinates are actually in.

1
  • Yes, I had not provided the CRS information. I added CRS, and it worked fine. Thanks :) crs = QgsProject.instance().crs().authid() vl = QgsVectorLayer('Point?crs=' + crs, 'Error_point', 'memory') May 27, 2022 at 13:03

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.