Mahmudm commited on
Commit
ffac2c6
·
verified ·
1 Parent(s): a2b3240

Update ash_animator/basemaps.py

Browse files
Files changed (1) hide show
  1. ash_animator/basemaps.py +11 -11
ash_animator/basemaps.py CHANGED
@@ -5,14 +5,15 @@ import contextily as ctx
5
  from mpl_toolkits.basemap import Basemap
6
  import cartopy.crs as ccrs
7
  import cartopy.feature as cfeature
 
8
  from PIL import Image
9
  import matplotlib.pyplot as plt
10
 
11
- # Determine platform and fallback cache path
12
  def get_cache_dir(app_name):
13
- if os.name == 'nt':
14
  return os.path.join(os.getenv('LOCALAPPDATA', tempfile.gettempdir()), f"{app_name}_cache")
15
- elif os.name == 'posix':
16
  home_dir = os.path.expanduser("~")
17
  if os.path.isdir(home_dir) and os.access(home_dir, os.W_OK):
18
  return os.path.join(home_dir, f".{app_name}_cache")
@@ -21,13 +22,18 @@ def get_cache_dir(app_name):
21
  else:
22
  return os.path.join(tempfile.gettempdir(), f"{app_name}_cache")
23
 
24
- # Define cache directories
25
  CTX_TILE_CACHE_DIR = get_cache_dir("contextily")
26
  BASEMAP_TILE_CACHE_DIR = get_cache_dir("basemap")
 
27
 
28
  os.environ["XDG_CACHE_HOME"] = CTX_TILE_CACHE_DIR
 
 
 
29
  os.makedirs(CTX_TILE_CACHE_DIR, exist_ok=True)
30
  os.makedirs(BASEMAP_TILE_CACHE_DIR, exist_ok=True)
 
31
 
32
  def draw_etopo_basemap(ax, mode="basemap", zoom=11):
33
  """
@@ -46,13 +52,7 @@ def draw_etopo_basemap(ax, mode="basemap", zoom=11):
46
  Default is "basemap".
47
 
48
  zoom : int, optional
49
- Tile zoom level (only for "contextily"). Higher = more detail. Default is 7.
50
-
51
- Notes
52
- -----
53
- - Uses high resolution for Basemap (resolution='h') and saves figure at 300 DPI.
54
- - Cached images are reused using extent-based hashing to avoid re-rendering.
55
- - Basemap is deprecated; Cartopy with web tiles is recommended for new projects.
56
  """
57
  try:
58
  if mode == "stock":
 
5
  from mpl_toolkits.basemap import Basemap
6
  import cartopy.crs as ccrs
7
  import cartopy.feature as cfeature
8
+ import cartopy.io.shapereader as shpreader
9
  from PIL import Image
10
  import matplotlib.pyplot as plt
11
 
12
+ # Determine platform-appropriate, writable cache directory
13
  def get_cache_dir(app_name):
14
+ if os.name == 'nt': # Windows
15
  return os.path.join(os.getenv('LOCALAPPDATA', tempfile.gettempdir()), f"{app_name}_cache")
16
+ elif os.name == 'posix': # Linux/macOS
17
  home_dir = os.path.expanduser("~")
18
  if os.path.isdir(home_dir) and os.access(home_dir, os.W_OK):
19
  return os.path.join(home_dir, f".{app_name}_cache")
 
22
  else:
23
  return os.path.join(tempfile.gettempdir(), f"{app_name}_cache")
24
 
25
+ # Define and create cache directories
26
  CTX_TILE_CACHE_DIR = get_cache_dir("contextily")
27
  BASEMAP_TILE_CACHE_DIR = get_cache_dir("basemap")
28
+ CARTOPY_CACHE_DIR = get_cache_dir("cartopy")
29
 
30
  os.environ["XDG_CACHE_HOME"] = CTX_TILE_CACHE_DIR
31
+ os.environ["CARTOPY_USER_BACKGROUNDS"] = CARTOPY_CACHE_DIR
32
+ os.environ["CARTOPY_CACHE_DIR"] = CARTOPY_CACHE_DIR
33
+
34
  os.makedirs(CTX_TILE_CACHE_DIR, exist_ok=True)
35
  os.makedirs(BASEMAP_TILE_CACHE_DIR, exist_ok=True)
36
+ os.makedirs(CARTOPY_CACHE_DIR, exist_ok=True)
37
 
38
  def draw_etopo_basemap(ax, mode="basemap", zoom=11):
39
  """
 
52
  Default is "basemap".
53
 
54
  zoom : int, optional
55
+ Tile zoom level (only for "contextily"). Higher = more detail. Default is 11.
 
 
 
 
 
 
56
  """
57
  try:
58
  if mode == "stock":