最近在使用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里面下载中国地图的库是在西藏地区是少一块的,希望用到的人注意一下。