5

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])

Taras
  • 32,823
  • 4
  • 66
  • 137
user2627043
  • 403
  • 3
  • 12

2 Answers2

5

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 Şahbaz
  • 76,800
  • 56
  • 247
  • 389
  • 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 Sep 08 '20 at 06:26
  • You should use addMapLayer, not addMapLayers. ...Layers([lyr]) -> ...Layer(lyr) – Kadir Şahbaz Sep 08 '20 at 06:44
  • Thanks, Kadir that works no to get it to display! – user2627043 Sep 08 '20 at 06: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 scripter – user2627043 Sep 08 '20 at 23:03
3

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
Comrade Che
  • 7,091
  • 27
  • 58