I've been working on this problem for several questions now - the most recent one here. I believe the code below had created the polygon from the points, and added this polygon to a layer as a polygon in a feature object. The layer is also added to the table of contents. However, for some reason I don't understand the polygon does not display on the canvas?
# Attach libraries
from qgis.core import * # attach main QGIS library
from qgis.utils import * # attach main python library
import os # attach operating system library
Set a object for the current project
Prj = QgsProject.instance() # Object for current project
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 scratch layer for the feature, in the desired CRS
lyr = QgsVectorLayer('Polygon?crs=epsg:29194', '200909_Bdy',"memory")
Prj.addMapLayers([lyr])
Set an object for the data provider for the layer (not sure why I need this?)
prv = lyr.dataProvider()
Make the layer editable
lyr.startEditing()
Add the feature with the polygon to the layer (this fails)
lyr.addFeatures(ftr)
Save the layer changes
lyr.commitChanges()
refresh the canvas to show the polygon ?
iface.mapCanvas().refreshAllLayers()
I also expect there is method to interrogate each object to determine what objects have been appended as I go like the line print(ftr.geometry()) which reports the that the object contains a polygon with listed coordinates.
Is there a similar function, which reports what is in the layer?
iface.zoomToActiveLayer()should work. – Ben W Sep 09 '20 at 10:53