1- import warnings
21from pathlib import Path
32
43import matplotlib .pyplot as plt
@@ -37,12 +36,13 @@ def _get_environment_coordinates(self):
3736 if not hasattr (self .monte_carlo , "environment" ):
3837 raise ValueError (
3938 "MonteCarlo object must have an 'environment' attribute "
40- "to use automatic map background."
39+ "for automatically fetching the background map ."
4140 )
4241 env = self .monte_carlo .environment
4342 if not hasattr (env , "latitude" ) or not hasattr (env , "longitude" ):
4443 raise ValueError (
45- "Environment must have 'latitude' and 'longitude' attributes."
44+ "Environment must have 'latitude' and 'longitude' attributes "
45+ "for automatically fetching the background map."
4646 )
4747
4848 # Handle both StochasticEnvironment (which stores as lists) and
@@ -97,8 +97,13 @@ def _resolve_map_provider(self, background, contextily):
9797 for key in map_provider .split ("." ):
9898 p = p [key ]
9999 source_provider = p
100- except (KeyError , AttributeError ):
101- pass
100+ except (KeyError , AttributeError ) as e :
101+ raise ValueError (
102+ f"Invalid map provider '{ background } '. "
103+ f"The provider '{ map_provider } ' could not be found in contextily.providers. "
104+ f"Please check the provider name or use one of the built-in options: "
105+ f"'satellite', 'street', or 'terrain'."
106+ ) from e
102107
103108 return source_provider
104109
@@ -125,45 +130,46 @@ def _get_background_map(self, background, xlim, ylim):
125130 Image as a 3D array of RGB values
126131 extent : tuple
127132 Bounding box [minX, maxX, minY, maxY] of the returned image
133+
134+ Raises
135+ ------
136+ ImportError
137+ If the contextily library is not installed.
138+ RuntimeError
139+ If unable to fetch the background map from the provider.
128140 """
129141 if background is None :
130142 return None , None
131143
132- try :
133- contextily = import_optional_dependency ("contextily" )
134- except ImportError :
135- warnings .warn (
136- "contextily library is required for automatic map background. "
137- "Install it via 'pip install contextily' or 'pip install rocketpy[monte-carlo]'. "
138- "Plotting without background." ,
139- UserWarning ,
140- )
141- return None , None
144+ contextily = import_optional_dependency ("contextily" )
142145
143- try :
144- origin_lat , origin_lon , earth_radius = self ._get_environment_coordinates ()
145- source_provider = self ._resolve_map_provider (background , contextily )
146- local_extent = [xlim [0 ], xlim [1 ], ylim [0 ], ylim [1 ]]
147- west , south , east , north = convert_local_extent_to_wgs84 (
148- local_extent , origin_lat , origin_lon , earth_radius
149- )
146+ origin_lat , origin_lon , earth_radius = self ._get_environment_coordinates ()
147+ source_provider = self ._resolve_map_provider (background , contextily )
148+ local_extent = [xlim [0 ], xlim [1 ], ylim [0 ], ylim [1 ]]
149+ west , south , east , north = convert_local_extent_to_wgs84 (
150+ local_extent , origin_lat , origin_lon , earth_radius
151+ )
150152
153+ try :
151154 bg , mercator_extent = contextily .bounds2img (
152155 west , south , east , north , source = source_provider , ll = True
153156 )
154-
155- local_extent = convert_mercator_extent_to_local (
156- mercator_extent , origin_lat , origin_lon , earth_radius
157- )
158-
159- return bg , local_extent
160-
161157 except Exception as e :
162- warnings .warn (
163- f"Unable to fetch background map '{ background } '. "
164- f"Error: { e } . Plotting without background."
165- )
166- return None , None
158+ raise RuntimeError (
159+ f"Failed to fetch background map tiles from provider '{ background } '. "
160+ f"This could be due to:\n "
161+ f" - Network connectivity issues\n "
162+ f" - Invalid coordinate bounds (west={ west :.6f} , south={ south :.6f} , "
163+ f"east={ east :.6f} , north={ north :.6f} )\n "
164+ f" - Service unavailability\n "
165+ f" - Invalid map provider configuration\n \n "
166+ ) from e
167+
168+ local_extent = convert_mercator_extent_to_local (
169+ mercator_extent , origin_lat , origin_lon , earth_radius
170+ )
171+
172+ return bg , local_extent
167173
168174 # pylint: disable=too-many-statements
169175 def ellipses (
0 commit comments