4

For example, how to translate this code into pyqgis?

    rows = gp.UpdateCursor("D:/St_Johns/data.mdb/roads")
    row = rows.Next()

    while row:
       row.buffer_distance = row.road_type * 100
       rows.UpdateRow(row)
       print row
       row = rows.Next()

errata: buffer_distance is a calculate column road_type is a calumn with calculate value

Follow your advice I type this code in to python windows:

>>> from qgis.core import QgsVectorLayer, QgsFeature
>>> layer = QgsVectorLayer(r"D:\fold", "boundingBoxes.shp", "ogr")
>>> road_type_index = layer.fieldNameIndex("road_type")
>>> buffer_distance_index = layer.fieldNameIndex("buffer_dis")
>>> layer.select(layer.pendingAllAttributesList())
>>> layer.startEditing()
True
>>> for feature in layer:
...     newvalue = feature.attributeMap()[road_type_index].toInt()[0] * 100
...     feature.changeAttribute(buffer_distance_index, newvalue)
...     layer.updateFeature(feature)
...

next when I press "enter" the qgis (1.7.4) crash and exit

7
  • is buffer_distance a column? I don't know arcpy
    – Nathan W
    Commented May 6, 2012 at 10:42
  • sorry I just add errata
    – user7172
    Commented May 6, 2012 at 10:51
  • so is buffer_distance created within this code block or created before. Just so I can understand what is needed from a pyqgis point of view.
    – Nathan W
    Commented May 6, 2012 at 10:54
  • I have updated my code example to show what needs to go in QgsVectorLayer better. See layer = QgsVectorLayer(r"D:\fold\boundingBoxes.shp", "boundingBoxes", "ogr")
    – Nathan W
    Commented May 7, 2012 at 0:33
  • you can also check, layer.isValid() to make sure you have a valid layer before moving on
    – Nathan W
    Commented May 7, 2012 at 0:33

2 Answers 2

7

This should get you started

from qgis.core import QgsVectorLayer, QgsFeature
layer = QgsVectorLayer(r"D:\fold\boundingBoxes.shp", "boundingBoxes", "ogr")

road_type_index = layer.fieldNameIndex("road_type")
buffer_distance_index = layer.fieldNameIndex("buffer_distance")

layer.select(layer.pendingAllAttributesList())

layer.startEditing()
for feature in layer:
   newvalue = feature.attributeMap()[road_type_index].toInt()[0] * 100
   feature.changeAttribute(buffer_distance_index, newvalue)
   layer.updateFeature(feature)

layer.commitChanges()

If in 1.7.4 try this

from qgis.core import QgsVectorLayer, QgsFeature
layer = QgsVectorLayer(r"D:\fold\boundingBoxes.shp", "boundingBoxes", "ogr")

road_type_index = layer.fieldNameIndex("road_type")
buffer_distance_index = layer.fieldNameIndex("buffer_distance")

layer.select(layer.pendingAllAttributesList())

layer.startEditing()
for feature in layer:
   newvalue = feature.attributeMap()[road_type_index].toInt()[0] * 100
   layer.changeAttributeValue(feature.id(),buffer_distance_index,newvalue) 

layer.commitChanges()
5
  • don't post code in the comments. It's hard to read. Edit your answer to include the code you have tried.
    – Nathan W
    Commented May 6, 2012 at 14:03
  • I tried version for 1.7.4, but still crashes QGIS. Later I will test on the another computer.
    – user7172
    Commented May 7, 2012 at 11:18
  • Can you print the values of road_type_index and buffer_distance_index
    – Nathan W
    Commented May 7, 2012 at 12:07
  • yes, values are print correctly
    – user7172
    Commented May 7, 2012 at 19:04
  • I tried call script by execfile(r'C:\Program Files\Quantum GIS Wroclaw\apps\qgis\python\plugins\exec.py') but without success (*exec.py is code for 1.7.4)
    – user7172
    Commented May 7, 2012 at 19:33
0

Geopandas provides a very intuitive way to update feature attributes. For example, the following is how you would use Geopandas and .itertuples() to update rows as you would an Esri Update Cursor:

import geopandas as gpd

shp = '/path/to/your/shapefile.shp'

# Read shp
data = gpd.read_file(shp)

# Iterate over rows and update buffer_distance field
for row in data.itertuples():
    data.at[row.Index, 'buffer_distance'] = row.road_type *100

# Write to shapefile
data.to_file('/path/to/your/shapefile_updated.shp')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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