如何使用GDAL / OGR更改Shapefile的字段值?


18

我正在尝试更改shapefile的字段值。但是,它不会更改值。我的代码有什么问题?

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

Answers:


23

Open中的第二个参数指定是否可以更新(写入)数据。尝试:

dataSource = driver.Open(fn,1)


此解决方案有效!从GDAL-PYTHON API确认。默认选项为0,即FALSE,否则为1,如果需要更新则为TRUE
Erick
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.