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

发布于 2024-10-03 03:38:45 字数 396 浏览 0 评论 0原文

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

当然,我安装了 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)

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

杀お生予夺 2024-10-10 03:38:45

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

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

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文