Last active
February 4, 2019 14:37
-
-
Save tomasholderness/2377b617a4da2529f1dbc987a9689448 to your computer and use it in GitHub Desktop.
Lambda function to read COG, maintaining file object outside the handler
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 | |
# open file as a global | |
SRC = rasterio.open('s3://bucket/cog.tif') # <<-- THIS LINE HAS MOVED UP FROM THE FUNCTION BELOW :-) | |
def handler(event, context): | |
"""Lambda function to read pixel values from a COG""" | |
# 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