@@ -162,6 +162,7 @@ def get_cartopy_scale(zoom: int) -> str:
162162 else :
163163 return "110m"
164164
165+
165166def webmercator_to_lonlat (x : float , y : float ) -> Tuple [float , float ]:
166167 """
167168 Converts Web Mercator projection coordinates (meters) to lon/lat.
@@ -184,6 +185,7 @@ def webmercator_to_lonlat(x: float, y: float) -> Tuple[float, float]:
184185 lat = math .degrees (2 * math .atan (math .exp (y / EARTH_RADIUS_M )) - math .pi / 2 )
185186 return lon , lat
186187
188+
187189def plot_usgs_raster_map (
188190 lat_min : float ,
189191 lat_max : float ,
@@ -194,6 +196,9 @@ def plot_usgs_raster_map(
194196 mask_ocean : bool = False ,
195197 add_features : bool = True ,
196198 display_width_px : int = 1024 ,
199+ figsize : Tuple [int , int ] = (12 , 8 ),
200+ fig : plt .Figure = None ,
201+ ax : plt .Axes = None ,
197202) -> Tuple [plt .Figure , plt .Axes ]:
198203 """
199204 Downloads and displays a USGS raster map for the given bounding box.
@@ -242,27 +247,31 @@ def plot_usgs_raster_map(
242247 lon_min , lat_min = webmercator_to_lonlat (xmin , ymin )
243248 lon_max , lat_max = webmercator_to_lonlat (xmax , ymax )
244249
245- print (f"Converted bounds to degrees: lon_min={ lon_min } , lon_max={ lon_max } , lat_min={ lat_min } , lat_max={ lat_max } " )
250+ scale = get_cartopy_scale (zoom )
251+ if verbose :
252+ print (f"Using Cartopy scale: { scale } " )
246253
247254 crs_tiles = ccrs .PlateCarree ()
248- fig = plt .figure (figsize = (12 , 8 ))
249- ax = plt .axes (projection = crs_tiles )
255+ if fig is None or ax is None :
256+ fig = plt .figure (figsize = figsize )
257+ ax = plt .axes (projection = crs_tiles )
258+
250259 ax .set_extent ([lon_min , lon_max , lat_min , lat_max ], crs = crs_tiles )
251260
252261 ax .imshow (
253- map_img , origin = "upper" , extent = [xmin , xmax , ymin , ymax ], transform = ccrs .Mercator .GOOGLE
262+ map_img ,
263+ origin = "upper" ,
264+ extent = [xmin , xmax , ymin , ymax ],
265+ transform = ccrs .Mercator .GOOGLE ,
254266 )
255- scale = get_cartopy_scale (zoom )
256- if verbose :
257- print (f"Using Cartopy scale: { scale } " )
258267
259268 if add_features :
260269 ax .add_feature (cfeature .BORDERS .with_scale (scale ), linewidth = 0.8 )
261- ax .add_feature (cfeature .COASTLINE .with_scale (scale ))
270+ ax .add_feature (cfeature .COASTLINE .with_scale (scale ), zorder = 1 )
262271 ax .add_feature (cfeature .STATES .with_scale (scale ), linewidth = 0.5 )
263272
264273 if mask_ocean :
265- ax .add_feature (cfeature .OCEAN .with_scale (scale ), facecolor = "w" , zorder = 3 )
274+ ax .add_feature (cfeature .OCEAN .with_scale (scale ), facecolor = "w" , zorder = 1 )
266275
267276 return fig , ax
268277
0 commit comments