Sample pixels at map coordinates#

Use coordinates in the raster’s CRS to look up one cell on the host or sample a batch on the GPU.

Look up one cell on the host#

For a map coordinate in the dataset CRS, index() gives the row and column. You can read that cell as a bounded one-pixel window and check its validity separately.

import numpy as np
import cugeo.raster as cr
from cugeo.raster.windows import Window

path = "cugeo-raster/docs/guide/tutorial/assets/rpc-image.tif"

with cr.open(path, "r") as src:
    row, col = src.index(1.5, 0.5)
    window = Window(col, row, 1, 1)
    value = src.read(1, window=window, out=np.empty((1, 1), dtype=np.uint16))
    valid = src.read_masks(1, window=window, out=np.empty((1, 1), dtype=np.uint8))
    print(row, col)
    print(value)
    print(valid)

For the fixture, the index is (0, 1), the value is 20, and the mask reports that the cell is valid. When a value could also be a fill value, use read_masks for band validity or dataset_mask for dataset validity.

Sample coordinates on the GPU#

For a batch of points on your own data, sample() requires a stored affine and an admitted native GPU codec. Supply coordinates as a CUDA array of shape (N, 2) with dtype float32 or float64, in the dataset’s coordinate system and units. Host iterables and masked=True are refused.

Admission depends on the input and GPU codec. Check supported formats and execution routes for your dataset before sampling. The sample contract is documented in the reader implementation.