async def transform_data(self, item: Item) -> None: cog_asset_list = [] for asset in item.assets: if not is_tiff(asset.source_path): return start_time = time_in_ms() output_path = os.path.join(item.collection.get_temp_dir(), f"{ulid.ULID()}.tiff") await create_cog(asset.source_path, output_path, compression_method="lzw").run() get_log().debug("Created COG", output_path=output_path, duration=time_in_ms() - start_time) asset.needs_upload = False cog_asset = Asset(output_path) cog_asset.content_type = pystac.MediaType.COG cog_asset.target = asset.target cog_asset_list.append(cog_asset) for asset in cog_asset_list: item.add_asset(asset)
async def load_metadata(self, asset: Asset) -> None: if not self.is_init: self.read_csv() filename = os.path.splitext(os.path.basename(asset.source_path))[0] if filename not in self.raw_metadata: asset.add_error("Asset not found in CSV file", self.name) return asset_metadata = self.raw_metadata[filename] asset.target = f"{asset_metadata['survey']}/{asset_metadata['sufi']}{asset.file_ext()}" collection = get_collection(asset_metadata["survey"]) item = get_item(asset_metadata["sufi"]) collection.add_item(item) item.add_asset(asset) item.collection = collection item.properties.update( { "linz:sufi": asset_metadata["sufi"], "linz:survey": asset_metadata["survey"], "linz:run": asset_metadata["run"], "linz:photo_no": asset_metadata["photo_no"], "linz:alternate_survey_name": asset_metadata["alternate_survey_name"], "linz:camera": asset_metadata["camera"], "linz:camera_sequence_no": asset_metadata["camera_sequence_no"], "linz:nominal_focal_length": asset_metadata["nominal_focal_length"], "linz:altitude": asset_metadata["altitude"], "linz:scale": asset_metadata["scale"], "linz:photocentre_lat": asset_metadata["photocentre_lat"], "linz:photocentre_lon": asset_metadata["photocentre_lon"], "linz:date": asset_metadata["date"], "linz:film": asset_metadata["film"], "linz:film_sequence_no": asset_metadata["film_sequence_no"], "linz:photo_type": asset_metadata["photo_type"], "linz:format": asset_metadata["format"], "linz:source": asset_metadata["source"], "linz:physical_film_condition": asset_metadata["physical_film_condition"], "linz:image_anomalies": asset_metadata["image_anomalies"], "linz:scanned": asset_metadata["scanned"], "linz:raw_filename": asset_metadata["raw_filename"], "linz:released_filename": asset_metadata["released_filename"], "linz:when_scanned": asset_metadata["when_scanned"], "linz:photo_version": asset_metadata["photo_version"], } )
async def load_metadata(self, asset: Asset) -> None: async with self.lock: for loader in self.loaders: if loader.is_applicable(asset): start_time = time_in_ms() try: await loader.load_metadata(asset) if not asset.is_valid: break except Exception as e: asset.add_error(str(e), loader.name, e) get_log().warning(f"Metadata Load Failed: {e}", loader=loader.name) return get_log().debug( "Metadata Loaded", loader=loader.name, duration=time_in_ms() - start_time, )
async def test_load_metadata(): source_path = os.path.join(os.getcwd(), "test_data", "tiffs", "SURVEY_1", "CONTROL.tiff") asset = Asset(source_path) item = Item("item_id") item.add_asset(asset) loader = MetadataLoaderTiff() assert loader.is_applicable(asset) await loader.load_metadata(asset) assert item.properties["proj:epsg"] is None assert len(item.assets) == 1
async def test_item_not_found_in_csv(): source_path = "test_abc.tiff" asset = Asset(source_path) metadata_loader_imagery_historic = MetadataLoaderImageryHistoric() await metadata_loader_imagery_historic.load_metadata(asset) error_msg = { "msg": "Asset not found in CSV file", "level": "error", "cause": "metadata.loader.imagery.historic", "error": None, } assert error_msg in asset.log
async def test_check_validity(): source_path = os.path.join(os.getcwd(), "test_data", "tiffs", "SURVEY_1", "CONTROL.tiff") asset = Asset(source_path) item = Item("item_id") item.add_asset(asset) item.properties.update({"linz:photo_type": "COLOUR"}) validator = MetadataValidatorTiff() assert validator.is_applicable(item) with pytest.raises(Exception, match=r"Wrong photo type of gray"): await validator.validate_metadata(item)
def test_duplicate_asset(): item = Item("item_id") item.collection = Collection("fake_title") cog_1 = Asset("fake_asset.source_path") cog_1.target = "fake_target.tiff" item.add_asset(cog_1) cog_2 = Asset("fake_asset.source_path2") cog_2.target = "fake_target.tiff" item.add_asset(cog_2) with pytest.raises(Exception, match=r"./fake_title/item_id.tiff already exists."): item.create_stac()
def test_is_applicable(): source_path = "test_abc.tiff" asset = Asset(source_path) metadata_loader_imagery_historic = MetadataLoaderImageryHistoric() assert metadata_loader_imagery_historic.is_applicable(asset)