tilecache
darts_utils.tilecache
¶
Caching functionality for xarray datasets.
manager
module-attribute
¶
manager = darts_utils.bands.BandManager(
{
"blue": darts_utils.bands.BandCodec.optical(),
"red": darts_utils.bands.BandCodec.optical(),
"green": darts_utils.bands.BandCodec.optical(),
"nir": darts_utils.bands.BandCodec.optical(),
"s2_scl": darts_utils.bands.BandCodec.mask(11),
"planet_udm": darts_utils.bands.BandCodec.mask(8),
"quality_data_mask": darts_utils.bands.BandCodec.mask(
2
),
"dem": darts_utils.bands.BandCodec(
disk_dtype="float32",
memory_dtype="float32",
valid_range=(-100, 3000),
scale_factor=0.1,
offset=-100.0,
fill_value=-1,
),
"arcticdem_data_mask": darts_utils.bands.BandCodec(
disk_dtype="bool",
memory_dtype="uint8",
valid_range=(0, 1),
),
"tc_brightness": darts_utils.bands.BandCodec.tc(),
"tc_greenness": darts_utils.bands.BandCodec.tc(),
"tc_wetness": darts_utils.bands.BandCodec.tc(),
"ndvi": darts_utils.bands.BandCodec.ndi(),
"relative_elevation": darts_utils.bands.BandCodec(
disk_dtype="int16",
memory_dtype="float32",
valid_range=(-50, 50),
scale_factor=100 / 30000,
offset=-50.0,
fill_value=-1,
),
"slope": darts_utils.bands.BandCodec(
disk_dtype="int16",
memory_dtype="float32",
valid_range=(0, 90),
scale_factor=1 / 100,
offset=0.0,
fill_value=-1,
),
"aspect": darts_utils.bands.BandCodec(
disk_dtype="int16",
memory_dtype="float32",
valid_range=(0, 360),
scale_factor=1 / 10,
offset=0.0,
fill_value=-1,
),
"hillshade": darts_utils.bands.BandCodec(
disk_dtype="int16",
memory_dtype="float32",
valid_range=(0, 1),
scale_factor=1 / 10000,
offset=0.0,
fill_value=-1,
),
"curvature": darts_utils.bands.BandCodec.ndi(),
"probabilities": darts_utils.bands.BandCodec.percentage(),
"probabilities-*": darts_utils.bands.BandCodec.percentage(),
"binarized_segmentation": darts_utils.bands.BandCodec.bool(),
"binarized_segmentation-*": darts_utils.bands.BandCodec.bool(),
"extent": darts_utils.bands.BandCodec.bool(),
}
)
XarrayCacheManager
¶
Manager for caching xarray datasets.
Example
def process_tile(tile_id: str):
# Initialize cache manager
preprocess_cache = Path("preprocess_cache")
cache_manager = XarrayCacheManager(preprocess_cache)
def create_tile():
# Your existing tile creation logic goes here
return create_tile(...) # Replace with actual implementation
# Get cached tile or create and cache it
tile = cache_manager.get_or_create(
identifier=tile_id,
creation_func=create_tile
)
return tile
Initialize the cache manager.
Parameters:
Source code in darts-utils/src/darts_utils/tilecache.py
cache_dir
instance-attribute
¶
cache_dir = (
pathlib.Path(
darts_utils.tilecache.XarrayCacheManager(cache_dir)
)
if isinstance(
darts_utils.tilecache.XarrayCacheManager(cache_dir),
str,
)
else darts_utils.tilecache.XarrayCacheManager(cache_dir)
)
exists
¶
Check if a cached Dataset exists.
Parameters:
-
identifier
(str
) –Unique identifier for the cached file
Returns:
-
bool
(bool
) –True if the Dataset exists in cache, False otherwise
Source code in darts-utils/src/darts_utils/tilecache.py
get_or_create
¶
get_or_create(
identifier: str,
creation_func: callable,
force: bool,
use_band_manager: bool = True,
*args: tuple[typing.Any, ...],
**kwargs: dict[str, typing.Any],
) -> xarray.Dataset
Get cached Dataset or create and cache it if it doesn't exist.
Parameters:
-
identifier
(str
) –Unique identifier for the cached file
-
creation_func
(callable
) –Function to create the Dataset if not cached
-
force
(bool
) –If True, forces reprocessing even if cached
-
use_band_manager
(bool
, default:True
) –If True, uses the band manager save and load the data. Defaults to True.
-
*args
(tuple[typing.Any, ...]
, default:()
) –Arguments to pass to creation_func
-
**kwargs
(dict[str, typing.Any]
, default:{}
) –Keyword arguments to pass to creation_func
Returns:
Source code in darts-utils/src/darts_utils/tilecache.py
load_from_cache
¶
Load a Dataset from cache if it exists.
Parameters:
-
identifier
(str
) –Unique identifier for the cached file
-
use_band_manager
(bool
, default:True
) –If True, uses the band manager to load the data. Defaults to True.
Returns:
Source code in darts-utils/src/darts_utils/tilecache.py
save_to_cache
¶
Save a Dataset to cache.
Parameters:
-
dataset
(xarray.Dataset
) –Dataset to cache
-
identifier
(str
) –Unique identifier for the cached file
-
use_band_manager
(bool
, default:True
) –If True, uses the band manager to save the data. Defaults to True.
Returns:
-
bool
(bool
) –Success of operation