Monday, 20 May 2019

python - Getting pixel values at single point using rasterio


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)

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)


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...