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])
2 Answers 2
Change 'org'
into 'memory'
and add Prj = QgsProject.instance()
line before using Prj
.
...
# Create a layer for the feature, in the desired CRS
lyr = QgsVectorLayer('Polygon?crs=epsg:29194', '200905_Bdy', "memory")
Prj = QgsProject.instance() # ADD THIS LINE
Prj.addMapLayers([lyr])
Since you add one layer, you can use Prj.addMapLayer(lyr)
instead of Prj.addMapLayers([lyr])
.
...
-
Kadir - thanks for the reply. I did set a reference Prj = QgsProject.instance() higher up in the script but forgot to include it in the post. However this did not work as I still get the error TypeError: QgsProject.addMapLayers(): argument 1 has unexpected type 'QgsVectorLayer' when I run the line Prj.addMapLayers(lyr)user2627043– user26270432020年09月08日 06:26:01 +00:00Commented Sep 8, 2020 at 6:26
-
You should use
addMapLayer
, notaddMapLayers
....Layers([lyr])
->...Layer(lyr)
Kadir Şahbaz– Kadir Şahbaz2020年09月08日 06:44:10 +00:00Commented Sep 8, 2020 at 6:44 -
Thanks, Kadir that works no to get it to display!user2627043– user26270432020年09月08日 06:47:00 +00:00Commented Sep 8, 2020 at 6:47
-
I found using prv.addFeatures([ftr]) more meaningful as the console reports on execuation 'True, [<qgis._core.QgsFeature object at 0x000002116B69A288>])', while using prv.addFeatures(ftr) there console reports is '(True, [])' , the latter being a bit crytpic for the novice scripteruser2627043– user26270432020年09月08日 23:03:41 +00:00Commented Sep 8, 2020 at 23:03
You must start editing before adding the feature to the layer and commit the changes after you are done:
if feature.isValid():
layer.startEditing()
layer.addFeatures([feature])
commited = layer.commitChanges()
if commited:
return True
else:
print(f'{layer.commitErrors()}')
return False