admin管理员组

文章数量:1122846

When I run gdal2tiles command in a subprocess, how can I follow the progress of the tiling process (from 0 to 1) ?

I tried using --verbose parameter, but logs from gdal2tiles do not show progress line by line.

I tried using watchdog and listen to the tiles generated by gdal2tiles, but it does not seems to work.

Here's my code :

def run_command(command):
    """Run a shell command, handle errors and return its output."""

    try:
        result = subprocess.run(
            command,
            shell=True,
            check=True,
            text=True,
            capture_output=True,
        )
        if result.stderr:
            logger.warning(result.stderr)
        return result.stdout
    except subprocess.CalledProcessError as error:
        error_message = (
            f"Command '{error.cmd}' failed with return code {error.returncode}.\n"
            f"Standard output:\n{error.stdout}\n"
            f"Standard error:\n{error.stderr}\n"
        )
        raise RuntimeError(error_message) from error


def extract_tiles_for_geotiff(folder_images, path):
    """Use gdal2tiles to extract tile images."""

    options = [
        f"-p mercator",
        "--resampling bilinear",
        f"--processes={multiprocessing.cpu_count()}",
        "--xyz"
    ]

    def run_gdal2tiles(options):
        options_str = " ".join(options)
        command = f"gdal2tiles.py {options_str} {path} {folder_images}/"
        run_command(command)

    run_gdal2tiles(options)

本文标签: subprocessHow can we track progress of gdal2tiles command in pythonStack Overflow