22

I am trying to change the field value of a shapefile. However it doesn't change the value. What's wrong with my code?

import ogr

driver = ogr.GetDriverByName('ESRI Shapefile')
fn = 'dist.shp'
dataSource = driver.Open(fn, 0)

layer = dataSource.GetLayer()
feature = layer.GetNextFeature()

dist = 233

while feature:
    feature.SetField("dist", dist)
    layer.SetFeature(feature)
    feature = layer.GetNextFeature()

dataSource.Destroy()
underdark
  • 84,148
  • 21
  • 231
  • 413
ustroetz
  • 7,994
  • 10
  • 72
  • 118

1 Answers1

27

The second argument in Open specifies if the data can be updated (written to). Try:

dataSource = driver.Open(fn,1)
Aaron
  • 51,658
  • 28
  • 154
  • 317
Matthew Snape
  • 6,127
  • 2
  • 27
  • 48