3

I am trying to convert a few lists into fields. I have no problem creating the fields. I just don't have any idea on how to fill them.

if result:
    layer = self.dlg.comboLayerLine.currentData()
    features = layer.getFeatures()
    ID = []
    X = []
    Y = []
    for feature in features:
        geom = feature.geometry()
            
        if geom.type() == QgsWkbTypes.PointGeometry:
            co = geom.asPoint()

            ID.append(feature.id())
            X.append(co[0]
            Y.append(co[1])

        elif geom.type() == QgsWkbTypes.LineGeometry:
            #Do the same with Line geometry

        elif geom.type() == QgsWkbTypes.PolygonGeometry:
            #Do the same with PolyLine geometry
           
        elif geom.type() == QgsWkbTypes.UnknownGeometry:
            #Prompt error box for unknown geometry

        #Add fields here

Here I make 3 differents lists. I want to convert each of them into 3 differents fields.

Edit: I was about to add a sample of the lists I get but I realized they don't even appear anymore! Here is the rest of my code.

new_layer = QgsVectorLayer(r'D:\Users\user\Desktop\trash temp', "essai.shp", "ogr") 
new_layer.startEditing() 
id_field = QgsField('ID', QVariant.String)
x_field = QgsField('X', QVariant.String)
y_field = QgsField('Y', QVariant.String)
new_layer.addAttribute(id_field)
new_layer.addAttribute(x_field)
new_layer.addAttribute(y_field)
print("Ok pressed")

Edit 2: I managed to fix the lists so here is the output of the list "y":

[6414701.507300033, 6416326.521299997, 6414870.419924344, 6414629.207300014, 6414839.753699968, 6414545.455399935, 6414596.490248112, 6414756.708799932, 6414599.551207293, 6414792.136099978, 6414952.573700016, 6414714.024700028, 6414630.641128357, 6414606.850143043, 6414661.849399988, 6414634.387000008, 6414601.288791229, 6414930.830580306, 6414609.777690075]

Edit 3: I tried something which didn't work. here it is:

#Add fields here 
layerFields = QgsFields()
layerFields.append(QgsField('ID', QVariant.Int))
layerFields.append(QgsField('x', QVariant.Double))
layerFields.append(QgsField('y', QVariant.Double))
path = r'D:\Users\me\Desktop\trash temp'
writer = QgsVectorFileWriter(path, 'UTF-8', layerFields, QgsWkbTypes.Point)
feat = QgsFeature(layerFields)
feat.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(attrs[1], attrs[2])))
for i in range(2, len(attrs), 3):
   print(attrs)
   feat.setAttributes(attrs[i-2], attrs[i-1], attrs[i])
   writer.addFeature(feat)

#Finish

layer = iface.addVectorLayer(fn, '','ogr')
del(writer)

It returns me the following error:

feat.setAttributes(attrs[i-2], attrs[i-1], attrs[i]) TypeError: QgsFeature.setAttributes(): argument 1 has unexpected type 'int'

Edit 4: After receiving a few advices, I decided to make a list of lists. So my new output is:

[[0, 506099.200000004, 6414701.507300033], [1, 505724.36900000286, 6416326.521299997], [2, 506076.8426659516, 6414870.419924344], [3, 506265.86200000404, 6414629.207300014], [4, 506945.2999999959, 6414839.753699968], [5, 506180.2869999982, 6414545.455399935], [6, 506047.2159920711, 6414596.490248112], [7, 506081.38259999966, 6414756.708799932], [8, 506045.6930898201, 6414599.551207293], [9, 506816.1533999958, 6414792.136099978], [10, 506090.7625000037, 6414952.573700016], [11, 506660.63870000176, 6414714.024700028], [12, 506112.8389316339, 6414630.641128357], [13, 506134.08904640994, 6414606.850143043], [14, 506548.10649999965, 6414661.849399988], [15, 506436.27269999706, 6414634.387000008], [16, 506303.8354239211, 6414601.288791229], [17, 506062.6988654828, 6414930.830580306], [18, 506047.52282746276, 6414609.777690075]]

Each list represents a future feature with its ID, X and Y coordinate

  • I did add the field ! The fact is that I absolutely don't know how to put those lists in the fields ! I actually looked for it but couldn't find much. – Cinouna Oct 20 at 7:06
1

What if you try the following code:

layer = QgsVectorLayer("Point", "Test", "memory")
crs = QgsCoordinateReferenceSystem("epsg:26912")
layer.setCrs(crs)

provider = layer.dataProvider()
provider.addAttributes([QgsField('ID', QVariant.Int), QgsField('X', QVariant.Double), QgsField('Y', QVariant.Double)])
layer.updateFields()

attributes = [[0, 506099.200000004, 6414701.507300033], [1, 505724.36900000286, 6416326.521299997], [2, 506076.8426659516, 6414870.419924344], [3, 506265.86200000404, 6414629.207300014], [4, 506945.2999999959, 6414839.753699968], [5, 506180.2869999982, 6414545.455399935], [6, 506047.2159920711, 6414596.490248112], [7, 506081.38259999966, 6414756.708799932], [8, 506045.6930898201, 6414599.551207293], [9, 506816.1533999958, 6414792.136099978], [10, 506090.7625000037, 6414952.573700016], [11, 506660.63870000176, 6414714.024700028], [12, 506112.8389316339, 6414630.641128357], [13, 506134.08904640994, 6414606.850143043], [14, 506548.10649999965, 6414661.849399988], [15, 506436.27269999706, 6414634.387000008], [16, 506303.8354239211, 6414601.288791229], [17, 506062.6988654828, 6414930.830580306], [18, 506047.52282746276, 6414609.777690075]]

for attr in attributes:
    f = QgsFeature()
    f.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(attr[1], attr[2])))
    f.setAttributes(attr)
    provider.addFeature(f)

QgsProject.instance().addMapLayer(layer)

The output will look like:

result

P.S.: I do not know which CRS are you working with


References:

| improve this answer | |
  • Your code worked perfectly ! Thank you very much for your help. By the way, I'm working with espg:26912. – Cinouna Oct 21 at 8:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.