简体   繁体   English

通过新的Writer对象在Python中编辑shapefile不起作用

[英]Editing shapefiles in Python via new Writer object isn't working

I'm currently following a Python geospatial analysis tutorial book by Joel Lawhead, and I'm running into an error when trying to edit shapefiles. 我目前正在关注Joel Lawhead撰写的Python地理空间分析教程,并且在尝试编辑shapefile时遇到错误。

The shapefile I'm using is available a http://git.io/vLd8Y . 我正在使用的shapefile可从http://git.io/vLd8Y获得 I'm running my code in a Jupyter Notebook on python3. 我正在python3上的Jupyter Notebook中运行我的代码。

This is my code. 这是我的代码。 I'm simply reading in a shapefile as a Reader object r , and creating a new Writer object w with the same shapetype as r . 我只是在读取shapefile中的Reader对象r ,并创建一个与r具有相同形状类型的新Writer对象w Then, I'm attempting to attach the records from r to w . 然后,我尝试将记录从r附加到w

import shapefile
r = shapefile.Reader("NYC_MUSEUMS_GEO")
w = shapefile.Writer(r.shapeType)
w.fields = list(r.fields)
w.records.extend(r.records())

However, I'm running into this error: 但是,我遇到了这个错误:

AttributeError                            Traceback (most recent call last)
<ipython-input-151-ceee096fbafa> in <module>()
      6 w = shapefile.Writer(r.shapeType)
      7 w.fields = list(r.fields)
----> 8 w.records.extend(r.records())

AttributeError: 'Writer' object has no attribute 'records'

Any ideas why? 有什么想法吗?

I am unable to reproduce the problem you've described. 我无法重现您描述的问题。 I started with: 我开始于:

$ python
Python 3.5.4 (default, Oct  9 2017, 12:07:29) 
>>>

And install the pyshp module: 并安装pyshp模块:

$ pip install pyshp
...
Successfully installed pyshp-1.2.12

And now: 现在:

$ python
Python 2.7.13 (default, Dec  1 2017, 09:21:53) 
[GCC 6.4.1 20170727 (Red Hat 6.4.1-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import shapefile
>>> r = shapefile.Reader("NYC_MUSEUMS_GEO")
>>> w = shapefile.Writer(r.shapeType)
>>> w.fields = list(r.fields)
>>> w.records.extend(r.records())
>>> len(w.records)
130

Do the python and pyshp versions you are working with match what I am using? 您使用的pythonpyshp版本与我使用的版本是否匹配? If not, can you update your question to include the specific details of your environment? 如果不是,您是否可以更新您的问题以包括您的环境的特定详细信息?

I've been having this same issue. 我一直在遇到同样的问题。 It seems that w.records.extend(r.records()) is no longer possible to do in the new version of pyshp. 在新版本的pyshp中似乎无法再执行w.records.extend(r.records())了。 The correct method to do this is now: 现在,正确的方法是:

for shaperec in r.iterShapeRecords():
   w.record(*shaperec.record)
   w.shape(shaperec.shape)

Please see an explanation for the change and more context around the above code here . 在此处查看上述代码的更改说明和更多上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM