python使用BaseMap画地图时出现解码错误

最近在使用python的basemap库画中国地图的时候,出现读取地图的文件时出现编码错误。

错误如下:

Traceback (most recent call last):
  File "C:/Users/admin/PycharmProjects/untitled/TC/track_plot.py", line 97, in <module>
    shp_info = bmap.readshapefile(r'F:\CHN_adm_shp\CN_pro', 'states', drawbounds=False)
  File "C:\Users\admin\PycharmProjects\untitled\venv\lib\site-packages\mpl_toolkits\basemap\__init__.py", line 2148, in readshapefile
    for shprec in shf.shapeRecords():
  File "C:\Users\admin\PycharmProjects\untitled\venv\lib\site-packages\shapefile.py", line 1039, in shapeRecords
    for rec in zip(self.shapes(), self.records())])
  File "C:\Users\admin\PycharmProjects\untitled\venv\lib\site-packages\shapefile.py", line 1012, in records
    r = self.__record(oid=i)
  File "C:\Users\admin\PycharmProjects\untitled\venv\lib\site-packages\shapefile.py", line 987, in __record
    value = u(value, self.encoding, self.encodingErrors)
  File "C:\Users\admin\PycharmProjects\untitled\venv\lib\site-packages\shapefile.py", line 104, in u
    return v.decode(encoding, encodingErrors)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xba in position 0: invalid start byte

就是在使用basemap的函数readshapefile时:

shp_info = bmap.readshapefile(r'F:\CHN_adm_shp\CN_pro', 'states', drawbounds=False)

报错:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xba in position 0: invalid start byte

问题就是由于字符编码的问题,utf-8不识别shp地图中的字符串。

查阅了很多网上的资料,有的提到更改utf-8头文件了什么之类,发现没有什么作用,还有更改什么shapefile.py里面的解码格式,发现对我的里面找不到相同格式,可能是python版本或者basemap库的版本不一样吧。

他的思想是:

return v.decode('latin-1')

就是将解码格式设置为 latin-1格式。

这个可以参考http://wap.sciencenet.cn/blog-2410131-1194788.html?mobile=1

然后我就查找basemap的函数readshapefile函数

def readshapefile(self,shapefile,name,drawbounds=True,zorder=None,
                      linewidth=0.5,color='k',antialiased=1,ax=None,
                      default_encoding='utf-8'):

发现函数里面的编解码格式如下:

import shapefile as shp
        from shapefile import Reader
        shp.default_encoding = default_encoding
        if not os.path.exists('%s.shp'%shapefile):
            raise IOError('cannot locate %s.shp'%shapefile)
        if not os.path.exists('%s.shx'%shapefile):
            raise IOError('cannot locate %s.shx'%shapefile)
        if not os.path.exists('%s.dbf'%shapefile):
            raise IOError('cannot locate %s.dbf'%shapefile)
        # open shapefile, read vertices for each object, convert
        # to map projection coordinates (only works for 2D shape types).
        try:
            shf = Reader(shapefile, encoding=default_encoding)
        except:
            raise IOError('error reading shapefile %s.shp' % shapefile)

于是我将我得编解码的默认格式也设置为 latin-1格式。

def readshapefile(self,shapefile,name,drawbounds=True,zorder=None,
                      linewidth=0.5,color='k',antialiased=1,ax=None,
                      default_encoding='latin-1'):

问题迎刃而解。

再来个友情提示,直接使用python里面下载中国地图的库是在西藏地区是少一块的,希望用到的人注意一下。

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值