def write(self, overwrite: bool = False, **kwargs: Any): """Write mosaicjson document to AWS DynamoDB. Args: overwrite (bool): delete old mosaic items inthe Table. **kwargs (any): Options forwarded to `dynamodb.create_table` Returns: dict: dictionary with metadata constructed from the sceneid. Raises: MosaicExistsError: If mosaic already exists in the Table. """ if not self._table_exists(): self._create_table(**kwargs) if self._mosaic_exists(): if not overwrite: raise MosaicExistsError( f"Mosaic already exists in {self.table_name}, use `overwrite=True`." ) self.delete() items = self._create_items() self._write_items(items)
def write(self, overwrite: bool = False, **kwargs: Any): """Write mosaicjson document to AWS S3.""" if not overwrite and self._head_object(self.key, self.bucket): raise MosaicExistsError("Mosaic file already exist, use `overwrite=True`.") mosaic_doc = self.mosaic_def.dict(exclude_none=True) if self.key.endswith(".gz"): body = _compress_gz_json(mosaic_doc) else: body = json.dumps(mosaic_doc).encode("utf-8") self._put_object(self.key, self.bucket, body, **kwargs)
def write(self, overwrite: bool = False): """Write mosaicjson document to a file.""" if not overwrite and pathlib.Path(self.path).exists(): raise MosaicExistsError( "Mosaic file already exist, use `overwrite=True`.") body = self.mosaic_def.dict(exclude_none=True) with open(self.path, "wb") as f: try: if self.path.endswith(".gz"): f.write(_compress_gz_json(body)) else: f.write(json.dumps(body).encode("utf-8")) except Exception as e: exc = _FILE_EXCEPTIONS.get(e, MosaicError) # type: ignore raise exc(str(e)) from e
def write(self, overwrite: bool = False, **kwargs: Any): """Write mosaicjson document to AWS DynamoDB. Args: overwrite (bool): delete old mosaic items inthe Table. **kwargs (any): Options forwarded to `dynamodb.create_table` Raises: MosaicExistsError: If mosaic already exists in the Table. """ if not self._table_exists(): self._create_table(**kwargs) if self._mosaic_exists(): if not overwrite: raise MosaicExistsError( f"Mosaic already exists in {self.table_name}, use `overwrite=True`." ) self.delete() items: List[Dict[str, Any]] = [] # Create Metadata item # Note: `parse_float=Decimal` is required because DynamoDB requires all numbers to be # in Decimal type (ref: https://blog.ruanbekker.com/blog/2019/02/05/convert-float-to-decimal-data-types-for-boto3-dynamodb-using-python/) meta = json.loads(self.mosaic_def.json(exclude={"tiles"}), parse_float=Decimal) items.append({ "quadkey": self._metadata_quadkey, "mosaicId": self.mosaic_name, **meta }) # Create Tile items for quadkey, assets in self.mosaic_def.tiles.items(): items.append({ "mosaicId": self.mosaic_name, "quadkey": quadkey, "assets": assets }) self._write_items(items)
def write(self, overwrite: bool = False): """Write mosaicjson document to an SQLite database. Args: overwrite (bool): delete old mosaic items in the Table. Raises: MosaicExistsError: If mosaic already exists in the Table. """ if self._mosaic_exists(): if not overwrite: raise MosaicExistsError( f"'{self.mosaic_name}' Table already exists in {self.db_path}, use `overwrite=True`." ) self.delete() with self.db: logger.debug( f"Creating '{self.mosaic_name}' Table in {self.db_path}.") self.db.execute(f""" CREATE TABLE IF NOT EXISTS {self._metadata_table} ( mosaicjson TEXT NOT NULL, name TEXT NOT NULL, description TEXT, version TEXT NOT NULL, attribution TEXT, minzoom INTEGER NOT NULL, maxzoom INTEGER NOT NULL, quadkey_zoom INTEGER, bounds JSON NOT NULL, center JSON ); """) self.db.execute(f""" CREATE TABLE "{self.mosaic_name}" ( quadkey TEXT NOT NULL, assets JSON NOT NULL ); """) logger.debug(f"Adding items in '{self.mosaic_name}' Table.") self.db.execute( f""" INSERT INTO {self._metadata_table} ( mosaicjson, name, description, version, attribution, minzoom, maxzoom, quadkey_zoom, bounds, center ) VALUES ( :mosaicjson, :name, :description, :version, :attribution, :minzoom, :maxzoom, :quadkey_zoom, :bounds, :center ); """, self.mosaic_def.dict(), ) self.db.executemany( f'INSERT INTO "{self.mosaic_name}" (quadkey, assets) VALUES (?, ?);', self.mosaic_def.tiles.items(), )