Skip to content

Instantly share code, notes, and snippets.

Last active August 15, 2019 06:30
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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)
src_ds = None
#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)
key = 'data/landsat/out.tif'
s3.Bucket('remotepixel').put_object(Key=key, Body=data, ACL='public-read', ContentLength=size)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment