使用Python将自定义功能属性添加到ESRI Shapefile

时间:2010-11-18 14:13:37

标签: esri geodjango shapefile osgeo ogr

我正在寻找一种方法来获取功能集为200个国家/地区的现有ESRI Shapefile。每个国家/地区要素都具有“NAME”属性。我的目标是创建一个Python脚本,添加一个任意(现在)的附加属性,比如“POPULATION”。

当然我安装了OSGeo和GeoDjango模块。我到目前为止:

from osgeo import ogr

infile = ogr.Open('sample.shp', 1) #'sample.shp' is a pre-existing ESRI shapefile described above
inlyr = infile.GetLayerByIndex(0)

我是否缺少一个允许我将Feature属性字段插入现有Shapefile的OGR函数?

1 个答案:

答案 0 :(得分:7)

要添加字段,您必须创建一个OGRFieldDefn,然后调用inlyr.CreateField

fieldDefn = ogr.FieldDefn('POPULATION', ogr.OFTReal) 
fieldDefn.SetWidth(14) 
fieldDefn.SetPrecision(6)
inlyr.CreateField(fieldDefn)
相关问题