Python中大型Shapefile的内存错误

2024-05-14 20:57:56 发布

您现在位置:Python中文网/ 问答频道 /正文

import shapefile
data = shapefile.Reader("data_file.shp")
shapes = data.shapes()

我的问题是,从Shapefile读取器获取形状时,使用Pyshp时会出现异常MemoryError。在

.shp文件相当大,为1.2GB。但我只使用了我机器32gB的3%,所以我不明白。在

我还有别的办法吗?可以在Python中分块处理文件吗?或者使用一些工具将文件分成缝隙,然后分别处理它们?在


Tags: 文件importdata读取器readerfile形状shapefile
2条回答

引用托马斯this answer的话:

The MemoryError exception that you are seeing is the direct result of running out of available RAM. This could be caused by either the 2GB per program limit imposed by Windows (32bit programs), or lack of available RAM on your computer. (This link is to a previous question). You should be able to extend the 2GB by using 64bit copy of Python, provided you are using a 64bit copy of windows.

因此,请尝试64位的Python副本,或者提供有关您的平台和Python版本的更多详细信息。在

虽然我还没能测试它,但是Pyshp应该能够读取它,而不管文件大小或内存限制如何。创建Reader实例不会加载整个文件,只加载头信息。在

这里的问题似乎是您使用了shapes()方法,它一次将所有形状信息读入内存。这通常不是个问题,但是对于这么大的文件来说。一般来说,您应该使用iterShapes()方法,该方法逐个读取每个形状。在

import shapefile
data = shapefile.Reader("data_file.shp")
for shape in data.iterShapes():
    # do something...

相关问题 更多 >

    热门问题