Rasterio Single Point Analysis – Getting Pixel Values at Specific Point

numpypythonrasterio

To get a single pixel value at a point in a raster using rasterio, there is an example here: https://github.com/mapbox/rasterio/pull/275

However, is there a direct API within rasterio (and not the cli) which can be used to extract value at a single point in a raster?

— EDIT

with rasterio.drivers():

    # Read raster bands directly to Numpy arrays.
    #
    with rasterio.open('C:\\Users\\rit\\38ERP.tif') as src:
        x = (src.bounds.left + src.bounds.right) / 2.0
        y = (src.bounds.bottom + src.bounds.top) / 2.0

        vals = src.sample((x, y))
        for val in vals:
            print list(val)

Best Answer

The Python API method that supports the rio-sample command is documented here: https://rasterio.readthedocs.io/en/latest/api/rasterio._io.html#rasterio._io.DatasetReaderBase.sample

src.sample() takes an iterator over x, y tuples, so do:

for val in src.sample([(x, y)]): 
    print(val)
Related Question