Пример #1
0
def save_grid_to_image(grid, path, classes, colormap, geo_transform=None):
    """Save this grid as an image.

    Assumes that all values in the grid are values that come from
    one of the classes. Translates the values in the classes to colors
    from the colormap, then finds all the places in the grid that are
    equal to that class and sets all those to the right color.

    Because of the above (classes) this save functions is not exactly
    the same as the ColorMap.apply_to_grid() and files.save_geopng()
    functions.

    The type of image is decided by the path, but I only test with
    PNG."""

    classvalues = set()
    for classline in classes:
        for value in classline:
            classvalues.add(value)

    class_to_color = dict()
    for classvalue in classvalues:
        class_to_color[classvalue] = (
            colormap.value_to_color(classvalue) or (0, 0, 0, 0))

    n, m = grid.shape
    colorgrid = numpy.zeros((4, n, m), dtype=numpy.uint8)

    redgrid = numpy.zeros((n, m))
    greengrid = numpy.zeros((n, m))
    bluegrid = numpy.zeros((n, m))

    for classvalue, color in class_to_color.items():
        mask = (grid == classvalue)
        redgrid += mask * color[0]
        greengrid += mask * color[1]
        bluegrid += mask * color[2]

    colorgrid[0] = redgrid
    colorgrid[1] = greengrid
    colorgrid[2] = bluegrid

    # Colored pixels get opacity 255, non-colored pixels opacity 0
    # (transparent)
    colorgrid[3] = (
        ((redgrid > 0) | (greengrid > 0) | (bluegrid > 0)) * 255)

    files.save_geopng(path, colorgrid, geo_transform)
Пример #2
0
def save_grid_to_image(grid, path, classes, colormap, geo_transform=None):
    """Save this grid as an image.

    Assumes that all values in the grid are values that come from
    one of the classes. Translates the values in the classes to colors
    from the colormap, then finds all the places in the grid that are
    equal to that class and sets all those to the right color.

    Because of the above (classes) this save functions is not exactly
    the same as the ColorMap.apply_to_grid() and files.save_geopng()
    functions.

    The type of image is decided by the path, but I only test with
    PNG."""

    classvalues = set()
    for classline in classes:
        for value in classline:
            classvalues.add(value)

    class_to_color = dict()
    for classvalue in classvalues:
        class_to_color[classvalue] = (colormap.value_to_color(classvalue)
                                      or (0, 0, 0, 0))

    n, m = grid.shape
    colorgrid = numpy.zeros((4, n, m), dtype=numpy.uint8)

    redgrid = numpy.zeros((n, m))
    greengrid = numpy.zeros((n, m))
    bluegrid = numpy.zeros((n, m))

    for classvalue, color in class_to_color.items():
        mask = (grid == classvalue)
        redgrid += mask * color[0]
        greengrid += mask * color[1]
        bluegrid += mask * color[2]

    colorgrid[0] = redgrid
    colorgrid[1] = greengrid
    colorgrid[2] = bluegrid

    # Colored pixels get opacity 255, non-colored pixels opacity 0
    # (transparent)
    colorgrid[3] = (((redgrid > 0) | (greengrid > 0) | (bluegrid > 0)) * 255)

    files.save_geopng(path, colorgrid, geo_transform)
Пример #3
0
def generate_from_asc(
    output_dir_name, input_files, colormapobject, result):
    input_files = sorted(i for i in input_files if i.lower().endswith('.asc'))

    amount = len(input_files)

    if amount == 1:
        basename = os.path.basename(input_files[0])[:8]
        output_file = os.path.join(output_dir_name, basename + ".png")
    elif amount > 1:
        basename = os.path.basename(input_files[0])[:4]
        output_file = os.path.join(
            output_dir_name, basename[:4] + "{:04d}.png")
    else:
        logger.warning('no files found to generate pngs')
        return 0, None

    for i, filename in enumerate(input_files):
        real_filename = output_file.format(i)

        try:
            dataset = gdal.Open(str(filename))
            if dataset is None:
                logger.debug("Dataset None: " + str(filename))
            else:
                grid = dataset.ReadAsArray()

                # Hack -- correct grid_ta for start moment of breach
                if result.resulttype.name == 'gridta':
                    correct_gridta(grid, result)

                colored_grid = colormapobject.apply_to_grid(grid)

                files.save_geopng(
                    real_filename, colored_grid, dataset.GetGeoTransform())
        finally:
            del dataset  # Closes the file object so that it can be
                         # deleted on Windows

    return amount, basename
Пример #4
0
def generate_from_asc(output_dir_name, input_files, colormapobject, result):
    input_files = sorted(i for i in input_files if i.lower().endswith('.asc'))

    amount = len(input_files)

    if amount == 1:
        basename = os.path.basename(input_files[0])[:8]
        output_file = os.path.join(output_dir_name, basename + ".png")
    elif amount > 1:
        basename = os.path.basename(input_files[0])[:4]
        output_file = os.path.join(output_dir_name,
                                   basename[:4] + "{:04d}.png")
    else:
        logger.warning('no files found to generate pngs')
        return 0, None

    for i, filename in enumerate(input_files):
        real_filename = output_file.format(i)

        try:
            dataset = gdal.Open(str(filename))
            if dataset is None:
                logger.debug("Dataset None: " + str(filename))
            else:
                grid = dataset.ReadAsArray()

                # Hack -- correct grid_ta for start moment of breach
                if result.resulttype.name == 'gridta':
                    correct_gridta(grid, result)

                colored_grid = colormapobject.apply_to_grid(grid)

                files.save_geopng(real_filename, colored_grid,
                                  dataset.GetGeoTransform())
        finally:
            del dataset  # Closes the file object so that it can be
            # deleted on Windows

    return amount, basename