首页 > 解决方案 > Python3.8--Cartopy/Fiona 错误:shapefile 未被识别为支持的文件格式

问题描述

在这里用 Cartopy 制作一个简单的情节时遇到了麻烦。好吧,这个问题突然出现在一个更复杂的情节中,但归结为简单地让 Cartopy 摄取州边界的 shapefile。

快速代码快照:

#!/home/fewx/anaconda3/bin/python3.8

import matplotlib.pyplot as plt
import xarray as xr
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from cartopy.io.shapereader import Reader
from cartopy.feature import ShapelyFeature
import cartopy
import geopandas as gpd
import matplotlib.ticker as mticker
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER

ax = plt.axes(projection=ccrs.PlateCarree())

ax.set_extent([235., 290., 20., 55.])
ax.add_feature(cfeature.COASTLINE.with_scale('110m'), linewidth=0.5)
ax.add_feature(cfeature.STATES.with_scale('110m'), linewidth=0.5)

##Print default location of needed natural earth datasets
path = cartopy.config['data_dir']
print(path)

# Display the plot
#plt.show()
plt.savefig('cartopy_test.png')

海岸线 shapefile 绘制得非常好,但是在尝试访问必要的状态 shapefile 'ne_110m_admin_1_states_provinces_lakes.shp' 时失败,并出现错误:

ERROR:fiona._env:`/home/fewx/.local/share/cartopy/shapefiles/natural_earth/cultural/ne_110m_admin_1_states_provinces_lakes.shp' not recognized as a supported file format.

Traceback (most recent call last):
  File "fiona/_shim.pyx", line 83, in fiona._shim.gdal_open_vector
  File "fiona/_err.pyx", line 291, in fiona._err.exc_wrap_pointer
fiona._err.CPLE_OpenFailedError: '/home/fewx/.local/share/cartopy/shapefiles/natural_earth/cultural/ne_110m_admin_1_states_provinces_lakes.shp' not recognized as a supported file format.

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "./plot_cartopy_contourf.py", line 35, in <module>
    plt.savefig('cartopy_test.png')
  File "/home/fewx/anaconda3/lib/python3.8/site-packages/matplotlib/pyplot.py", line 859, in savefig
    res = fig.savefig(*args, **kwargs)
  File "/home/fewx/anaconda3/lib/python3.8/site-packages/matplotlib/figure.py", line 2311, in savefig
    self.canvas.print_figure(fname, **kwargs)
  File "/home/fewx/anaconda3/lib/python3.8/site-packages/matplotlib/backends/backend_qt5agg.py", line 81, in print_figure
    super().print_figure(*args, **kwargs)
  File "/home/fewx/anaconda3/lib/python3.8/site-packages/matplotlib/backend_bases.py", line 2210, in print_figure
    result = print_method(
  File "/home/fewx/anaconda3/lib/python3.8/site-packages/matplotlib/backend_bases.py", line 1639, in wrapper
    return func(*args, **kwargs)
  File "/home/fewx/anaconda3/lib/python3.8/site-packages/matplotlib/backends/backend_agg.py", line 509, in print_png
    FigureCanvasAgg.draw(self)
  File "/home/fewx/anaconda3/lib/python3.8/site-packages/matplotlib/backends/backend_agg.py", line 407, in draw
    self.figure.draw(self.renderer)
  File "/home/fewx/anaconda3/lib/python3.8/site-packages/matplotlib/artist.py", line 41, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/home/fewx/anaconda3/lib/python3.8/site-packages/matplotlib/figure.py", line 1863, in draw
    mimage._draw_list_compositing_images(
  File "/home/fewx/anaconda3/lib/python3.8/site-packages/matplotlib/image.py", line 131, in _draw_list_compositing_images
    a.draw(renderer)
  File "/home/fewx/anaconda3/lib/python3.8/site-packages/matplotlib/artist.py", line 41, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/home/fewx/anaconda3/lib/python3.8/site-packages/cartopy/mpl/geoaxes.py", line 479, in draw
    return matplotlib.axes.Axes.draw(self, renderer=renderer, **kwargs)
  File "/home/fewx/anaconda3/lib/python3.8/site-packages/matplotlib/artist.py", line 41, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/home/fewx/anaconda3/lib/python3.8/site-packages/matplotlib/cbook/deprecation.py", line 411, in wrapper
    return func(*inner_args, **inner_kwargs)
  File "/home/fewx/anaconda3/lib/python3.8/site-packages/matplotlib/axes/_base.py", line 2747, in draw
    mimage._draw_list_compositing_images(renderer, self, artists)
  File "/home/fewx/anaconda3/lib/python3.8/site-packages/matplotlib/image.py", line 131, in _draw_list_compositing_images
    a.draw(renderer)
  File "/home/fewx/anaconda3/lib/python3.8/site-packages/matplotlib/artist.py", line 41, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/home/fewx/anaconda3/lib/python3.8/site-packages/cartopy/mpl/feature_artist.py", line 155, in draw
    geoms = self._feature.intersecting_geometries(extent)
  File "/home/fewx/anaconda3/lib/python3.8/site-packages/cartopy/feature/__init__.py", line 302, in intersecting_geometries
    return super(NaturalEarthFeature, self).intersecting_geometries(extent)
  File "/home/fewx/anaconda3/lib/python3.8/site-packages/cartopy/feature/__init__.py", line 110, in intersecting_geometries
    return (geom for geom in self.geometries() if
  File "/home/fewx/anaconda3/lib/python3.8/site-packages/cartopy/feature/__init__.py", line 287, in geometries
    geometries = tuple(shapereader.Reader(path).geometries())
  File "/home/fewx/anaconda3/lib/python3.8/site-packages/cartopy/io/shapereader.py", line 197, in __init__
    with fiona.open(filename) as f:
  File "/home/fewx/anaconda3/lib/python3.8/site-packages/fiona/env.py", line 417, in wrapper
    return f(*args, **kwargs)
  File "/home/fewx/anaconda3/lib/python3.8/site-packages/fiona/__init__.py", line 256, in open
    c = Collection(path, mode, driver=driver, encoding=encoding,
  File "/home/fewx/anaconda3/lib/python3.8/site-packages/fiona/collection.py", line 162, in __init__
    self.session.start(self, **kwargs)
  File "fiona/ogrext.pyx", line 540, in fiona.ogrext.Session.start
  File "fiona/_shim.pyx", line 90, in fiona._shim.gdal_open_vector
fiona.errors.DriverError: '/home/fewx/.local/share/cartopy/shapefiles/natural_earth/cultural/ne_110m_admin_1_states_provinces_lakes.shp' not recognized as a supported file format.

请注意,'ne_110m_admin_1_states_provinces_lakes.shp' 的父 zip 文件直接不存在于相应的目录中,主要是因为 NACISCDN (再次)关闭,我需要从 github 中提取文件。

有什么想法会导致这种情况吗?我在这里有点新手......这个测试代码运行在一个全新的基于 conda 的 python 安装上,该安装是在过去一周内构建的。我是否有不兼容的库版本(例如 PROJ、GDAL 等)?这个错误似乎在这个较新的安装中突然出现。

谢谢你的帮助!

更新:: 我成功绘制了状态,但我需要通过 Conda 物理绘制 cartopy_offlinedata 包中的“ne_110m_admin_1_states_provinces_shp.shp”文件。这需要通过 cfeat.NaturalEarthFeature() 创建一个新的独立特征,并指定要使用的 shapefile:

states_provinces = cfeat.NaturalEarthFeature(
        category='cultural',
        name='admin_1_states_provinces_shp',
        scale='110m',
        facecolor='none')

ax.add_feature(states_provinces, edgecolor='black')

这让我觉得我已经损坏了我试图绘制的 shapefile,导致失败......但我不能确定直到我可以从 NACISCDN 拉下完整的包——它仍然处于关闭状态。

标签: python-3.xgeopandascartopy

解决方案


推荐阅读