Skip to content

Instantly share code, notes, and snippets.

@tomasholderness
Last active February 4, 2019 14:36
Show Gist options
  • Select an option

  • Save tomasholderness/60ec206b3e0f9ae120d8d0969008e4fb to your computer and use it in GitHub Desktop.

Select an option

Save tomasholderness/60ec206b3e0f9ae120d8d0969008e4fb to your computer and use it in GitHub Desktop.
Lambda function to read values from a COG
import json
import rasterio
def handler(event, context):
"""Lambda function to read pixel values from a COG"""
# open file
src = rasterio.open('s3://bucket/cog.tif')
# note: lat, lon extracted from Lambda event object
row, col = src.index(lon, lat) # convert coordinates to raster grid
window = Window(col, row, 1, 1) # create a window of 1 pixel
# Set rasterio environment as required
with rasterio.Env(CPL_CURL_VERBOSE=False, CPL_VSIL_CURL_ALLOWED_EXTENSIONS='.tif', GDAL_DISABLE_READDIR_ON_OPEN='YES'):
pixel = src.read(window=window) # read data
return { # return as json object
'headers': {},
'body': JSON.dumps(pixel.tolist())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment