Spaces:
Sleeping
Sleeping
Update ash_animator/basemaps.py
Browse files- ash_animator/basemaps.py +30 -29
ash_animator/basemaps.py
CHANGED
|
@@ -8,46 +8,47 @@ import cartopy.crs as ccrs
|
|
| 8 |
import cartopy.feature as cfeature
|
| 9 |
|
| 10 |
def get_cache_dir(app_name):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
if os.name == 'nt':
|
| 12 |
-
# Windows
|
| 13 |
-
|
| 14 |
else:
|
| 15 |
-
# Unix
|
| 16 |
base_dir = "/code"
|
| 17 |
-
cache_path = os.path.join(base_dir, f"{app_name}_cache")
|
| 18 |
try:
|
| 19 |
-
os.
|
| 20 |
-
os.
|
|
|
|
|
|
|
| 21 |
except PermissionError:
|
| 22 |
-
print(f"[PermissionError] Cannot
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
#
|
| 28 |
CTX_TILE_CACHE_DIR = get_cache_dir("contextily")
|
| 29 |
BASEMAP_TILE_CACHE_DIR = get_cache_dir("basemap")
|
| 30 |
CARTOPY_CACHE_DIR = get_cache_dir("cartopy")
|
| 31 |
|
| 32 |
-
# Set environment variables
|
| 33 |
os.environ["CARTOPY_USER_BACKGROUNDS"] = CARTOPY_CACHE_DIR
|
| 34 |
os.environ["CARTOPY_CACHE_DIR"] = CARTOPY_CACHE_DIR
|
| 35 |
|
| 36 |
-
# Ensure cache dirs exist
|
| 37 |
-
os.makedirs(CTX_TILE_CACHE_DIR, exist_ok=True)
|
| 38 |
-
os.makedirs(BASEMAP_TILE_CACHE_DIR, exist_ok=True)
|
| 39 |
-
|
| 40 |
def draw_etopo_basemap(ax, mode="basemap", zoom=11):
|
| 41 |
"""
|
| 42 |
-
Draws a
|
| 43 |
Parameters
|
| 44 |
----------
|
| 45 |
-
ax :
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
The basemap mode to use: "stock", "contextily", "basemap".
|
| 49 |
-
zoom : int, optional
|
| 50 |
-
Tile zoom level for contextily.
|
| 51 |
"""
|
| 52 |
try:
|
| 53 |
if mode == "stock":
|
|
@@ -61,7 +62,7 @@ def draw_etopo_basemap(ax, mode="basemap", zoom=11):
|
|
| 61 |
|
| 62 |
elif mode == "basemap":
|
| 63 |
extent = ax.get_extent(crs=ccrs.PlateCarree())
|
| 64 |
-
extent_str = f"{
|
| 65 |
cache_key = hashlib.md5(extent_str.encode()).hexdigest()
|
| 66 |
cache_file = os.path.join(BASEMAP_TILE_CACHE_DIR, f"{cache_key}_highres.png")
|
| 67 |
|
|
@@ -69,8 +70,8 @@ def draw_etopo_basemap(ax, mode="basemap", zoom=11):
|
|
| 69 |
img = Image.open(cache_file)
|
| 70 |
ax.imshow(img, extent=extent, transform=ccrs.PlateCarree())
|
| 71 |
else:
|
| 72 |
-
|
| 73 |
-
|
| 74 |
temp_ax.set_extent(extent, crs=ccrs.PlateCarree())
|
| 75 |
|
| 76 |
m = Basemap(projection='cyl',
|
|
@@ -79,8 +80,8 @@ def draw_etopo_basemap(ax, mode="basemap", zoom=11):
|
|
| 79 |
resolution='f', ax=temp_ax)
|
| 80 |
|
| 81 |
m.shadedrelief()
|
| 82 |
-
|
| 83 |
-
plt.close(
|
| 84 |
|
| 85 |
img = Image.open(cache_file)
|
| 86 |
ax.imshow(img, extent=extent, transform=ccrs.PlateCarree())
|
|
@@ -89,6 +90,6 @@ def draw_etopo_basemap(ax, mode="basemap", zoom=11):
|
|
| 89 |
raise ValueError(f"Unsupported basemap mode: {mode}")
|
| 90 |
|
| 91 |
except Exception as e:
|
| 92 |
-
print(f"[
|
| 93 |
ax.add_feature(cfeature.LAND)
|
| 94 |
ax.add_feature(cfeature.OCEAN)
|
|
|
|
| 8 |
import cartopy.feature as cfeature
|
| 9 |
|
| 10 |
def get_cache_dir(app_name):
|
| 11 |
+
"""
|
| 12 |
+
Returns a writable cache directory path depending on the OS.
|
| 13 |
+
Linux: tries /code/<app>_cache, falls back to /tmp/<app>_cache
|
| 14 |
+
Windows: uses LOCALAPPDATA/<app>_cache
|
| 15 |
+
"""
|
| 16 |
if os.name == 'nt':
|
| 17 |
+
# Windows
|
| 18 |
+
base_dir = os.getenv('LOCALAPPDATA', os.getcwd())
|
| 19 |
else:
|
| 20 |
+
# Unix
|
| 21 |
base_dir = "/code"
|
|
|
|
| 22 |
try:
|
| 23 |
+
test_path = os.path.join(base_dir, f"{app_name}_cache")
|
| 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 |
+
# Setup cache directories
|
| 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 Cartopy environment variables
|
| 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 onto a Cartopy GeoAxes object.
|
| 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":
|
|
|
|
| 62 |
|
| 63 |
elif mode == "basemap":
|
| 64 |
extent = ax.get_extent(crs=ccrs.PlateCarree())
|
| 65 |
+
extent_str = "_".join(f"{v:.4f}" for v in extent)
|
| 66 |
cache_key = hashlib.md5(extent_str.encode()).hexdigest()
|
| 67 |
cache_file = os.path.join(BASEMAP_TILE_CACHE_DIR, f"{cache_key}_highres.png")
|
| 68 |
|
|
|
|
| 70 |
img = Image.open(cache_file)
|
| 71 |
ax.imshow(img, extent=extent, transform=ccrs.PlateCarree())
|
| 72 |
else:
|
| 73 |
+
fig, temp_ax = plt.subplots(figsize=(12, 9),
|
| 74 |
+
subplot_kw={'projection': ccrs.PlateCarree()})
|
| 75 |
temp_ax.set_extent(extent, crs=ccrs.PlateCarree())
|
| 76 |
|
| 77 |
m = Basemap(projection='cyl',
|
|
|
|
| 80 |
resolution='f', ax=temp_ax)
|
| 81 |
|
| 82 |
m.shadedrelief()
|
| 83 |
+
fig.savefig(cache_file, dpi=300, bbox_inches='tight', pad_inches=0)
|
| 84 |
+
plt.close(fig)
|
| 85 |
|
| 86 |
img = Image.open(cache_file)
|
| 87 |
ax.imshow(img, extent=extent, transform=ccrs.PlateCarree())
|
|
|
|
| 90 |
raise ValueError(f"Unsupported basemap mode: {mode}")
|
| 91 |
|
| 92 |
except Exception as e:
|
| 93 |
+
print(f"[Basemap Error: {mode}] {e}")
|
| 94 |
ax.add_feature(cfeature.LAND)
|
| 95 |
ax.add_feature(cfeature.OCEAN)
|