Skip to content

Instantly share code, notes, and snippets.

@vincentsarago
Last active August 15, 2019 06:30
Show Gist options
  • Save vincentsarago/c31f0db27b128b9d50797aacbe9229b0 to your computer and use it in GitHub Desktop.
Save vincentsarago/c31f0db27b128b9d50797aacbe9229b0 to your computer and use it in GitHub Desktop.
from osgeo import gdal
from boto3.session import Session
#Do something with the array
def myFunction(arr):
return np.where(arr > 0, (arr * 10000).astype(np.uint16), 0)
session = Session(region_name='us-west-2')
s3 = session.resource('s3')
src_ds = gdal.Open('myfile.tif', gdal.GA_ReadOnly)
geoT = src_ds.GetGeoTransform()
proj = src_ds.GetProjection()
cols = src_ds.RasterXSize
rows = src_ds.RasterYSize
nBands = 1
driver = gdal.GetDriverByName("GTiff")
mem_ds = driver.Create('/vsimem/inMem.tif', cols, rows, nBands, gdal.GDT_UInt16)
mem_ds.SetGeoTransform(geoT)
mem_ds.SetProjection(proj)
mem_ds.GetRasterBand(1).SetNoDataValue(0)
mem_ds.GetRasterBand(1).WriteArray(myFunction(src_ds.GetRasterBand(1).ReadAsArray()))
src_ds = None
#http://osgeo-org.1560.x6.nabble.com/GDAL-Python-Save-a-dataset-to-an-in-memory-Python-Bytes-object-td5280254.html
#Tweet from Even Rouault
#@_VincentS_ Yes you create something that is not a GeoTIFF but a raw file. You need to CreateCopy('/vsimem/your.tiff', mem_ds) first
#@_VincentS_ and then use VSIFReadL() to ingest the /vsimem/your.tiff, and then VSFIWriteL() it to /vsis3/
f = gdal.VSIFOpenL('/vsimem/inMem.tif', 'rb')
gdal.VSIFSeekL(f, 0, 2) # seek to end
size = gdal.VSIFTellL(f)
gdal.VSIFSeekL(f, 0, 0) # seek to beginning
data = gdal.VSIFReadL(1, size, f)
gdal.VSIFCloseL(f)
key = 'data/landsat/out.tif'
s3.Bucket('remotepixel').put_object(Key=key, Body=data, ACL='public-read', ContentLength=size)
gdal.Unlink('/vsimem/inMem.tif')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment