Пример #1
0
def append_PictureFillSymbolLayer(symbol, layer: PictureFillSymbolLayer,
                                  context: Context):
    """
    Appends a PictureFillSymbolLayer to a symbol
    """
    picture = layer.picture
    if issubclass(picture.__class__, StdPicture):
        picture = picture.picture

    if layer.swap_fb_gb:
        raise NotImplementedException('Swap FG/BG color not implemented')

    if issubclass(picture.__class__,
                  EmfPicture) or context.force_svg_instead_of_raster:
        if issubclass(picture.__class__, EmfPicture):
            path = symbol_name_to_filename(context.symbol_name,
                                           context.picture_folder, 'emf')
            with open(path, 'wb') as f:
                f.write(picture.content)

            svg_path = symbol_name_to_filename(context.symbol_name,
                                               context.picture_folder, 'svg')
            emf_to_svg(path, svg_path)

            svg_path = context.convert_path(svg_path)
        else:
            svg = PictureUtils.to_embedded_svg(
                picture.content,
                symbol_color_to_qcolor(layer.color_foreground),
                symbol_color_to_qcolor(layer.color_background),
                symbol_color_to_qcolor(layer.color_transparent))
            if context.embed_pictures:
                svg_base64 = base64.b64encode(
                    svg.encode('UTF-8')).decode('UTF-8')
                svg_path = 'base64:{}'.format(svg_base64)
            else:
                svg_path = write_svg(svg, context.symbol_name,
                                     context.picture_folder)
                svg_path = context.convert_path(svg_path)

        width_in_pixels = layer.scale_x * PictureUtils.width_pixels(
            picture.content)
        width_in_in_points = width_in_pixels / 96 * 72

        out = QgsSVGFillSymbolLayer(svg_path,
                                    context.convert_size(width_in_in_points),
                                    convert_angle(layer.angle))
        out.setPatternWidthUnit(context.units)

    else:
        # use raster fill
        image_path = write_picture(picture, context.symbol_name,
                                   context.picture_folder,
                                   layer.color_foreground,
                                   layer.color_background,
                                   layer.color_transparent)

        out = QgsRasterFillSymbolLayer(image_path)

        # convert to points, so that print layouts work nicely. It's a better match for Arc anyway
        width_in_pixels = layer.scale_x * PictureUtils.width_pixels(
            picture.content)
        width_in_in_points = width_in_pixels / 96 * 72

        out.setWidth(context.convert_size(width_in_in_points))
        out.setWidthUnit(context.units)

        out.setAngle(convert_angle(layer.angle))

    symbol.appendSymbolLayer(out)
    if layer.outline_layer:
        append_SymbolLayer_to_QgsSymbolLayer(symbol, layer.outline_layer,
                                             context)
    elif layer.outline_symbol:
        # get all layers from outline
        append_SymbolLayer_to_QgsSymbolLayer(symbol, layer.outline_symbol,
                                             context)