def main(): # Create TileDB context ctx = tiledb.Ctx() # Create TileDB VFS vfs = tiledb.VFS(ctx) # Create directory if not vfs.is_dir("dir_A"): vfs.create_dir("dir_A") print("Created dir_A") else: print("dir_A already exists") # Creating an(empty) file if not vfs.is_file("dir_A/file_A"): vfs.touch("dir_A/file_A") print("Created empty file dir_A/file_A") else: print("dir_A/file_A already exists") # Getting the file size print("File size: {0!s}".format(vfs.file_size("dir_A/file_A"))) # Moving files(moving directories is similar) print("Moving file dir_A/file_A to dir_A/file_B") vfs.move("dir_A/file_A", "dir_A/file_B", force=True) # Deleting files and directories print("Deleting dir_A/file_B and dir_A") vfs.remove_file("dir_A/file_B") vfs.remove_dir("dir_A")
def __init__(self): self.logger = logging.getLogger(self.__class__.__name__) self.config = Config.instance() self.tiledb_conf = tiledb.Config() self.init_tiledb_conf() self.tiledb_ctx = tiledb.Ctx(config=self.tiledb_conf) self.array_conn = {} self.vfs = tiledb.VFS(ctx=self.tiledb_ctx, config=self.tiledb_conf) self.data_bucket_ready = False self.check_and_create_bucket()
def main(): # create TileDB context ctx = tiledb.Ctx() # create TileDB VFS vfs = tiledb.VFS(ctx) # Read binary data fh = vfs.open("tiledb_vfs.bin", mode='r') nbytes = struct.calcsize("f") f1 = vfs.read(fh, 0, nbytes) s1 = vfs.read(fh, nbytes, 12) print("Binary read:\n{0:0.1f}\n{1!r}".format( struct.unpack("f", f1)[0], s1)) # Close and remove the binary file vfs.close(fh) vfs.remove_file("tiledb_vfs.bin")
def main(): config = tiledb.Config() # Print the default config parameters print("Default settings:\n{0!r}".format(config)) # Set values config["vfs.s3.connect_timeout_ms"] = 5000 config["vfs.s3.endpoint_override"] = "localhost:88880" # Get values tile_cache_size = config["sm.tile_cache_size"] print("\nTile cache size: ", tile_cache_size) # Print only the s3 settings print("\nVFS S3 settings:") for p, v in config.items(prefix="vfs.s3."): print("{0!r} : {1!r}".format(p, v)) # Assign a config object to Ctx and VFS ctx = tiledb.Ctx(config=config) vfs = tiledb.VFS(ctx, config=config)
def main(): ctx = tiledb.Ctx() vfs = tiledb.VFS(ctx) # write binary data fh = vfs.open("tiledb_vfs.bin", mode='w') f1, s1 = 153.0, b'abcd' vfs.write(fh, struct.pack("f", f1)) vfs.write(fh, s1) vfs.close(fh) # write binary data again, this will overwrite the previous file fh = vfs.open("tiledb_vfs.bin", mode='w') f1, s2 = 153.1, b'abcdef' vfs.write(fh, struct.pack("f", f1)) vfs.write(fh, s2) vfs.close(fh) # append binary data to existing file fh = vfs.open("tiledb_vfs.bin", mode='a') s3 = b'ghijkl' vfs.write(fh, s3) vfs.close(fh)
def stack(_input, output, tile_x_size, tile_y_size, config=None, attrs=None, bbox=None): with rasterio.open(_input) as src: profile = src.profile trans = Affine.to_gdal(src.transform) dt = np.dtype(src.dtypes[0]) # read first band data type # read initial image metadata profile['driver'] = 'TileDB' profile['blockxsize'] = tile_x_size profile['blockysize'] = tile_y_size if 'tiled' in profile: del profile['tiled'] arr = xr.open_rasterio(_input, chunks={'x': tile_x_size, 'y': tile_y_size}) if bbox is None: w = profile['width'] h = profile['height'] bbox = (0, 0, w, h) else: w = bbox[2] - bbox[0] h = bbox[3] - bbox[1] nBlocksX = math.ceil(w / (tile_x_size * 1.0)) nBlocksY = math.ceil(h / (tile_y_size * 1.0)) # GDAL TileDB driver writes/reads blocks so bypass rasterio dom = tiledb.Domain( tiledb.Dim(name='BANDS', domain=(0, profile['count'] - 1), tile=1), tiledb.Dim(name='Y', domain=(0, (nBlocksY * tile_y_size) - 1), tile=tile_y_size, dtype=np.uint64), tiledb.Dim(name='X', domain=(0, (nBlocksX * tile_x_size) - 1), tile=tile_x_size, dtype=np.uint64)) cfg = tiledb.Config(config) ctx = tiledb.Ctx(config=cfg) schema = tiledb.ArraySchema( domain=dom, sparse=False, attrs=[tiledb.Attr(name="TDB_VALUES", dtype=dt)], ctx=ctx) tiledb.DenseArray.create(output, schema) with tiledb.DenseArray(output, 'w', ctx=ctx) as arr_output: arr[:, bbox[0]:bbox[2], bbox[1]:bbox[3]].data.to_tiledb(arr_output, storage_options=config) # write the GDAL metadata file from the source profile vfs = tiledb.VFS() meta = f"{output}/{os.path.basename(output)}.tdb.aux.xml" try: f = vfs.open(meta, "w") root = ET.Element('PAMDataset') geo = ET.SubElement(root, 'GeoTransform') geo.text = ', '.join(map(str, trans)) meta = ET.SubElement(root, 'Metadata') meta.set('domain', 'IMAGE_STRUCTURE') t = ET.SubElement(meta, 'MDI') t.set('key', 'DATA_TYPE') t.text = _gdal_typename(np.complex128) nbits = ET.SubElement(meta, 'MDI') nbits.set('key', 'NBITS') nbits.text = str(dt.itemsize * 8) xsize = ET.SubElement(meta, 'MDI') xsize.set('key', 'X_SIZE') xsize.text = str(w) ysize = ET.SubElement(meta, 'MDI') ysize.set('key', 'Y_SIZE') ysize.text = str(h) vfs.write(f, ET.tostring(root)) finally: vfs.close(f)