Skip to content

files

Working with file inputs and outputs

BadArchiveError

Bases: OSError

The archive contains a bad file

Source code in packages/common/src/bag3d/common/utils/files.py
13
14
class BadArchiveError(OSError):
    """The archive contains a bad file"""

check_export_results(path_quadtree_tsv, export_dir)

Parse the quadtree.tsv written by tyler, check if all formats exists for each tile, add the tile WKT.

Returns:

Type Description
Iterator[ExportResult]

Generator of ExportResult

Source code in packages/common/src/bag3d/common/utils/files.py
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
def check_export_results(
    path_quadtree_tsv: Path, export_dir: Path
) -> Iterator[ExportResult]:
    """Parse the `quadtree.tsv` written by *tyler*, check if all formats exists for each
    tile, add the tile WKT.

    Returns:
         Generator of ExportResult
    """
    with path_quadtree_tsv.open("r") as fo:
        csvreader = csv.DictReader(fo, delimiter="\t")
        for row in csvreader:
            if row["leaf"] == "true" and int(row["nr_items"]) > 0:
                leaf_id = row["id"]
                basename = export_tile_path(export_dir, leaf_id, "")
                obj_paths = tuple(basename.parent.glob(f"{basename.name}*.obj"))
                expected_paths = (
                    basename.with_suffix(".city.json"),
                    basename.with_suffix(".gpkg"),
                    *obj_paths,
                )
                if any(path.exists() for path in expected_paths):
                    yield ExportResult(
                        tile_id=leaf_id,
                        cityjson_path=basename.with_suffix(".city.json"),
                        gpkg_path=basename.with_suffix(".gpkg"),
                        obj_paths=obj_paths,
                        wkt=row["wkt"],
                    )

export_tile_path(export_dir, tile_id, suffix)

Return the path of a Tyler tile output.

Source code in packages/common/src/bag3d/common/utils/files.py
17
18
19
def export_tile_path(export_dir: Path, tile_id: str, suffix: str) -> Path:
    """Return the path of a Tyler tile output."""
    return export_dir.joinpath("t", tile_id).with_suffix(suffix)

unzip(file, dest, remove=True)

Uncompress the whole zip archive and optionally delete the zip.

Parameters:

Name Type Description Default
file Path

The Path to the zip.

required
dest Path

The Path to the destination directory.

required
remove bool

Whether to remove the zip.

True

Raises:

Type Description
BadArchiveError

The archive contains at least one bad file

Source code in packages/common/src/bag3d/common/utils/files.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
def unzip(file: Path, dest: Path, remove: bool = True) -> None:
    """Uncompress the whole zip archive and optionally delete the zip.

    Args:
        file: The Path to the zip.
        dest: The Path to the destination directory.
        remove: Whether to remove the zip.

    Raises:
        BadArchiveError: The archive contains at least one bad file
    """
    logger = get_dagster_logger()
    logger.info(f"Uncompressing {file} to {dest}")
    with ZipFile(file, "r") as ezip:
        first_bad_file = ezip.testzip()
        if first_bad_file:
            raise BadArchiveError(
                f"The archive contains at least one bad file: {first_bad_file}"
            )
        ezip.extractall(path=dest)
    if remove:
        logger.info(f"Deleting {file}")
        file.unlink()