Skip to main content
added 368 characters in body
Source Link
Ben W
  • 23.2k
  • 3
  • 19
  • 43

Personally, I prefer to use the changeAttributeValues() method of QgsVectorDataProvider (see API docs here) as I find it more reliable. This method takes an attributes map which is basically a dictionary where the key is a feature id and the value is a second dictionary comprising field indexes as keys and the new attributes as values. Try

I have tested this starting with a csv file with the following structure:

enter image description here

After running the following code below:

srcFile = "D'D:\\Data\\filename.csv"
csv'

layer = QgsVectorLayer("Polygon?crs=EPSG:2193", "Polygons", "memory")
QgsProject.instance().addMapLayer(layer)
 
provider = layer.dataProvider()
provider.addAttributes([QgsField('ViewName',QVariant.String, '', 50, 0),
                        QgsField('FigName',QVariant.String, '', 10, 0)])
layer.updateFields()
f1 = layer.fields().lookupField('ViewName')
f2 = layer.fields().lookupField('FigName')
fid = 1

with open(srcFile,'r') as infile:
    rectcsv_lines = infile.readlines()[1:]
    
for bline in rect[1:]csv_lines:
    partsline = bline.split(',')
 
    #### Coordinates coming from CSV ####
    xMin = float(parts[2]line[2])
    yMin = float(parts[3]line[3])
    xMax = float(parts[4]line[4])
    yMax = float(parts[5]line[5])

    rect = QgsRectangle(xMin, yMin, xMax, yMax)
    polygon = QgsGeometry.fromRect(rect)
    
    feature = QgsFeature()
    feature.setGeometry(polygon)
    
    with edit(layer):
        provider.addFeatures([feature])
        provider.changeAttributeValues({feature.id():atts = {f1: parts[0]line[0], f2: parts[line[-2]}
        provider.changeAttributeValues({fid: atts})
        fid += 1

The resulting layer and attribute table looks like this:

enter image description here

Personally, I prefer to use the changeAttributeValues() method of QgsVectorDataProvider (see API docs here) as I find it more reliable. This method takes an attributes map which is basically a dictionary where the key is a feature id and the value is a second dictionary comprising field indexes as keys and the new attributes as values. Try the code below:

srcFile = "D:\\Data\\filename.csv"


layer = QgsVectorLayer("Polygon?crs=EPSG:2193", "Polygons", "memory")
QgsProject.instance().addMapLayer(layer)
 
provider = layer.dataProvider()
provider.addAttributes([QgsField('ViewName',QVariant.String, '', 50, 0),
                        QgsField('FigName',QVariant.String, '', 10, 0)])
layer.updateFields()
f1 = layer.fields().lookupField('ViewName')
f2 = layer.fields().lookupField('FigName')

with open(srcFile,'r') as infile:
    rect = infile.readlines()
    
for b in rect[1:]:
    parts = b.split(',')
 
    #### Coordinates coming from CSV ####
    xMin = float(parts[2])
    yMin = float(parts[3])
    xMax = float(parts[4])
    yMax = float(parts[5])

    rect = QgsRectangle(xMin, yMin, xMax, yMax)
    polygon = QgsGeometry.fromRect(rect)
    
    feature = QgsFeature()
    feature.setGeometry(polygon)
    
    with edit(layer):
        provider.addFeatures([feature])
        provider.changeAttributeValues({feature.id(): {f1: parts[0], f2: parts[-2]}})

Personally, I prefer to use the changeAttributeValues() method of QgsVectorDataProvider (see API docs here) as I find it more reliable. This method takes an attributes map which is basically a dictionary where the key is a feature id and the value is a second dictionary comprising field indexes as keys and the new attributes as values.

I have tested this starting with a csv file with the following structure:

enter image description here

After running the following code:

srcFile = 'D:\\Data\\filename.csv'

layer = QgsVectorLayer("Polygon?crs=EPSG:2193", "Polygons", "memory")
QgsProject.instance().addMapLayer(layer)
provider = layer.dataProvider()
provider.addAttributes([QgsField('ViewName',QVariant.String, '', 50, 0),
                        QgsField('FigName',QVariant.String, '', 10, 0)])
layer.updateFields()
f1 = layer.fields().lookupField('ViewName')
f2 = layer.fields().lookupField('FigName')
fid = 1

with open(srcFile,'r') as infile:
    csv_lines = infile.readlines()[1:]

for line in csv_lines:
    line = line.split(',')
    #### Coordinates coming from CSV ####
    xMin = float(line[2])
    yMin = float(line[3])
    xMax = float(line[4])
    yMax = float(line[5])

    rect = QgsRectangle(xMin, yMin, xMax, yMax)
    polygon = QgsGeometry.fromRect(rect)
    
    feature = QgsFeature()
    feature.setGeometry(polygon)
    
    with edit(layer):
        provider.addFeatures([feature])
        atts = {f1: line[0], f2: line[-2]}
        provider.changeAttributeValues({fid: atts})
        fid += 1

The resulting layer and attribute table looks like this:

enter image description here

added 348 characters in body
Source Link
Ben W
  • 23.2k
  • 3
  • 19
  • 43

Personally, I prefer to use the provider.changeAttributeValues() method. of QgsVectorDataProvider (see API docs here) as I find it more reliable. This method takes an attributes map which is basically a dictionary where the key is a feature id and the value is a second dictionary comprising field indexes as keys and the new attributes as values. Try the code below:

srcFile = "D:\\Data\\filename.csv"


layer = QgsVectorLayer("Polygon?crs=EPSG:2193", "Polygons", "memory")
QgsProject.instance().addMapLayer(layer)

provider = layer.dataProvider()
provider.addAttributes([QgsField('ViewName',QVariant.String, '', 50, 0),
                        QgsField('FigName',QVariant.String, '', 10, 0)])
layer.updateFields()
f1 = layer.fields().lookupField('ViewName')
f2 = layer.fields().lookupField('FigName')

with open(srcFile,'r') as infile:
    rect = infile.readlines()
    
for b in rect[1:]:
    parts = b.split(',')

    #### Coordinates coming from CSV ####
    xMin = float(parts[2])
    yMin = float(parts[3])
    xMax = float(parts[4])
    yMax = float(parts[5])

    rect = QgsRectangle(xMin, yMin, xMax, yMax)
    polygon = QgsGeometry.fromRect(rect)
    
    feature = QgsFeature()
    feature.setGeometry(polygon)
    
    with edit(layer):
        provider.addFeatures([feature])
        provider.changeAttributeValues({feature.id(): {f1: parts[0], f2: parts[-2]}})

Personally, I prefer to use the provider.changeAttributeValues() method. I find it more reliable. Try the code below:

srcFile = "D:\\Data\\filename.csv"


layer = QgsVectorLayer("Polygon?crs=EPSG:2193", "Polygons", "memory")
QgsProject.instance().addMapLayer(layer)

provider = layer.dataProvider()
provider.addAttributes([QgsField('ViewName',QVariant.String, '', 50, 0),
                        QgsField('FigName',QVariant.String, '', 10, 0)])
layer.updateFields()
f1 = layer.fields().lookupField('ViewName')
f2 = layer.fields().lookupField('FigName')

with open(srcFile,'r') as infile:
    rect = infile.readlines()
    
for b in rect[1:]:
    parts = b.split(',')

    #### Coordinates coming from CSV ####
    xMin = float(parts[2])
    yMin = float(parts[3])
    xMax = float(parts[4])
    yMax = float(parts[5])

    rect = QgsRectangle(xMin, yMin, xMax, yMax)
    polygon = QgsGeometry.fromRect(rect)
    
    feature = QgsFeature()
    feature.setGeometry(polygon)
    
    with edit(layer):
        provider.addFeatures([feature])
        provider.changeAttributeValues({feature.id(): {f1: parts[0], f2: parts[-2]}})

Personally, I prefer to use the changeAttributeValues() method of QgsVectorDataProvider (see API docs here) as I find it more reliable. This method takes an attributes map which is basically a dictionary where the key is a feature id and the value is a second dictionary comprising field indexes as keys and the new attributes as values. Try the code below:

srcFile = "D:\\Data\\filename.csv"


layer = QgsVectorLayer("Polygon?crs=EPSG:2193", "Polygons", "memory")
QgsProject.instance().addMapLayer(layer)

provider = layer.dataProvider()
provider.addAttributes([QgsField('ViewName',QVariant.String, '', 50, 0),
                        QgsField('FigName',QVariant.String, '', 10, 0)])
layer.updateFields()
f1 = layer.fields().lookupField('ViewName')
f2 = layer.fields().lookupField('FigName')

with open(srcFile,'r') as infile:
    rect = infile.readlines()
    
for b in rect[1:]:
    parts = b.split(',')

    #### Coordinates coming from CSV ####
    xMin = float(parts[2])
    yMin = float(parts[3])
    xMax = float(parts[4])
    yMax = float(parts[5])

    rect = QgsRectangle(xMin, yMin, xMax, yMax)
    polygon = QgsGeometry.fromRect(rect)
    
    feature = QgsFeature()
    feature.setGeometry(polygon)
    
    with edit(layer):
        provider.addFeatures([feature])
        provider.changeAttributeValues({feature.id(): {f1: parts[0], f2: parts[-2]}})
Source Link
Ben W
  • 23.2k
  • 3
  • 19
  • 43

Personally, I prefer to use the provider.changeAttributeValues() method. I find it more reliable. Try the code below:

srcFile = "D:\\Data\\filename.csv"


layer = QgsVectorLayer("Polygon?crs=EPSG:2193", "Polygons", "memory")
QgsProject.instance().addMapLayer(layer)

provider = layer.dataProvider()
provider.addAttributes([QgsField('ViewName',QVariant.String, '', 50, 0),
                        QgsField('FigName',QVariant.String, '', 10, 0)])
layer.updateFields()
f1 = layer.fields().lookupField('ViewName')
f2 = layer.fields().lookupField('FigName')

with open(srcFile,'r') as infile:
    rect = infile.readlines()
    
for b in rect[1:]:
    parts = b.split(',')

    #### Coordinates coming from CSV ####
    xMin = float(parts[2])
    yMin = float(parts[3])
    xMax = float(parts[4])
    yMax = float(parts[5])

    rect = QgsRectangle(xMin, yMin, xMax, yMax)
    polygon = QgsGeometry.fromRect(rect)
    
    feature = QgsFeature()
    feature.setGeometry(polygon)
    
    with edit(layer):
        provider.addFeatures([feature])
        provider.changeAttributeValues({feature.id(): {f1: parts[0], f2: parts[-2]}})