开发者

Adding custom Feature attributes to ESRI Shapefile with Python

开发者 https://www.devze.com 2023-01-25 13:43 出处:网络
I am seeking a way to take an existing ESRI Shapefile that has a Feature set of 200 countries. Each country Feature has an attribute of \"NAME.\" My objective is to create a Python script that adds an

I am seeking a way to take an existing ESRI Shapefile that has a Feature set of 200 countries. Each country Feature has an attribute of "NAME." My objective is to create a Python script that adds an arbitrary (for now) additional attribute, say, "POPULATION开发者_运维问答".

Of course I have the OSGeo and GeoDjango modules installed. I'm as far as:

from osgeo import ogr

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

Am I missing an OGR function that will allow me to insert Feature attribute fields into an existing Shapefile?


To add a field you have to create an OGRFieldDefn and then call inlyr.CreateField

fieldDefn = ogr.FieldDefn('POPULATION', ogr.OFTReal) 
fieldDefn.SetWidth(14) 
fieldDefn.SetPrecision(6)
inlyr.CreateField(fieldDefn)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号