Mahmudm commited on
Commit
253a100
·
verified ·
1 Parent(s): c71d790

Update ash_animator/basemaps.py

Browse files
Files changed (1) hide show
  1. ash_animator/basemaps.py +32 -9
ash_animator/basemaps.py CHANGED
@@ -1,23 +1,46 @@
1
 
2
  import os
3
  import hashlib
4
- 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
- # Define cache directories
12
- # Optional: Set tile cache directory (must be done before contextily downloads tiles)
13
- os.environ["XDG_CACHE_HOME"] = os.path.expanduser("~/.contextily_cache")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
- CTX_TILE_CACHE_DIR = os.path.expanduser("~/.contextily_cache")
16
- BASEMAP_TILE_CACHE_DIR = os.path.expanduser("~/.basemap_cache")
17
 
18
  os.makedirs(CTX_TILE_CACHE_DIR, exist_ok=True)
19
  os.makedirs(BASEMAP_TILE_CACHE_DIR, exist_ok=True)
20
 
 
 
 
 
 
 
21
  def draw_etopo_basemap(ax, mode="basemap", zoom=11):
22
  """
23
  Draws a high-resolution basemap background on the provided Cartopy GeoAxes.
 
1
 
2
  import os
3
  import hashlib
 
 
 
 
4
  from PIL import Image
5
  import matplotlib.pyplot as plt
6
 
7
+ import os
8
+ import sys
9
+
10
+ def get_cache_dir(app_name):
11
+ if os.name == 'nt':
12
+ # Windows: Use LOCALAPPDATA or fallback
13
+ return os.path.join(os.getenv('LOCALAPPDATA', os.getcwd()), f"{app_name}_cache")
14
+ else:
15
+ # Unix: Attempt to use root-level /code/app_name_cache
16
+ base_dir = "/code"
17
+ cache_path = os.path.join(base_dir, f"{app_name}_cache")
18
+ try:
19
+ os.makedirs(cache_path, exist_ok=True)
20
+ os.chmod(cache_path, 0o777) # Make it writable by all (if necessary)
21
+ except PermissionError:
22
+ print(f"[PermissionError] Cannot write to {cache_path}. Falling back to /tmp.")
23
+ cache_path = os.path.join("/tmp", f"{app_name}_cache")
24
+ os.makedirs(cache_path, exist_ok=True)
25
+ return cache_path
26
+
27
+ CTX_TILE_CACHE_DIR = get_cache_dir("contextily")
28
+ BASEMAP_TILE_CACHE_DIR = get_cache_dir("basemap")
29
+ CARTOPY_CACHE_DIR = get_cache_dir("cartopy")
30
+
31
+ os.environ["CARTOPY_USER_BACKGROUNDS"] = CARTOPY_CACHE_DIR
32
+ os.environ["CARTOPY_CACHE_DIR"] = CARTOPY_CACHE_DIR
33
 
 
 
34
 
35
  os.makedirs(CTX_TILE_CACHE_DIR, exist_ok=True)
36
  os.makedirs(BASEMAP_TILE_CACHE_DIR, exist_ok=True)
37
 
38
+
39
+ import contextily as ctx
40
+ from mpl_toolkits.basemap import Basemap
41
+ import cartopy.crs as ccrs
42
+ import cartopy.feature as cfeature
43
+
44
  def draw_etopo_basemap(ax, mode="basemap", zoom=11):
45
  """
46
  Draws a high-resolution basemap background on the provided Cartopy GeoAxes.