Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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

share|improve this question
    
is buffer_distance a column? I don't know arcpy –  Nathan W May 6 '12 at 10:42
    
sorry I just add errata –  user7172 May 6 '12 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 May 6 '12 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 May 7 '12 at 0:33
    
you can also check, layer.isValid() to make sure you have a valid layer before moving on –  Nathan W May 7 '12 at 0:33

1 Answer 1

up vote 5 down vote accepted

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()
share|improve this answer
    
don't post code in the comments. It's hard to read. Edit your answer to include the code you have tried. –  Nathan W May 6 '12 at 14:03
    
I tried version for 1.7.4, but still crashes QGIS. Later I will test on the another computer. –  user7172 May 7 '12 at 11:18
    
Can you print the values of road_type_index and buffer_distance_index –  Nathan W May 7 '12 at 12:07
    
yes, values are print correctly –  user7172 May 7 '12 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 May 7 '12 at 19:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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