Download sunstar_plus/terrain.py from summerAIO/sunstar-plus: direct link, hf CLI and curl.
- Browser
- Download file 4.64 kB
-
https://huggingface.co/summerAIO/sunstar-plus/resolve/main/sunstar_plus/terrain.py
- Command line
-
hf download hf://summerAIO/sunstar-plus/sunstar_plus/terrain.py
-
curl -L -o terrain.py https://huggingface.co/summerAIO/sunstar-plus/resolve/main/sunstar_plus/terrain.py
4.64 kB
| """任意地点の地形・地理特徴量を計算 (stations_nationwide.csv の t_* 列と同じ定義)。 | |
| 標高: AWS Terrain Tiles (terrarium z12) / 海岸距離: global-land-mask / 土地被覆: ESA WorldCover 2021 (COG) | |
| 要: requests, pillow, scipy, global-land-mask, (任意) rasterio""" | |
| import io, math | |
| import numpy as np, requests | |
| from PIL import Image | |
| from scipy import ndimage as ndi | |
| Z = 12 | |
| def _ll2tile(lon, lat, z=Z): | |
| n = 2 ** z; return (lon + 180) / 360 * n, (1 - np.arcsinh(np.tan(np.radians(lat))) / np.pi) / 2 * n | |
| def _tile(x, y): | |
| r = requests.get(f"https://s3.amazonaws.com/elevation-tiles-prod/terrarium/{Z}/{x}/{y}.png", timeout=30) | |
| if r.status_code == 404: return np.zeros((256, 256), np.float32) | |
| r.raise_for_status() | |
| a = np.asarray(Image.open(io.BytesIO(r.content)).convert("RGB")).astype(np.float32) | |
| return a[..., 0] * 256 + a[..., 1] + a[..., 2] / 256 - 32768 | |
| def _coast(lat, lon): | |
| from global_land_mask import globe | |
| la = np.arange(lat - 2, lat + 2.0001, 0.01); lo = np.arange(lon - 2, lon + 2.0001, 0.01) | |
| LO, LA = np.meshgrid(lo, la); land = globe.is_land(LA, LO) | |
| ky = 1.11; kx = 1.11 * math.cos(math.radians(lat)) | |
| dl = ndi.distance_transform_edt(land, sampling=(ky, kx)); ds = ndi.distance_transform_edt(~land, sampling=(ky, kx)) | |
| i = int(round(2 / 0.01)); sgn = dl[i, i] if land[i, i] else -ds[i, i] | |
| yy, xx = np.mgrid[-200:201, -200:201]; dist = np.hypot(yy * ky, xx * kx) | |
| return dict(t_coast_dist_km=float(sgn), t_sea_frac_20km=float((~land)[dist <= 20].mean()), t_sea_frac_50km=float((~land)[dist <= 50].mean()), t_sea_frac_150km=float((~land)[dist <= 150].mean())) | |
| def _landcover(lat, lon): | |
| import rasterio | |
| from rasterio.windows import Window | |
| lat0 = int(math.floor(lat / 3) * 3); lon0 = int(math.floor(lon / 3) * 3) | |
| url = f"/vsicurl/https://esa-worldcover.s3.eu-central-1.amazonaws.com/v200/2021/map/ESA_WorldCover_10m_2021_v200_N{lat0:02d}E{lon0:03d}_Map.tif" | |
| with rasterio.Env(GDAL_DISABLE_READDIR_ON_OPEN="EMPTY_DIR"), rasterio.open(url) as src: | |
| row, col = src.index(lon, lat); a = src.read(1, window=Window(col - 13, row - 13, 26, 26), boundless=True, fill_value=0) | |
| return {f"t_lc_{n}_frac": float((a == c).mean()) for c, n in {10: "tree", 20: "shrub", 30: "grass", 40: "crop", 50: "built", 80: "water"}.items()} | |
| def compute_terrain(lat, lon, elevation_m, landcover=True): | |
| """returns dict of t_* features for one point. elevation_m: 観測点の標高 (AMeDAS 台帳値など)""" | |
| R_KM = 12.0; dlat = R_KM / 111.0; dlon = R_KM / (111.0 * math.cos(math.radians(lat))) | |
| xa, ya = _ll2tile(lon - dlon, lat + dlat); xb, yb = _ll2tile(lon + dlon, lat - dlat) | |
| xa, xb, ya, yb = int(xa), int(xb), int(ya), int(yb); P = 256 | |
| H = np.zeros(((yb - ya + 1) * P, (xb - xa + 1) * P), np.float32) | |
| for x in range(xa, xb + 1): | |
| for y in range(ya, yb + 1): H[(y - ya) * P:(y - ya + 1) * P, (x - xa) * P:(x - xa + 1) * P] = _tile(x, y) | |
| land = H > 0; Hf = np.where(land, H, 0.0).astype(np.float32) | |
| px = 40075016.686 * math.cos(math.radians(lat)) / (2 ** Z * 256) | |
| gy, gx = np.gradient(Hf, px); slope = np.degrees(np.arctan(np.hypot(gx, gy))); asp = np.arctan2(-gx, gy) | |
| fx, fy = _ll2tile(lon, lat); cx = int(round((fx - xa) * P)); cy = int(round((fy - ya) * P)) | |
| def bm(a, r): | |
| k = int(round(r / px)); return float(a[max(cy - k, 0):cy + k + 1, max(cx - k, 0):cx + k + 1].mean()) | |
| def box(a, r): | |
| k = int(r / px); return a[max(cy - k, 0):cy + k + 1, max(cx - k, 0):cx + k + 1] | |
| o = {} | |
| o["t_elevation_m"] = bm(Hf, 125); o["t_dem_pt_m"] = float(Hf[cy, cx]); o["t_slope_deg"] = bm(slope, 125) | |
| o["t_aspect_sin"] = bm(np.sin(asp), 125); o["t_aspect_cos"] = bm(np.cos(asp), 125) | |
| c = bm(Hf, 125) | |
| o["t_tpi_500m"] = c - bm(Hf, 500); o["t_tpi_2km"] = c - bm(Hf, 2000); o["t_tpi_10km"] = c - bm(Hf, 10000) | |
| b1 = box(Hf, 500); b5 = box(Hf, 2500) | |
| o["t_relief_1km"] = float(b1.max() - b1.min()); o["t_relief_5km"] = float(b5.max() - b5.min()) | |
| o["t_land_mask"] = float(bm(land.astype(np.float32), 125) >= 0.5) | |
| o["t_elev_mean_r10"] = bm(Hf, 2500) - elevation_m; o["t_elev_mean_r40"] = bm(Hf, 10000) - elevation_m | |
| o["t_elev_max_r10"] = float(box(Hf, 2500).max()) - elevation_m; o["t_elev_min_r40"] = float(box(Hf, 10000).min()) - elevation_m | |
| o.update(_coast(lat, lon)) | |
| if landcover: | |
| try: o.update(_landcover(lat, lon)) | |
| except Exception as e: print("WorldCover 取得失敗 (土地被覆は NaN):", repr(e)[:80]); o.update({f"t_lc_{n}_frac": np.nan for n in ("tree", "shrub", "grass", "crop", "built", "water")}) | |
| return o | |