Skip to content

Instantly share code, notes, and snippets.

@vincentsarago vincentsarago/writeToS3.py
Last active Aug 15, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.