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 (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...