|
1 | 1 | import datetime |
2 | 2 | import os.path |
| 3 | +import warnings |
3 | 4 | from dataclasses import dataclass |
4 | 5 | from typing import Any, Callable, Dict, List, Optional, Tuple |
5 | 6 |
|
6 | 7 | import fsspec |
7 | 8 | import numpy as np |
| 9 | +import rasterio |
8 | 10 | from lxml import etree |
9 | 11 | from rasterio import Affine |
10 | 12 | from rasterio.crs import CRS |
| 13 | +from rasterio.errors import NotGeoreferencedWarning |
11 | 14 | from shapely.geometry import shape |
12 | 15 | from stactools.core.io import ReadHrefModifier |
13 | 16 | from stactools.core.io.xml import XmlElement |
@@ -252,6 +255,32 @@ def from_cog_tags(cls, cog_tags: Dict[str, str]) -> "Metadata": |
252 | 255 | collection=collection, |
253 | 256 | ) |
254 | 257 |
|
| 258 | + @classmethod |
| 259 | + def from_hdf_href( |
| 260 | + cls, href: str, read_href_modifier: Optional[ReadHrefModifier] = None |
| 261 | + ) -> "Metadata": |
| 262 | + """Reads metadata from an HDF file when XML is not available. |
| 263 | +
|
| 264 | + Args: |
| 265 | + href (str): The href of the HDF file |
| 266 | + read_href_modifier (Optional[Callable[[str], str]]): Optional |
| 267 | + function to modify the read href |
| 268 | +
|
| 269 | + Returns: |
| 270 | + Metadata: Information that will map to Item attributes. |
| 271 | + """ |
| 272 | + if read_href_modifier: |
| 273 | + read_href = read_href_modifier(href) |
| 274 | + else: |
| 275 | + read_href = href |
| 276 | + |
| 277 | + with warnings.catch_warnings(): |
| 278 | + warnings.simplefilter("ignore", category=NotGeoreferencedWarning) |
| 279 | + with rasterio.open(read_href) as dataset: |
| 280 | + hdf_tags = dataset.tags() |
| 281 | + |
| 282 | + return cls.from_cog_tags(hdf_tags) |
| 283 | + |
255 | 284 | @property |
256 | 285 | def datetime(self) -> Optional[datetime.datetime]: |
257 | 286 | """Returns a single nominal datetime for this metadata file. |
|
0 commit comments