17

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

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

dataSource = driver.Open(fn,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 '18 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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