Skip to content

Commit

Permalink
Fix issues with new projection error message
Browse files Browse the repository at this point in the history
  • Loading branch information
lukelbd committed Oct 11, 2021
1 parent a93a3a5 commit 4884006
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 44 deletions.
16 changes: 9 additions & 7 deletions proplot/constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
try:
import cartopy.crs as ccrs
import cartopy.mpl.ticker as cticker
from cartopy.crs import CRS
from cartopy.crs import Projection
except ModuleNotFoundError:
CRS = ccrs = cticker = object
Projection = ccrs = cticker = object

__all__ = [
'Proj',
Expand Down Expand Up @@ -1391,13 +1391,12 @@ def Proj(name, basemap=None, **kwargs):
.. _wintri: https://proj4.org/operations/projections/wintri.html
""" # noqa: E501
# Class instances
is_crs = CRS is not object and isinstance(name, CRS)
basemap = _not_none(basemap, rc['basemap'])
is_crs = Projection is not object and isinstance(name, Projection)
is_basemap = Basemap is not object and isinstance(name, Basemap)
use_basemap = _not_none(basemap, rc['basemap'])
if is_crs or is_basemap:
proj = name
if basemap is not None:
kwargs['basemap'] = basemap
package = 'cartopy' if is_crs else 'basemap'
if kwargs:
warnings._warn_proplot(f'Ignoring Proj() keyword arg(s): {kwargs!r}.')

Expand All @@ -1417,8 +1416,9 @@ def Proj(name, basemap=None, **kwargs):
# https://stackoverflow.com/questions/56299971/ (also triggers 'no room for axes')
# NOTE: Unlike cartopy, basemap resolution is configured
# on initialization and controls *all* features.
elif use_basemap:
elif basemap:
import mpl_toolkits.basemap as mbasemap
package = 'basemap'
if dependencies._version_mpl >= 3.3:
raise RuntimeError(
'Basemap is no longer maintained and is incompatible with '
Expand Down Expand Up @@ -1464,6 +1464,7 @@ def Proj(name, basemap=None, **kwargs):
# NOTE: Error message matches basemap invalid projection message
else:
import cartopy.crs as ccrs # noqa: F401
package = 'cartopy'
kwproj = {
PROJ_ALIASES_KW.get(key, key): value
for key, value in kwargs.items()
Expand All @@ -1489,6 +1490,7 @@ def Proj(name, basemap=None, **kwargs):
)
proj = crs(**kwproj)

proj._proj_package = package
return proj


Expand Down
57 changes: 20 additions & 37 deletions proplot/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,30 +400,6 @@
docstring._snippet_manager['figure.save'] = _save_docstring


def _get_proj_package(basemap=None):
"""
Try to get the projection package.
"""
use_basemap = _not_none(basemap, rc['basemap'])
if use_basemap:
package = 'basemap'
try:
import mpl_toolkits.basemap # noqa: F401
except ImportError:
installed = False
else:
installed = True
else:
package = 'cartopy'
try:
import cartopy # noqa: F401
except ImportError:
installed = False
else:
installed = True
return package, installed


def _get_journal_size(preset):
"""
Return the width and height corresponding to the given preset.
Expand Down Expand Up @@ -796,28 +772,35 @@ def _parse_proj(
raise ValueError('Matplotlib axes cannot be added to proplot figures.')

# Search axes projections
subclass = None
name = None
if isinstance(proj, str):
try:
mproj.get_projection_class('proplot_' + proj)
except (KeyError, ValueError):
pass
else:
subclass = proj

# Redirect to basemap or cartopy projection
if subclass is None:
subclass, installed = _get_proj_package(basemap=basemap)
if not installed:
raise ValueError(
f'Invalid projection name {proj!r}. Valid axes subclasses are '
+ ', '.join(map(repr, paxes.CLASSES)) + '. If you are trying to '
+ f'create a geographic axes then {subclass} must be installed.'
)
name = proj
# Helpful error message
if (
name is None
and basemap is None
and isinstance(proj, str)
and constructor.Projection is object
and constructor.Basemap is object
):
raise ValueError(
f'Invalid projection name {proj!r}. Valid axes subclasses are '
+ ', '.join(map(repr, paxes.CLASSES)) + '. If you are trying to '
+ 'create a geographic axes then cartopy or basemap must be installed.'
)
# Search geographic projections
# NOTE: Also raises errors due to unexpected projection type
if name is None:
proj = constructor.Proj(proj, basemap=basemap, **proj_kw)
name = proj._proj_package
kwargs['map_projection'] = proj

kwargs['projection'] = 'proplot_' + subclass
kwargs['projection'] = 'proplot_' + name
return kwargs

def _get_align_axes(self, side):
Expand Down

0 comments on commit 4884006

Please sign in to comment.