如何解决AttributeError: 'Writer' 对象没有属性 'shapes'
我正在学习 Joel Lawhead 的《使用 Python 学习地理空间分析》。我有 python 3.7(如他的书中所建议)和最新的 pyshp 模块保存在 conda 发行版的虚拟环境中。我的工作也主要使用 Spyder。
下面是有问题的代码:
import glob
import shapefile
from dbfpy3 import dbf
shp_files = glob.glob('footprints_*.shp')
w = shapefile.Writer(shp = "merged2.shp",shx = "merged2.shx")
#Open only the .shp files and copy the geometries to the writer. Avoid opening dbf files to avoid field-parsing errors:
# Loop through ONLY the shp files and copy their shapes
# to a Writer object. We avoid opening the dbf files
# to prevent any field-parsing errors.
for f in shp_files:
print ("Shp: {}".format(f))
shpf = open(f,"rb")
r = shapefile.Reader(f)
r = shapefile.Reader(shp=shpf)
for s in r.shapes():
w.poly([s.points])
print("Num. shapes: {}". format(len(r.shapes())))
#Once all geometryhas been copied over to the writer; save the shp and have pyshp create an index file for the geom:
w.close()
#Get a list of .dbf files using glob:
dbf_files = glob.glob('*.dbf')
#Use first .dbf of list as a template to get field data and use it to set properties of the SHP writer:
template = dbf_files.pop(0)
merged_dbf_name = 'merged2.dbf'
#copy entire template to merged file:
merged_dbf = open(merged_dbf_name,'wb')
temp = open(template,'rb')
merged_dbf.write(temp.read())
merged_dbf.close()
temp.close()
#Simply loop through the .dbf files and copy records to Writer:
db = dbf.Dbf(merged_dbf_name)
for f in dbf_files:
print('Dbf: {}'.format(f))
dba = dbf.Dbf(f)
print('dba',len(dba))
for rec in dba:
db_rec = db.newRecord()
for k,v in rec.asDict().items():
db_rec[k] = v
db_rec.store()
db.close()
我收到以下错误:
文件“D:\Personal_Life\Python\LGAP\Code\Personal_Code\Chap4_merging_shp.py”,第 94 行,在 db_rec[k] = v
文件“C:\Users\farnh\anaconda3\envs\LGAP\lib\site-packages\dbfpy3\record.py”,第 264 行,setitem self.fieldData[self.dbf.indexOfFieldName(key)] = value
文件“C:\Users\farnh\anaconda3\envs\LGAP\lib\site-packages\dbfpy3\dbf.py”,第 206 行,在 indexOfFieldName 中 返回names.index(name.upper())
ValueError: 'OBJECTID' 不在列表中
这是什么意思?它实际上不是在读取 dbf 文件?关于如何解决它的任何想法都意味着很多!
非常感谢!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。