Skip to content

make pyshp optional #231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Changelog
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
version 1.0.8 (not yet released)
--------------------------------
* allow shapefile to be optional, so a system version can be used
in stead of the bundled version
* add 'textcolor' kwarg to drawmeridians and drawparallels (issue 145).
* don't assume grid is regular when adding cyclic point in addcyclic.
New kwargs 'axis' and 'cyclic' added. More than one array
Expand Down
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ include lib/mpl_toolkits/__init__.py
include lib/mpl_toolkits/basemap/__init__.py
include lib/mpl_toolkits/basemap/proj.py
include lib/mpl_toolkits/basemap/pyproj.py
include lib/mpl_toolkits/basemap/shapefile.py
include lib/mpl_toolkits/basemap/cm.py
include lib/mpl_toolkits/basemap/solar.py
include lib/mpl_toolkits/basemap/test.py
include opt_lib/mpl_toolkits/basemap/shapefile.py
include doc/users/figures/*py
include doc/users/*rst
include doc/api/*rst
Expand Down
8 changes: 7 additions & 1 deletion lib/mpl_toolkits/basemap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2128,7 +2128,13 @@ def readshapefile(self,shapefile,name,drawbounds=True,zorder=None,
matplotlib.patches.LineCollection object is appended to the tuple.
"""
import shapefile as shp
from .shapefile import Reader
try:
# try system version first
from shapefile import Reader
except ImportError:
# try bundled version as fallback
from .shapefile import Reader

shp.default_encoding = default_encoding
if not os.path.exists('%s.shp'%shapefile):
raise IOError('cannot locate %s.shp'%shapefile)
Expand Down
34 changes: 34 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,29 @@ def checkversion(GEOS_dir):
geos_include_dirs=[os.path.join(GEOS_dir,'include'),inc_dirs]
geos_library_dirs=[os.path.join(GEOS_dir,'lib'),os.path.join(GEOS_dir,'lib64')]

# get location of pyshp lib from environment variable if it is set.
if 'PYSHP_DIR' in os.environ:
PYSHP_dir = os.environ.get('PYSHP_DIR')
else:
# set PYSHP_DIR to None if not defined by the user
PYSHP_dir = None

if PYSHP_dir:
if not PYSHP_dir in sys.path:
sys.path.append(PYSHP_dir)
try:
import shapefile
except ImportError:
raise SystemExit("""
Could not import shapefile!'
Please make sure the pyshp module is installed and set the PYSHP_DIR
environment variable to point to the proper location.
Alternatively, you may choose to not define the PYSHP_DIR environment
variable, in which case the bundled copy of pyshp will be used.
""")
else:
pass

# proj4 and geos extensions.
deps = glob.glob('src/*.c')
deps.remove(os.path.join('src','_proj.c'))
Expand All @@ -89,6 +112,13 @@ def checkversion(GEOS_dir):
packages = ['mpl_toolkits','mpl_toolkits.basemap']
namespace_packages = ['mpl_toolkits']
package_dirs = {'':'lib'}

if PYSHP_dir is None:
# copy shapefile into basemap dir
import shutil
shutil.copy('opt_lib/mpl_toolkits/basemap/shapefile.py',
'lib/mpl_toolkits/basemap/shapefile.py')

extensions = [Extension("mpl_toolkits.basemap._proj",deps+['src/_proj.c'],include_dirs = ['src'],)]
# can't install _geoslib in mpl_toolkits.basemap namespace,
# or Basemap objects won't be pickleable.
Expand Down Expand Up @@ -165,3 +195,7 @@ def checkversion(GEOS_dir):
cmdclass = {'build_py': build_py},
package_data = package_data
)

# clean up afterwards
if PYSHP_dir is None:
os.remove('lib/mpl_toolkits/basemap/shapefile.py')
7 changes: 6 additions & 1 deletion utils/readboundaries_shp.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import numpy as np
from mpl_toolkits.basemap.shapefile import Reader
try:
# try system version first
from shapefile import Reader
except ImportError:
# try bundled version as fallback
from mpl_toolkits.basemap.shapefile import Reader

lsd = 5

Expand Down