@@ -38,12 +38,72 @@ def s3_http_url(bucket: str, key: str) -> str:
3838_YMDHMS_RE = re .compile (r"^\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}(:\d{2})?$" )
3939
4040
41+ #Support functions to ensute the geopackage file is local
42+ def _s3_bucket_key_from_http_url (url : str ) -> Optional [tuple [str , str ]]:
43+ try :
44+ u = urllib .parse .urlparse (url )
45+ host = (u .netloc or "" ).lower ()
46+ path = (u .path or "" ).lstrip ("/" )
47+ if not host or not path :
48+ return None
49+
50+ if ".s3." in host or host .endswith (".s3.amazonaws.com" ):
51+ bucket = host .split (".s3" , 1 )[0 ]
52+ key = path
53+ return bucket , key
54+
55+ return None
56+ except Exception :
57+ return None
58+
59+
60+ def _ensure_local_gpkg (uri : str ) -> str :
61+ """
62+ Ensure we can read a GPKG even when uri is an https S3 URL by caching locally.
63+ Returns a path usable by geopandas.read_file().
64+ """
65+ if not isinstance (uri , str ) or not uri .strip ():
66+ return uri
67+
68+ u = uri .strip ()
69+ if os .path .exists (u ):
70+ return u
71+
72+ # Cache https S3 gpkg locally
73+ if u .lower ().startswith ("http" ) and ".amazonaws.com/" in u .lower () and u .lower ().endswith (".gpkg" ):
74+ parsed = _s3_bucket_key_from_http_url (u .split ("?" , 1 )[0 ])
75+ if not parsed :
76+ return u
77+
78+ bucket , key = parsed
79+ cache_dir = os .path .join (os .path .expanduser ("~" ), ".fimeval_cache" , "aoi_gpkg" )
80+ os .makedirs (cache_dir , exist_ok = True )
81+
82+ local = os .path .join (cache_dir , os .path .basename (key ))
83+ if not os .path .exists (local ):
84+ _download (bucket , key , local )
85+ return local
86+ return u
87+
4188def _normalize_user_dt (s : str ) -> str :
4289 s = s .strip ()
4390 s = s .replace ("/" , "-" )
4491 s = re .sub (r"\s+" , " " , s )
4592 return s
4693
94+ def _display_raster_name (rec : Dict [str , Any ]) -> str :
95+ tif_url = rec .get ("tif_url" )
96+ if isinstance (tif_url , str ) and tif_url .strip ():
97+ # drop querystring if any
98+ tif_url = tif_url .split ("?" , 1 )[0 ]
99+ return os .path .basename (tif_url )
100+
101+ # fallback: last path token of id
102+ rid = rec .get ("id" )
103+ if isinstance (rid , str ) and rid .strip ():
104+ return rid .strip ().split ("/" )[- 1 ] + ".tif"
105+
106+ return "NA"
47107
48108def _to_date (s : str ) -> dt .date :
49109 s = _normalize_user_dt (s )
@@ -186,7 +246,7 @@ def _return_period_text_local(r: Dict[str, Any]) -> str:
186246 tier = r .get ("tier" ) or r .get ("quality" ) or "Unknown"
187247 res = r .get ("resolution_m" )
188248 res_txt = f"{ res } m" if res is not None else "NA"
189- fname = r . get ( "file_name" ) or "NA"
249+ fname = _display_raster_name ( r )
190250
191251 # Build lines with Tier-aware event text
192252 lines = [f"Data Tier: { tier } " ]
0 commit comments