使用Python将自定义要素属性添加到ESRI Shapefile [英] Adding custom Feature attributes to ESRI Shapefile with Python

查看:401
本文介绍了使用Python将自定义要素属性添加到ESRI Shapefile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种采用现有ESRI Shapefile的方法,该文件具有200个国家/地区的功能集.每个国家/地区要素的属性均为"NAME".我的目标是创建一个Python脚本,该脚本添加一个任意的(暂时)附加属性,例如"POPULATION".

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".

我当然已经安装了OSGeo和GeoDjango模块.我距离:

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)

我是否缺少OGR函数,该函数将允许我将Feature属性字段插入到现有的Shapefile中?

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

推荐答案

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

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)

这篇关于使用Python将自定义要素属性添加到ESRI Shapefile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆