Search code examples
pythoncartopy

ShapelyFeature.Reader not Working in Cartopy


I am running the following simple code:

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import cartopy.crs as ccrs
from cartopy.io import shapereader as shpreader
from cartopy.feature import ShapelyFeature

fname = r'C:/cb_2018_us_cd116_500k.shp'

ax = plt.axes(projection=ccrs.Robinson())
shape_feature = ShapelyFeature(shpreader(fname).geometries(),
                                ccrs.PlateCarree(), facecolor='none')
ax.add_feature(shape_feature)
plt.show()

I get the following error:

shape_feature = ShapelyFeature(shpreader(fname).geometries(),
TypeError: 'module' object is not callable

Can someone help?

I'm running Python 3.6 in Pycharm on Windows 10


Solution

  • shapereader as shpreader is module, what you want is class present in that module i.e. cartopy.io.shapereader.Reader

    shape_feature = ShapelyFeature(shpreader.Reader(fname).geometries(),
                                    ccrs.PlateCarree(), facecolor='none')