Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

API Reference

Python API

All public functions are importable from nbs.noaabathymetry:

from nbs.noaabathymetry import fetch_tiles, mosaic_tiles, status_tiles

fetch_tiles

fetch_tiles(
    project_dir: str,
    geometry: str = None,
    data_source: str = None,
    tile_resolution_filter: list = None,
    debug: bool = False,
) -> FetchResult

Discover, download, and update NBS tiles.

Parameters

ParameterTypeDefaultDescription
project_dirstrrequiredAbsolute path to the project directory. Created if it does not exist.
geometrystr | NoneNoneGeometry input defining the area of interest. Accepts a file path, GeoJSON string, bounding box (xmin,ymin,xmax,ymax), or WKT. String inputs assume EPSG:4326. Required on the first fetch to initialize a project. Pass None on subsequent runs to skip tile discovery (useful for re-downloading existing tiles).
data_sourcestr | NoneNoneAn S3 source name (e.g. "bluetopo", "bag", "s102v30"), or None (defaults to "bluetopo").
tile_resolution_filterlist[int] | NoneNoneOnly fetch tiles at these resolutions (meters). Example: [4, 8].
debugboolFalseIf True, writes a diagnostic report to the project directory.

Returns: FetchResult

Raises

ExceptionCondition
ValueErrorproject_dir is not an absolute path.
ValueErrorgeometry path is not absolute.
ValueErrorUnknown data_source name and path is not a directory.
ValueErrorLocal directory has no tile scheme geopackage.
RuntimeErrorNo tile scheme found on S3 after retry.

Example

from nbs.noaabathymetry import fetch_tiles

result = fetch_tiles(
    '/home/user/bathymetry',
    geometry='-76.1,36.9,-75.9,37.1',
    data_source='bluetopo',
)

print(f"Tiles in AOI: {result.available_tiles_intersecting_aoi}")
print(f"Newly tracked: {result.new_tiles_tracked}")
print(f"Downloaded: {len(result.downloaded)}")
print(f"Not found on S3: {len(result.not_found)}")
print(f"Already existing: {len(result.existing)}")
for failure in result.failed:
    print(f"  Failed: {failure['tile']} - {failure['reason']}")

mosaic_tiles

mosaic_tiles(
    project_dir: str,
    data_source: str = None,
    relative_to_vrt: bool = True,
    mosaic_resolution_target: float = None,
    tile_resolution_filter: list = None,
    hillshade: bool = False,
    workers: int = None,
    reproject: bool = False,
    output_dir: str = None,
    debug: bool = False,
) -> MosaicResult

Build a per-UTM-zone mosaic from all source tiles.

Parameters

ParameterTypeDefaultDescription
project_dirstrrequiredAbsolute path to the project directory.
data_sourcestr | NoneNoneA known source name, or None (defaults to "bluetopo").
relative_to_vrtboolTrueStore referenced file paths as relative to the VRT’s directory. Set to False for absolute paths.
mosaic_resolution_targetfloat | NoneNoneForce output pixel size in meters. Must be a positive number.
tile_resolution_filterlist[int] | NoneNoneOnly include tiles at these resolutions (meters). Outputs to a separate mosaic directory.
hillshadeboolFalseIf True, generate a hillshade GeoTIFF from the elevation band.
workersint | NoneNoneNumber of parallel worker processes for building UTM zones. None or 1 = sequential. Must not exceed os.cpu_count().
reprojectboolFalseIf True, reproject to EPSG:3857 (Web Mercator) GeoTIFFs. Outputs to a separate directory.
output_dirstr | NoneNoneCustom output directory name within the project directory. Overrides the auto-generated name. Each directory can only be used by one build configuration.
debugboolFalseIf True, writes a diagnostic report to the project directory.

Returns: MosaicResult

Raises

ExceptionCondition
ValueErrorproject_dir is not an absolute path.
ValueErrorProject directory does not exist.
ValueErrorRegistry database not found (fetch_tiles must run first).
ValueErrorTile downloads folder not found (fetch_tiles must run first).
ValueErrormosaic_resolution_target is not positive.
ValueErrorworkers is not a positive integer or exceeds os.cpu_count().
ValueErrorreproject is used with a data source other than BlueTopo.
ValueErroroutput_dir contains a path separator (must be a single directory name).
RuntimeErrorGDAL version is too old for the data source.
RuntimeErrorGDAL is missing required drivers (e.g. S102, BAG).
RuntimeErrorProject was created with an incompatible internal version.
ValueErrorNo parseable tile resolutions found for a UTM zone.

Example

from nbs.noaabathymetry import mosaic_tiles

result = mosaic_tiles(
    '/home/user/bathymetry',
    data_source='bluetopo',
    mosaic_resolution_target=8,
    tile_resolution_filter=[4, 8],
)

for entry in result.built:
    print(f"Built UTM {entry['utm']}: {entry['mosaic']}")
print(f"Skipped (up to date): {len(result.skipped)}")
print(f"Missing mosaics reset: {len(result.missing_reset)}")

FetchResult

Dataclass returned by fetch_tiles.

AttributeTypeDescription
downloadedlist[str]Tile names successfully downloaded in this run.
failedlist[dict]Tiles that failed download. Each dict has tile (str) and reason (str) keys.
not_foundlist[str]Tile names whose files could not be located on S3.
existinglist[str]Tile names already downloaded, verified, and up to date.
filtered_outlist[str]Tiles excluded by the resolution filter. Empty when no filter is active.
missing_resetlist[str]Tiles previously downloaded but missing from disk.
available_tiles_intersecting_aoiintNumber of tiles with valid metadata intersecting the area of interest geometry. Includes tiles already tracked.
new_tiles_trackedintNumber of tiles actually newly added to tracking in this run. Tiles already in the database are not counted.
tile_resolution_filterlist[int] | NoneResolution filter that was active, or None if unfiltered.

Example

result = fetch_tiles('/home/user/bathymetry', geometry='aoi.gpkg')
print(result)

# FetchResult(
#     downloaded=['BlueTopo_BC25L4NW_20240315', 'BlueTopo_BC25L4NE_20240315',
#                 'BlueTopo_BC25L6SW_20240315'],
#     failed=[{'tile': 'BlueTopo_BC25L6SE_20240315',
#              'reason': 'incorrect hash for geotiff (expected=a1b2c3d4e5f6... got=9f8e7d6c5b4a...)'}],
#     not_found=['BlueTopo_BC25L8NW_20240315'],
#     existing=['BlueTopo_BC25M4NW_20240301', 'BlueTopo_BC25M4NE_20240301'],
#     filtered_out=[],
#     missing_reset=[],
#     available_tiles_intersecting_aoi=8,
#     new_tiles_tracked=6,
#     tile_resolution_filter=None
# )

MosaicResult

Dataclass returned by mosaic_tiles.

AttributeTypeDescription
builtlist[dict]UTM zones that were built. Each dict has utm (str), mosaic (str), ovr (str or None), and hillshade (str or None) keys.
skippedlist[str]UTM zone identifiers that were already up to date, or had no matching tiles after resolution filtering.
failedlist[dict]UTM zones that failed during the build. Each dict has utm (str) and reason (str) keys.
missing_resetlist[str]UTM zones reset due to mosaic files missing on disk.
hillshadeslist[dict]UTM zones where a hillshade was generated. Each dict has utm (str) and hillshade (str, absolute path) keys.
tile_resolution_filterlist[int] | NoneResolution filter that was active, or None if unfiltered.
mosaic_resolution_targetfloat | NoneOutput pixel size override that was active, or None for native resolution.

Example

result = mosaic_tiles('/home/user/bathymetry')
print(result)

# MosaicResult(
#     built=[
#         {'utm': '18', 'mosaic': '/home/user/bathymetry/BlueTopo_Mosaic/BlueTopo_Fetched_UTM18.vrt',
#          'ovr': '/home/user/bathymetry/BlueTopo_Mosaic/BlueTopo_Fetched_UTM18.vrt.ovr',
#          'hillshade': None},
#         {'utm': '19', 'mosaic': '/home/user/bathymetry/BlueTopo_Mosaic/BlueTopo_Fetched_UTM19.vrt',
#          'ovr': '/home/user/bathymetry/BlueTopo_Mosaic/BlueTopo_Fetched_UTM19.vrt.ovr',
#          'hillshade': None}
#     ],
#     skipped=['17'],
#     failed=[],
#     missing_reset=[],
#     hillshades=[],
#     tile_resolution_filter=None,
#     mosaic_resolution_target=None
# )

status_tiles

status_tiles(
    project_dir: str,
    data_source: str = None,
    verbosity: str = "normal",
) -> StatusResult

Check local project freshness against the remote tile scheme.

Parameters

ParameterTypeDefaultDescription
project_dirstrrequiredAbsolute path to the project directory.
data_sourcestr | NoneNoneA known source name, or None (defaults to "bluetopo").
verbositystr"normal"Logging verbosity: "quiet" suppresses all log output, "normal" shows UTM/resolution counts, "verbose" shows individual tiles.

Returns: StatusResult

Raises

ExceptionCondition
ValueErrorproject_dir is not an absolute path.
ValueErrorProject directory does not exist.
ValueErrorRegistry database not found (fetch_tiles must run first).
ValueErrorRate limit exceeded.
RuntimeErrorRemote tile scheme cannot be read from S3.

Example

from nbs.noaabathymetry import status_tiles

result = status_tiles('/home/user/bathymetry')

print(f"Up to date: {len(result.up_to_date)}")
print(f"Updates available: {len(result.updates_available)}")
for entry in result.updates_available:
    print(f"  {entry['tile']}: {entry['local_datetime']} -> {entry['remote_datetime']}")

StatusResult

Dataclass returned by status_tiles.

AttributeTypeDescription
up_to_datelist[dict]Tiles whose delivery datetime matches the remote and files exist on disk. Each dict has tile, utm, resolution, local_datetime, and geometry keys.
updates_availablelist[dict]Tiles with a newer delivery datetime on S3. Each dict has tile, utm, resolution, local_datetime, remote_datetime, and geometry keys.
missing_from_disklist[dict]Tiles whose delivery datetime matches the remote but files are missing from disk. Each dict has tile, utm, resolution, local_datetime, and geometry keys.
removed_from_nbslist[dict]Tiles tracked locally that no longer appear in the remote geopackage. Each dict has tile, utm, resolution, local_datetime, and geometry keys.
total_trackedintTotal number of tiles in the local database.

CLI Reference

The nbs command is installed when you pip install noaabathymetry.

nbs fetch

nbs fetch -d DIR [-g GEOMETRY] [-s SOURCE] [--tile-resolution-filter N [N ...]] [--debug] [--json]
Short formLong formDescription
-d--dir, --directoryRequired. Absolute path to the project directory.
-g--geom, --geometryGeometry input (file path, bounding box, WKT, or GeoJSON). String inputs assume EPSG:4326.
-s--source, --data-sourceData source identifier. Default: bluetopo.
--tile-resolution-filterOnly fetch tiles at these resolutions (meters). Multiple values allowed.
--debugWrite a diagnostic report to the project directory.
--jsonPrint result as JSON to stdout.
-v--versionShow version and exit.

Examples

# Discover tiles within a bounding box and download tracked tiles
nbs fetch -d /home/user/bathymetry -g "-76.1,36.9,-75.9,37.1"

# Fetch from a geopackage, only 4m and 8m tiles
nbs fetch -d /home/user/bathymetry -g /path/to/aoi.gpkg --tile-resolution-filter 4 8

# Fetch BAG data
nbs fetch -d /home/user/bathymetry -g aoi.gpkg -s bag

# Re-download/update without discovering new tiles (existing project, no geometry)
nbs fetch -d /home/user/bathymetry

nbs mosaic

nbs mosaic -d DIR [-s SOURCE] [-r BOOL] [-t RESOLUTION] [--tile-resolution-filter N [N ...]] [--hillshade] [--workers N] [--reproject] [-o OUTPUT_DIR] [--debug] [--json]
Short formLong formDescription
-d--dir, --directoryRequired. Absolute path to the project directory.
-s--source, --data-sourceData source identifier. Default: bluetopo.
-r--relative-to-vrtStore VRT file paths as relative. Default: true.
-t--mosaic-resolution-targetForce output pixel size in meters (any positive number).
--tile-resolution-filterOnly include tiles at these resolutions (meters). Multiple values allowed.
--hillshadeGenerate a hillshade GeoTIFF from the elevation band.
--workersNumber of parallel worker processes for building UTM zones.
--reprojectReproject to EPSG:3857 (Web Mercator) GeoTIFFs.
-o--output-dirCustom output directory name within the project directory.
--debugWrite a diagnostic report to the project directory.
--jsonPrint result as JSON to stdout.
-v--versionShow version and exit.

Examples

# Build mosaics from fetched BlueTopo tiles
nbs mosaic -d /home/user/bathymetry

# Build at 8m resolution target
nbs mosaic -d /home/user/bathymetry -t 8

# Build only from 4m tiles
nbs mosaic -d /home/user/bathymetry --tile-resolution-filter 4

# Build with 4 parallel workers
nbs mosaic -d /home/user/bathymetry --workers 4

# Build with hillshade generation
nbs mosaic -d /home/user/bathymetry --hillshade

# Build into a custom output directory
nbs mosaic -d /home/user/bathymetry -o my_custom_mosaics

# Build Modeling mosaics
nbs mosaic -d /home/user/bathymetry -s modeling

nbs status

nbs status -d DIR [-s SOURCE] [--verbosity quiet|normal|verbose] [--json]
Short formLong formDescription
-d--dir, --directoryRequired. Absolute path to the project directory.
-s--source, --data-sourceData source identifier. Default: bluetopo.
--verbosityLogging verbosity: quiet, normal (default), or verbose.
--jsonPrint result as JSON to stdout.

Examples

# Check for updates
nbs status -d /home/user/bathymetry

# Check with verbose tile listing
nbs status -d /home/user/bathymetry --verbosity verbose

# Quiet mode (return object only, no log output)
nbs status -d /home/user/bathymetry --verbosity quiet

# Check a different data source
nbs status -d /home/user/bathymetry -s bag