Last active
February 4, 2019 14:36
-
-
Save tomasholderness/60ec206b3e0f9ae120d8d0969008e4fb to your computer and use it in GitHub Desktop.
Lambda function to read values from a COG
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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