无法获取readshapefile()来读取.shp

时间:2017-06-13 01:42:17

标签: python matplotlib-basemap

所以我最近使用matplotlib和Basemap进入了使用Python的地图制作。出于某种原因,当我执行m.readshapefile()时,我的代码会中断,因为它无法找到.shp。

我为this下载了.zip,并将其放在桌面上的C:\ Users \ mattd \ Desktop \ pop \ ne_110m_populated_places中。我把

m.readshapefile('C:\Users\mattd\Desktop\pop\ne_110m_populated_places', 
            'populated_places')

并且它无法中断,因为它无法找到该文件。

1 个答案:

答案 0 :(得分:0)

在您附加的链接中,没有名为populated_places的文件 我找到了ne_110m_populated_places.shp。此外,将变量命名为自己的变量并在自己的代码中进行健全性检查,而不是依靠库来为您执行此操作,这也是一种很好的做法。

from os import path

shape_dir = 'C:\Users\mattd\Desktop\pop\ne_110m_populated_places'
shape_file = 'ne_110m_populated_places'
shape_file_full = shape_file + '.shp'

# check if we provided valid paths to our file and directory
if path.isdir(shape_dir) is False:
    print ("%s does not exist!" % (shape_dir))
    quit()

if path.isfile(shape_file_fill) is False:
    print ("%s does not exist" % (shape_file_full))
    quit()

# if it makes it here then you know it's an issue with the library
base_map = Basemap(...)
base_map.readshapefile(shape_dir, shape_file)