14

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)
12

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)

  • thanks! I am having trouble with the syntax. Should it be: vals = src.sample((x, y)) or vals = src.sample(x, y)? Neither seems to work – user1186 Apr 21 '16 at 2:54
  • Added code in my question above – user1186 Apr 21 '16 at 2:55
  • 4
    src.sample() takes an iterator over x, y tuples, so do for val in src.sample([(x, y)]): print(val). – sgillies Apr 21 '16 at 6:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.