def vsifile_21(): filename = '/vsimem/read.tif' filename_write = '/vsimem/write.tif' data = 'This is some data' vsifile = gdal.VSIFOpenL(filename, 'wb') if gdal.VSIFWriteL(data, 1, len(data), vsifile) != len(data): return 'fail' gdal.VSIFCloseL(vsifile) vsifile = gdal.VSIFOpenL(filename, 'rb') gdal.VSIFSeekL(vsifile, 0, 2) vsilen = gdal.VSIFTellL(vsifile) gdal.VSIFSeekL(vsifile, 0, 0) data_read = gdal.VSIFReadL(1, vsilen, vsifile) data_mem = gdal.VSIGetMemFileBuffer_unsafe(filename) if data_read != data_mem[:]: return 'fail' gdal.VSIFCloseL(vsifile) vsifile_write = gdal.VSIFOpenL(filename_write, 'wb') if gdal.VSIFWriteL(data_mem, 1, len(data_mem), vsifile_write) != len(data_mem): return 'fail' gdal.VSIFCloseL(vsifile_write) gdal.Unlink(filename) gdal.Unlink(filename_write) with gdaltest.error_handler(): data3 = gdal.VSIGetMemFileBuffer_unsafe(filename) if data3 != None: return 'fail' return 'success'
def test_vsifile_21(): filename = '/vsimem/read.tif' filename_write = '/vsimem/write.tif' data = 'This is some data' vsifile = gdal.VSIFOpenL(filename, 'wb') assert gdal.VSIFWriteL(data, 1, len(data), vsifile) == len(data) gdal.VSIFCloseL(vsifile) vsifile = gdal.VSIFOpenL(filename, 'rb') gdal.VSIFSeekL(vsifile, 0, 2) vsilen = gdal.VSIFTellL(vsifile) gdal.VSIFSeekL(vsifile, 0, 0) data_read = gdal.VSIFReadL(1, vsilen, vsifile) data_mem = gdal.VSIGetMemFileBuffer_unsafe(filename) assert data_read == data_mem[:] gdal.VSIFCloseL(vsifile) vsifile_write = gdal.VSIFOpenL(filename_write, 'wb') assert gdal.VSIFWriteL(data_mem, 1, len(data_mem), vsifile_write) == len(data_mem) gdal.VSIFCloseL(vsifile_write) gdal.Unlink(filename) gdal.Unlink(filename_write) with gdaltest.error_handler(): data3 = gdal.VSIGetMemFileBuffer_unsafe(filename) assert data3 == None
def get_geotiff(data, bbox, path): """ transform the vigilance numpy array into a Geotiff file param data : vigilance array param bbox : bounding box return : buffer : buffer of the geoTiff bytes """ driver = gdal.GetDriverByName('GTiff') ds = gdal.Open(path) ysize, xsize = data.shape driver.Create('/vsimem/vigi', xsize, ysize, 1, gdal.GDT_Byte) ds_ = gdal.Open('/vsimem/vigi', gdal.GA_Update) srs = osr.SpatialReference() wkt = ds.GetProjection() srs.ImportFromWkt(wkt) ds_.SetProjection(srs.ExportToWkt()) gt = ds.GetGeoTransform() gt = (bbox[0], gt[1], gt[2], bbox[3], gt[4], gt[5]) ds_.SetGeoTransform(gt) outband = ds_.GetRasterBand(1) outband.SetStatistics(np.min(data), np.max(data), np.average(data), np.std(data)) outband.WriteArray(data) driver.CreateCopy('/vsimem/vigi', ds_) file_buffer = gdal.VSIGetMemFileBuffer_unsafe('/vsimem/vigi') buffer = BytesIO() buffer.write(file_buffer) buffer.seek(0) gdal.Unlink('/vsimem/vigi') ds_ = None return buffer