I'm attempting to add a polygon to a new layer using PyQGIS in QGIS 3.14. Adapting some examples from questions and answers here and here. I believe I have successfully created the polygon from an array of (four) input points and then a feature that contains the polygon.
I have then created a layer for the feature in the CRS I desire but cannot work how to get the feature into the layer. Code so far below.
# Attach modules
from qgis.core import * # attach main QGIS library
from qgis.utils import * # attach main python library
import os # attach operating system library
Create an array [] object with the polygon vertices
vrtcs = []
vrtcs.append(QgsPointXY(396100,8969000))
vrtcs.append(QgsPointXY(396100,8973900))
vrtcs.append(QgsPointXY(397900,8973900))
vrtcs.append(QgsPointXY(397900,8969000))
Create a polygon from the coordinates
ply_01 = QgsGeometry.fromPolygonXY([vrtcs])
Create a feature object then put the polygon into the feature
ftr = QgsFeature()
ftr.setGeometry(ply_01)
print(ftr.geometry())
Create a layer for the feature, in the desired CRS
lyr = QgsVectorLayer('Polygon?crs=epsg:29194', '200905_Bdy',"org")
Prj.addMapLayers([lyr])
Set an object for the data provider for the layer
prv = lyr.dataProvider()
Add the feature to the layer using this provider (fails)
prv.addFeatures([ftr])
addMapLayer, notaddMapLayers....Layers([lyr])->...Layer(lyr)– Kadir Şahbaz Sep 08 '20 at 06:44