Example #1
0
    def add_roads_to_image(roads, image_path, extent):
        """ This function could be moved to top level. """

        # Get old image that needs indirect road damage visualized
        image = Image.open(result_image['path'])

        # Rasterize all roads that have indirect damage
        size = image.size
        geotransform = [
            extent[0],
            (extent[2] - extent[0]) / size[0],
            0,
            extent[3],
            0,
            (extent[1] - extent[3]) / size[1],
        ]
        roadgrid = raster.get_mask(
            roads=road_objects,
            shape=image.size[::-1],
            geo=(b'', geotransform),
        )

        # Paste it into the old image and overwrite the image file
        rgba = np.uint8([[[0, 0, 0, 153]]]) * roadgrid.reshape(
            roadgrid.shape[0], roadgrid.shape[1], 1
        )

        image_roads_rgb = Image.fromarray(rgba[:, :, 0:3])
        image_roads_mask = Image.fromarray(rgba[:, :, 3])
        image.paste(image_roads_rgb, None, image_roads_mask)
        image.save(result_image['path'])
Example #2
0
def get_roads_flooded_for_tile_and_code(code, depth, geo):
    """ Return dict {road: flooded_m2}. """
    area_per_pixel = raster.geo2cellsize(geo)
    roads_flooded_for_tile_and_code = {}
    roads = raster.get_roads(ROAD_GRIDCODE[code], geo, depth.shape)
    for road in roads:
        mask = raster.get_mask([road], depth.shape, geo)
        flooded_m2 = (mask * area_per_pixel * np.greater(depth, 0)).sum()
        if flooded_m2:
            roads_flooded_for_tile_and_code[road.pk] = flooded_m2

    return roads_flooded_for_tile_and_code
Example #3
0
def add_roads_to_image(roads, image_path, extent):
    """Draw roads into an existing PNG."""

    # Get old image that needs indirect road damage visualized
    image = Image.open(image_path)

    # Rasterize all roads that have indirect damage
    size = image.size
    geotransform = [
        extent[0],
        (extent[2] - extent[0]) / size[0],
        0,
        extent[3],
        0,
        (extent[1] - extent[3]) / size[1],
        ]
    roadgrid = raster.get_mask(
        roads=roads,
        shape=image.size[::-1],
        geo=(b'', geotransform),
        )

    # Paste it into the old image and overwrite the image file
    rgba = np.uint8([[[0, 0, 0, 153]]]) * roadgrid.reshape(
        roadgrid.shape[0], roadgrid.shape[1], 1
        )
    image_roads_rgb = Image.fromstring(
        'RGB',
        (rgba.shape[1], rgba.shape[0]),
        rgba[:, :, 0:3].tostring(),
        )
    image_roads_mask = Image.fromstring(
        'L',
        (rgba.shape[1], rgba.shape[0]),
        rgba[:, :, 3].tostring(),
        )
    image.paste(image_roads_rgb, None, image_roads_mask)
    image.save(image_path)