Skip to content

Instantly share code, notes, and snippets.

@mdsumner
Last active April 14, 2024 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdsumner/60a4713c775e4a0fa00a5401aec6c990 to your computer and use it in GitHub Desktop.
Save mdsumner/60a4713c775e4a0fa00a5401aec6c990 to your computer and use it in GitHub Desktop.
from osgeo import gdal
gdal.UseExceptions()



## we do something with vrt
dsn = "vrt:///vsicurl/https://github.com/cran/rgl/raw/master/inst/textures/worldsmall.png?a_ullr=-180,90,180,-90&expand=rgb"
ds = gdal.Open(dsn)
## write to tif format, but using MEM
temp_name = '/vsimem/some.tif'
opts = gdal.TranslateOptions(format = "COG", 
     creationOptions = [ "COMPRESS=ZSTD", "PREDICTOR=STANDARD", "RESAMPLING=AVERAGE"])
gdal.Translate(temp_name, ds, options = opts)

## now obtain the those bytes from the GDAL virtual file
f = gdal.VSIFOpenL(temp_name, '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)

## and clean up
gdal.VSIFCloseL(f)
gdal.Unlink(temp_name)

## write to S3 ...

## or a local test
out = open("some.tif", "wb")
out.write(data)
out.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment