Download Utils Documentation
Documentation for the terrakit.download.geodata_utils module.
terrakit.download.geodata_utils
map_netcdf_variables_to_requested_bands(netcdf_files: list, requested_bands: list) -> Dict[str, str]
Create a mapping from NetCDF variable names to requested band names.
Data providers often return abbreviated variable names (e.g., "t2m" for "2m_temperature"). This function intelligently matches NetCDF variable names to the requested band names using multiple strategies: attribute matching, substring matching, and pattern matching.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
netcdf_files
|
list
|
List of paths to NetCDF files |
required |
requested_bands
|
list
|
List of requested band/variable names |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, str]
|
Dictionary mapping NetCDF variable names to requested band names |
Example
netcdf_files = ["data.nc"] requested_bands = ["2m_temperature", "mean_total_precipitation_rate"] mapping = map_netcdf_variables_to_requested_bands(netcdf_files, requested_bands) print(mapping)
Source code in terrakit/download/geodata_utils.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | |
list_data_connectors(as_json: bool = False) -> Union[list, Dict[str, Any], Any]
List available data connectors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
as_json
|
bool
|
If True, return data connectors as a JSON object, otherwise return a list of connector names. |
False
|
Returns:
| Type | Description |
|---|---|
Union[list, Dict[str, Any], Any]
|
Union[list, Dict[str, Any], Any]: List of connector names or JSON object containing all connector specifications. |
Source code in terrakit/download/geodata_utils.py
load_and_list_collections(as_json: bool = False, connector_type: Union[str, None] = None) -> Union[list, Dict[str, Any]]
Load and list collections for a given data connector type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
as_json
|
bool
|
If True, return collection details as a JSON object, otherwise return a list of collection names. |
False
|
connector_type
|
str
|
The type of data connector to filter collections by. |
None
|
Returns:
| Type | Description |
|---|---|
Union[list, Dict[str, Any]]
|
Union[list, Dict[str, Any]]: List of collection names or JSON object containing collection specifications. |
Source code in terrakit/download/geodata_utils.py
check_bands(connector_type: str, collection_name: str, bands: list)
Check if the specified bands are available for a given collection and connector type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connector_type
|
str
|
The type of data connector. |
required |
collection_name
|
str
|
The name of the collection. |
required |
bands
|
list
|
List of band names to check. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
Modified list of bands with any unavailable bands replaced by their alternative names if possible. |
Source code in terrakit/download/geodata_utils.py
polygon_to_bbox(polygon, buffer_size)
Convert a Shapely Polygon to a bounding box with a buffer zone.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
polygon
|
Polygon
|
The input polygon. |
required |
buffer_size
|
float
|
The size of the buffer zone in the same units as the polygon's CRS. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
float
|
Bounding box [min_lon, min_lat, max_lon, max_lat] with buffer zone. |
Source code in terrakit/download/geodata_utils.py
calculate_resolution(meter_resolution, lat)
Calculate the spatial resolution in latitude and longitude for a given meter resolution at a specific latitude.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
meter_resolution
|
float
|
The desired resolution in meters. |
required |
lat
|
float
|
The latitude for which to calculate the resolution. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
(float, float)
|
Resolution in latitude and longitude. |
Source code in terrakit/download/geodata_utils.py
verify_input_image(image, standard_dimensions=224) -> typing.Tuple[int, str]
Verify input dimensions for supplied image
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
bytes
|
image |
required |
standard_dimensions
|
int
|
expected size of image |
224
|
Return
tuple[int, str]: [verification_status_code, verification_msg]
Source code in terrakit/download/geodata_utils.py
check_projection(file)
Check the projection is correct, if not reproject to EPSG:4326
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
str
|
The path to the input file. |
required |
Returns:
| Type | Description |
|---|---|
|
None |
Source code in terrakit/download/geodata_utils.py
pad_bbox(padding_degrees, bbox)
Add padding to bounding box to help with edge artifacts. Args: padding_degrees (float): number of degrees to add as border to bbox bbox (list(float)): original bounding box [min_lon, min_lat, max_lon, max_lat] Return: padded_bbox (list(float)): bouning box with border of padding [min_lon, min_lat, max_lon, max_lat]
Source code in terrakit/download/geodata_utils.py
tile_bbox(aoi_size, bbox, resolution, tile_size_x=2200.0, tile_size_y=2200.0)
Tile a bounding box if it exceeds 2400 pixels in any dimension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
aoi_size
|
tuple(float)
|
The size of the area of interest [width, height]. |
required |
bbox
|
list(float)
|
The original bounding box [min_lon, min_lat, max_lon, max_lat]. |
required |
resolution
|
float
|
The spatial resolution. |
required |
tile_size_x
|
float
|
The desired width of each tile. |
2200.0
|
tile_size_y
|
float
|
The desired height of each tile. |
2200.0
|
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
A tuple containing lists of tiled bounding boxes and their respective sizes. |
Source code in terrakit/download/geodata_utils.py
check_and_crop_bbox(bbox, resolution)
Check and crop a bounding box to ensure it fits within Sentinel Hub's processing limits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bbox
|
list(float)
|
The original bounding box [min_lon, min_lat, max_lon, max_lat]. |
required |
resolution
|
float
|
The spatial resolution. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
A tuple containing the cropped bounding box and its size. |
Source code in terrakit/download/geodata_utils.py
save_data_array_to_file(da, save_file, imputed=False) -> None
Save an xarray DataArray to a GeoTIFF file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
da
|
DataArray
|
The input DataArray. |
required |
save_file
|
str
|
The path to save the DataArray. |
required |
imputed
|
bool
|
Whether the DataArray has been imputed. |
False
|
Source code in terrakit/download/geodata_utils.py
save_cog(ds, filename='cogeo.tif') -> None
Save an xarray Dataset as a Cloud Optimized GeoTIFF with band descriptions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ds
|
Dataset
|
The input Dataset. |
required |
filename
|
str
|
The path and filename for the output GeoTIFF. |
'cogeo.tif'
|
Source code in terrakit/download/geodata_utils.py
save_data_array_as_netcdf(da: xr.DataArray, save_file: str | bool, **kwargs) -> None
Save an xarray DataArray as a NetCDF file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
da
|
DataArray
|
The input DataArray. |
required |
save_file
|
str
|
The path to save the DataArray. |
required |
**kwargs
|
Additional keyword arguments for the to_netcdf method. |
{}
|
Source code in terrakit/download/geodata_utils.py
validate_input_params(bbox: tuple | None, date_start: str | None, date_end: str | None) -> None
Validate input parameters for bounding box and date range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bbox
|
tuple | None
|
The bounding box as a tuple (min_lon, min_lat, max_lon, max_lat). |
required |
date_start
|
str | None
|
The start date in 'YYYY-MM-DD' format. |
required |
date_end
|
str | None
|
The end date in 'YYYY-MM-DD' format. |
required |