Documentation for Geospatial Utils
Documentation for the terrakit.general_utils.geospatial_util module.
terrakit.general_utils.geospatial_util
get_credentials_by_bucket
get the credentials to access the specified bucket. This method maps the bucket to a cos instance, then it gets the credentials to access this instance
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bucket
|
str
|
input bucket name |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Optional[str]]
|
dict[str, str]: a dict that contains endpoint, access_key_id, secret_access_key, region, endpoint |
Source code in terrakit/general_utils/geospatial_util.py
parse_region
extract region from endpoint
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
endpoint
|
str
|
e.g., s3.us-minimum_latitude.cloud-object-storage.appdomain.cloud |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
region, e.g., us-minimum_latitude |
Source code in terrakit/general_utils/geospatial_util.py
get_xarray_coord
Retrieves the coordinate name associated with the given dimension from an xarray DataArray.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataArray
|
The input xarray DataArray. |
required |
dimension
|
str
|
The dimension to search for in the coordinates. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
str | None: The name of the coordinate associated with the given dimension, or None if not found. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the specified dimension is not found in any of the coordinates. |
Source code in terrakit/general_utils/geospatial_util.py
clip_box
Clips an xarray DataArray to a bounding box.
This function clips an xarray DataArray to a user-defined bounding box. It handles both linear and curvilinear coordinate systems.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataArray
|
The input xarray DataArray to be clipped. |
required |
bbox
|
Tuple[float, float, float, float]
|
A tuple containing the bounding box coordinates (minx, miny, maxx, maxy). |
required |
x_dim
|
str
|
The name of the x-coordinate dimension. |
required |
y_dim
|
str
|
The name of the y-coordinate dimension. |
required |
crs
|
Optional[int]
|
The CRS (Coordinate Reference System) of the input data. Default is EPSG:4326. |
4326
|
Returns:
| Type | Description |
|---|---|
DataArray
|
xr.DataArray: The clipped xarray DataArray. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the bounding box coordinates are invalid (minx > maxx or miny > maxy). |
OneDimensionalRaster
|
If the resulting DataArray has either x or y dimension of size 1. |
TypeError
|
If the coordinates have more than one dimension and share a name with one of their dimensions. |
Source code in terrakit/general_utils/geospatial_util.py
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 | |
rename_vars
Rename DEFAULT_TIME_DIMENSION to "temp"
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Dataset
|
data set to check variables |
required |
Returns:
| Type | Description |
|---|---|
Dataset
|
xr.Dataset: Returns the updated dataset |
Source code in terrakit/general_utils/geospatial_util.py
expand_time_dimension
Expands the time dimension in the given xarray Dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Dataset
|
The input xarray Dataset. |
required |
time_dim
|
str | None
|
The name of the time dimension to expand. If None, no expansion is performed. |
required |
dt
|
str | None
|
A string representing a date-time in the format 'YYYY-MM-DD HH:MM:SS'. If provided, the time dimension is expanded with this date-time. |
required |
Returns:
| Type | Description |
|---|---|
Dataset
|
xr.Dataset: The xarray Dataset with the time dimension expanded. |
Source code in terrakit/general_utils/geospatial_util.py
create_missing_coords
Create a new coordinate to be attached to an existing dimension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Dataset
|
Dataset |
required |
time_dim
|
str
|
time dimension |
required |
Returns:
| Type | Description |
|---|---|
Dataset
|
xr.Dataset: Dataset |
Source code in terrakit/general_utils/geospatial_util.py
rename_dimensions
Renames dimensions in an xarray Dataset.
This function renames the dimensions of an xarray Dataset based on the provided parameters. If a dimension name is provided and it exists in the Dataset, it will be renamed to the corresponding default dimension name. If no dimension name is provided or it matches the default dimension name, the dimension remains unchanged.
The function returns the modified Dataset with the renamed dimensions. If any of the provided dimension names do not exist in the input Dataset, a ValueError is raised.
The default dimension names are defined as constants: - DEFAULT_X_DIMENSION - DEFAULT_TIME_DIMENSION - DEFAULT_Y_DIMENSION
These constants should be defined elsewhere in the codebase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Dataset
|
The input xarray Dataset to rename dimensions in. |
required |
x_dim
|
str
|
The current name of the x-dimension. Defaults to DEFAULT_X_DIMENSION. |
DEFAULT_X_DIMENSION
|
time_dim
|
str
|
The current name of the time dimension. Defaults to DEFAULT_TIME_DIMENSION. |
DEFAULT_TIME_DIMENSION
|
y_dim
|
str
|
The current name of the y-dimension. Defaults to DEFAULT_Y_DIMENSION. |
DEFAULT_Y_DIMENSION
|
Returns:
| Type | Description |
|---|---|
Dataset
|
xr.Dataset: The xarray Dataset with renamed dimensions. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If any of the provided dimension names do not exist in the input Dataset. |
Source code in terrakit/general_utils/geospatial_util.py
filter_by_time
Filter data by timestamp
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataArray
|
datacube |
required |
temporal_extent
|
Tuple[datetime, datetime]
|
start and end datetime |
required |
temporal_dim
|
str
|
name of the temporal dimension |
required |
Returns:
| Type | Description |
|---|---|
DataArray
|
xr.DataArray: datacube |
Source code in terrakit/general_utils/geospatial_util.py
remove_repeated_time_coords
Squeeze duplicate timestamps into unique timestamps.
This function keeps the time dimension but merges duplicate timestamps by backward filling nan values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_array
|
DataArray
|
data array |
required |
time_dim
|
str
|
time dimension |
DEFAULT_TIME_DIMENSION
|
Returns:
| Type | Description |
|---|---|
DataArray
|
xr.DataArray: data array |
Source code in terrakit/general_utils/geospatial_util.py
reproject_bbox
reproject bounding box to specified dst_crs
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bbox
|
Tuple[float, float, float, float]
|
minimum_longitude, minimum_latitude, maximum_longitude, maximum_latitude |
required |
dst_crs
|
Union[int, str]
|
destination CRS |
required |
src_crs
|
Union[int, str]
|
source CRS. Defaults to 4326. |
4326
|
Returns:
| Type | Description |
|---|---|
Tuple[float, float, float, float]
|
Tuple[float, float, float, float]: reprojected bbox |
Source code in terrakit/general_utils/geospatial_util.py
convert_bbox_to_polygon
Converts a bounding box to a Shapely Polygon.
This function takes a bounding box represented as a tuple of four floats (min_longitude, min_latitude, max_longitude, max_latitude) and converts it into a Shapely Polygon.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bbox
|
Tuple[float, float, float, float]
|
A tuple containing the minimum and maximum longitude and latitude defining the bounding box. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Polygon |
Polygon
|
A Shapely Polygon object representing the bounding box. |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If the created Polygon is not valid. |
Source code in terrakit/general_utils/geospatial_util.py
to_geojson
convert shapely Polygon to either dict or str
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
geom
|
Polygon
|
geometry |
required |
output_format
|
str
|
description. Defaults to "dict". |
'dict'
|
Returns:
| Type | Description |
|---|---|
Union[dict, str]
|
Union[dict, str]: geojson |
Source code in terrakit/general_utils/geospatial_util.py
from_bbox_to_polygon
generates a polygon from a bounding box
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bbox
|
Tuple[float, float, float, float]
|
right, bottom, left, top |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Polygon |
Polygon
|
description |
Source code in terrakit/general_utils/geospatial_util.py
remove_invalid_characters
environment variables must have alpha-numeric characters and underscore. This function remove what is invalid
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
name of the bucket or instance |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
core part of env var |
Source code in terrakit/general_utils/geospatial_util.py
extract_date_from_filename
Extract a date from filename. Supports: YYYYDDD (7), YYYYMMDD (8), YYMMDD (6 -> 20YYMMDD).