Spaces:
Sleeping
Sleeping
Update ash_animator/basemaps.py
Browse files- ash_animator/basemaps.py +14 -29
ash_animator/basemaps.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import hashlib
|
| 3 |
from PIL import Image
|
| 4 |
import matplotlib.pyplot as plt
|
|
@@ -9,46 +10,30 @@ import cartopy.feature as cfeature
|
|
| 9 |
|
| 10 |
def get_cache_dir(app_name):
|
| 11 |
"""
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
Windows: uses LOCALAPPDATA/<app>_cache
|
| 15 |
"""
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
os.makedirs(test_path, exist_ok=True)
|
| 25 |
-
os.chmod(test_path, 0o777)
|
| 26 |
-
return test_path
|
| 27 |
-
except PermissionError:
|
| 28 |
-
print(f"[PermissionError] Cannot use {base_dir}, falling back to /tmp.")
|
| 29 |
-
base_dir = "/tmp"
|
| 30 |
-
|
| 31 |
-
cache_path = os.path.join(base_dir, f"{app_name}_cache")
|
| 32 |
-
os.makedirs(cache_path, exist_ok=True)
|
| 33 |
-
return cache_path
|
| 34 |
|
| 35 |
-
#
|
| 36 |
CTX_TILE_CACHE_DIR = get_cache_dir("contextily")
|
| 37 |
BASEMAP_TILE_CACHE_DIR = get_cache_dir("basemap")
|
| 38 |
CARTOPY_CACHE_DIR = get_cache_dir("cartopy")
|
| 39 |
|
| 40 |
-
# Set
|
| 41 |
os.environ["CARTOPY_USER_BACKGROUNDS"] = CARTOPY_CACHE_DIR
|
| 42 |
os.environ["CARTOPY_CACHE_DIR"] = CARTOPY_CACHE_DIR
|
| 43 |
|
| 44 |
def draw_etopo_basemap(ax, mode="basemap", zoom=11):
|
| 45 |
"""
|
| 46 |
-
Draws a basemap
|
| 47 |
-
Parameters
|
| 48 |
-
----------
|
| 49 |
-
ax : Cartopy GeoAxes
|
| 50 |
-
mode : 'stock' | 'contextily' | 'basemap'
|
| 51 |
-
zoom : int (contextily zoom level)
|
| 52 |
"""
|
| 53 |
try:
|
| 54 |
if mode == "stock":
|
|
|
|
| 1 |
import os
|
| 2 |
+
import tempfile
|
| 3 |
import hashlib
|
| 4 |
from PIL import Image
|
| 5 |
import matplotlib.pyplot as plt
|
|
|
|
| 10 |
|
| 11 |
def get_cache_dir(app_name):
|
| 12 |
"""
|
| 13 |
+
Try to create a cache directory under ./code first.
|
| 14 |
+
If it fails (e.g., due to permissions), fallback to system temp directory.
|
|
|
|
| 15 |
"""
|
| 16 |
+
try:
|
| 17 |
+
preferred = os.path.join("/code", f"{app_name}_cache")
|
| 18 |
+
os.makedirs(preferred, exist_ok=True)
|
| 19 |
+
return preferred
|
| 20 |
+
except PermissionError:
|
| 21 |
+
fallback = os.path.join(tempfile.gettempdir(), f"{app_name}_cache")
|
| 22 |
+
os.makedirs(fallback, exist_ok=True)
|
| 23 |
+
return fallback
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# Set environment variables for Cartopy
|
| 31 |
os.environ["CARTOPY_USER_BACKGROUNDS"] = CARTOPY_CACHE_DIR
|
| 32 |
os.environ["CARTOPY_CACHE_DIR"] = CARTOPY_CACHE_DIR
|
| 33 |
|
| 34 |
def draw_etopo_basemap(ax, mode="basemap", zoom=11):
|
| 35 |
"""
|
| 36 |
+
Draws a background basemap image on a Cartopy GeoAxes.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
"""
|
| 38 |
try:
|
| 39 |
if mode == "stock":
|