Grid Operations¶
bathy.clip(data, geometry=None, region=None)
¶
Clip a grid to a polygon boundary or region preset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataArray
|
Elevation grid. |
required |
geometry
|
GeoDataFrame, str, or Path
|
Clipping geometry. Can be a GeoDataFrame, or a path to a vector file (Shapefile, GeoPackage, GeoJSON, etc.). |
None
|
region
|
str
|
Named region preset (see :func: |
None
|
Returns:
| Type | Description |
|---|---|
DataArray
|
Clipped grid with dims normalised to bathy conventions. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If both or neither of geometry and region are specified. |
Examples:
Clip to a region preset:
Clip to a custom polygon:
>>> from shapely.geometry import box
>>> gdf = gpd.GeoDataFrame(geometry=[box(-8, 51, -7, 53)], crs="EPSG:4326")
>>> clipped = bathy.clip(data, geometry=gdf)
Source code in src/bathy/grid.py
bathy.resample(data, resolution_degrees=None, resolution_m=None, method='bilinear')
¶
Resample a grid to a new resolution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataArray
|
Elevation grid. |
required |
resolution_degrees
|
float
|
Target resolution in degrees. For projected CRS this is converted to metres. Exactly one of resolution_degrees or resolution_m must be given. |
None
|
resolution_m
|
float
|
Target resolution in metres. For geographic CRS this is converted to degrees using the grid's centre latitude. |
None
|
method
|
str
|
Resampling method. One of |
'bilinear'
|
Returns:
| Type | Description |
|---|---|
DataArray
|
Resampled grid with dims normalised to bathy conventions. |
Examples:
Resample to approximately 500 m:
Resample to 0.01°:
Source code in src/bathy/grid.py
bathy.reproject(data, target_crs, resolution=None, method='bilinear')
¶
Reproject a grid to a different coordinate reference system.
Dim names are automatically updated: lon/lat for geographic
CRS, x/y for projected CRS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataArray
|
Elevation grid. |
required |
target_crs
|
str, int, or pyproj.CRS
|
Target CRS (e.g. |
required |
resolution
|
float
|
Target resolution in the units of target_crs. If |
None
|
method
|
str
|
Resampling method (default |
'bilinear'
|
Returns:
| Type | Description |
|---|---|
DataArray
|
Reprojected grid with dim names matching bathy conventions. |
Examples:
Reproject to UTM zone 29N:
>>> utm = bathy.reproject(data, target_crs="EPSG:32629")
>>> utm.dims # ('y', 'x') — projected convention
Source code in src/bathy/grid.py
bathy.merge(datasets, method='mean', resolution=None)
¶
Merge multiple grids into a single grid.
Overlapping cells are combined using method. When grids have different resolutions they are resampled to the finest resolution (or to resolution if given) before merging.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
datasets
|
list of xr.DataArray
|
Grids to merge. Must share the same CRS. |
required |
method
|
str
|
How to combine overlapping cells: |
'mean'
|
resolution
|
float
|
Target resolution. If |
None
|
Returns:
| Type | Description |
|---|---|
DataArray
|
Merged grid with dims normalised to bathy conventions. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than two datasets are given, CRS do not match, or method is unknown. |
Examples:
Merge two adjacent tiles, averaging overlapping cells:
Merge keeping the minimum depth in overlaps:
Source code in src/bathy/grid.py
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 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | |
bathy.fill_gaps(data, method='nearest')
¶
Fill NaN gaps in a grid using interpolation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataArray
|
Elevation grid with NaN gaps. |
required |
method
|
str
|
Interpolation method: |
'nearest'
|
Returns:
| Type | Description |
|---|---|
DataArray
|
Grid with NaN values filled. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If method is not recognised. |
Examples: