API Reference⚓︎
These are the Python docs for cog3pio, for the Rust docs, see
https://docs.rs/cog3pio.
DLPack⚓︎
cog3pio.CudaCogReader
⚓︎
Python class interface to a Cloud-optimized GeoTIFF reader (nvTIFF backend) that decodes to GPU (CUDA) memory.
Warning
This is an experimental feature only enabled on linux-x86_64 and linux-aarch64 wheel builds.
Parameters:
-
(path⚓︎str) –The path to the file, or a url to a remote file.
-
(device_id⚓︎int) –The CUDA GPU device number to decode the TIFF data on.
Returns:
-
reader(CudaCogReader) –A new CudaCogReader instance for decoding GeoTIFF files.
Raises:
-
ImportError–If
nvTIFFis not installed. Please install it (e.g. viaapt install nvtiff-cuda-13ordnf install nvtiff-cuda-13) before using this class.
Examples:
Read a GeoTIFF from a HTTP url into a DLPack tensor:
>>> import cupy as cp
>>> from cog3pio import CudaCogReader
...
>>> cog = CudaCogReader(
... path="https://github.com/rasterio/rasterio/raw/1.5.0/tests/data/RGBA.byte.tif"
... device_id=0,
... )
>>> array: cp.ndarray = cp.from_dlpack(cog)
>>> array.shape
(2271752,)
>>> array.dtype
dtype('uint8')
Functions⚓︎
__dlpack__(stream=None, max_version=None, dl_device=None, copy=None)
⚓︎
Get image pixel data from GeoTIFF as a DLPack capsule.
Parameters:
-
(stream⚓︎int | None, default:None) –A Python integer representing a pointer to a stream, on devices that support streams. Device-specific values of stream for CUDA:
None: producer must assume the legacy default stream (default).1: the legacy default stream.2: the per-thread default stream.> 2: stream number represented as a Python integer.0is disallowed due to its ambiguity:0could mean eitherNone,1, or2.
-
(max_version⚓︎tuple[int, int] | None, default:None) –The maximum DLPack version that the consumer (i.e., the caller of
__dlpack__) supports, in the form of a 2-tuple (major,minor). This method may return a capsule of versionmax_version(recommended if it does support that), or of a different version. This means the consumer must verify the version even whenmax_versionis passed. -
(dl_device⚓︎tuple[int, int] | None, default:None) –The DLPack device type. Default is
None, meaning the exported capsule should be on the same device asselfis (i.e. CUDA). When specified, the format must be a 2-tuple, following that of the return value ofarray.__dlpack_device__(). If the device type cannot be handled by the producer, this function will raise BufferError. -
(copy⚓︎bool | None, default:None) –Boolean indicating whether or not to copy the input. Currently only
Noneis supported, meaning the function must reuse the existing memory buffer if possible and copy otherwise (copy is not actually implemented).
Returns:
-
tensor(CapsuleType) –1D tensor in row-major order containing the GeoTIFF pixel data.
Raises:
-
NotImplementedError–If either of these cases happen:
stream>2 is passed in, as only legacy default stream (1) or per-thread default stream (2) is supported for now.max_versionis incompatible with the DLPack major version in this library.copyis set to a value other thanNoneas Copy keyword argument behavior is not handled yet.
-
BufferError–If trying to decode to non-CUDA memory, i.e. when
dl_deviceis notNone, or set to a tuple other than(2, x). This error may also be raised if trying to decode to an unsupported version from the DLPack 0.x series.
__dlpack_device__()
⚓︎
xy_coords()
⚓︎
Get list of x and y coordinates.
Determined based on an Affine transformation matrix built from the
ModelPixelScaleTag and ModelTiepointTag TIFF tags. Note that non-zero
rotation (set by ModelTransformationTag) is currently unsupported.
Returns:
cog3pio.CogReader
⚓︎
Python class interface to a Cloud-optimized GeoTIFF reader (image-tiff backend).
Parameters:
Returns:
-
reader(CogReader) –A new CogReader instance for decoding GeoTIFF files.
Examples:
Read a GeoTIFF from a HTTP url into a DLPack tensor:
>>> import numpy as np
>>> from cog3pio import CogReader
...
>>> cog = CogReader(
... path="https://github.com/rasterio/rasterio/raw/1.5.0/tests/data/RGBA.uint16.tif"
... )
>>> array: np.ndarray = np.from_dlpack(cog)
>>> array.shape
(4, 411, 634)
>>> array.dtype
dtype('uint16')
Source code in cog3pio/__init__.py
15 | |
Functions⚓︎
__dlpack__(stream=None)
⚓︎
Get image pixel data from GeoTIFF as a DLPack capsule
Returns:
-
tensor(CapsuleType) –3D tensor of shape (band, height, width) containing the GeoTIFF pixel data.
Raises:
-
NotImplementedError–If
streamis notNone, as only decoding to the CPU is supported.
__dlpack_device__()
staticmethod
⚓︎
xy_coords()
⚓︎
Get list of x and y coordinates.
Determined based on an Affine transformation matrix built from the
ModelPixelScaleTag and ModelTiepointTag TIFF tags. Note that non-zero
rotation (set by ModelTransformationTag) is currently unsupported.
Returns:
Xarray⚓︎
cog3pio.xarray_backend.Cog3pioBackendEntrypoint
⚓︎
Bases: BackendEntrypoint
Xarray backend to read GeoTIFF files using 'cog3pio' engine.
When using :py:func:xarray.open_dataarray with engine="cog3pio", the optional
device parameter can be set to a (device_type, device_id) tuple, conforming
to DLPack's device convention.
For example:
(2, 0): for reading into a CUDA GPU at device_id 0 (Default).(1, 0)orNone: for reading into CPU.
Examples:
Read a GeoTIFF from a HTTP url into an xarray.DataArray:
>>> import xarray as xr
...
>>> # Read GeoTIFF into an xarray.DataArray
>>> dataarray: xr.DataArray = xr.open_dataarray(
... filename_or_obj="https://github.com/OSGeo/gdal/raw/v3.11.0/autotest/gcore/data/byte_zstd.tif",
... engine="cog3pio",
... device=(2, 0), # CUDA device (2), GPU 0
... )
>>> dataarray.sizes
Frozen({'band': 1, 'y': 20, 'x': 20})
>>> dataarray.dtype
dtype('uint8')
Source code in cog3pio/xarray_backend.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 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 | |
Functions⚓︎
open_dataset(filename_or_obj, *, drop_variables=None, device=(2, 0), mask_and_scale=None)
⚓︎
Backend open_dataset method used by Xarray in xarray.open_dataset.
Parameters:
-
(filename_or_obj⚓︎str) –File path or url to a TIFF (.tif) image file that can be read by the nvTIFF or image-tiff backend library.
-
(device⚓︎(int, int) | None, default:(2, 0)) –Device on which to place the created array, given as a tuple in the form of (device_type, device_id), corresponding to DLPack's device convention. Default is (2, 0) which means device_type='cuda', device_id=0. Pass
device=Noneordevice=(1, 0)to use the CPU fallback.
Returns:
-
Dataset–
Source code in cog3pio/xarray_backend.py
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 | |
NumPy⚓︎
cog3pio.read_geotiff(path)
builtin
⚓︎
Read a GeoTIFF file from a path on disk or a url into an ndarray.
Parameters:
Returns:
-
array(ndarray) –3D array of shape (band, height, width) containing the GeoTIFF pixel data.
Raises:
-
ValueError–If a TIFF which has a non-float32 dtype is passed in. Please use cog3pio.CogReader for reading TIFFs with other dtypes (e.g. uint16).
Examples:
Read a GeoTIFF from a HTTP url into a numpy.ndarray:
>>> from cog3pio import read_geotiff
...
>>> array = read_geotiff("https://github.com/pka/georaster/raw/v0.2.0/data/tiff/float32.tif")
>>> array.shape
(1, 20, 20)