From 4513d3aec2d825df0c270acc9b0b6363dae34503 Mon Sep 17 00:00:00 2001 From: Andrew Dawson Date: Wed, 2 Dec 2015 12:12:53 +0000 Subject: [PATCH] Improve error from setting an invalid extent. Closes #703. --- lib/cartopy/mpl/geoaxes.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/cartopy/mpl/geoaxes.py b/lib/cartopy/mpl/geoaxes.py index 182034495..ac3be49db 100644 --- a/lib/cartopy/mpl/geoaxes.py +++ b/lib/cartopy/mpl/geoaxes.py @@ -585,7 +585,18 @@ def set_extent(self, extents, crs=None): if projected is None: projected = self.projection.project_geometry(domain_in_crs, crs) - x1, y1, x2, y2 = projected.bounds + try: + # This might fail with an unhelpful error message ('need more + # than 0 values to unpack') if the specified extents fall outside + # the projection extents, so try and give a better error message. + x1, y1, x2, y2 = projected.bounds + except ValueError: + msg = ('Failed to determine the required bounds in projection ' + 'coordinates. Check that the values provided are within ' + 'the valid range (x_limits=[{xlim[0]}, {xlim[1]}], ' + 'y_limits=[{ylim[0]}, {ylim[1]}]).') + raise ValueError(msg.format(xlim=self.projection.x_limits, + ylim=self.projection.y_limits)) self.set_xlim([x1, x2]) self.set_ylim([y1, y2])