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()

1 Answer 1

27

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

dataSource = driver.Open(fn,1)
1
  • This solution worked! confirmed from GDAL-PYTHON API. Default option is 0 i.e FALSE, otherwise 1 for TRUE incase updating is required
    – Erick
    Mar 9, 2018 at 16:49

Your Answer

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

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