Ejemplo n.º 1
0
    def testQgsSVGFillSymbolLayer(self):
        """
        Create a new style from a .sld file and match test
        """
        mTestName = 'QgsSVGFillSymbolLayer'
        mFilePath = QDir.toNativeSeparators('%s/symbol_layer/%s.sld' %
                                            (unitTestDataPath(), mTestName))

        mDoc = QDomDocument(mTestName)
        mFile = QFile(mFilePath)
        mFile.open(QIODevice.ReadOnly)
        mDoc.setContent(mFile, True)
        mFile.close()
        mSymbolLayer = QgsSVGFillSymbolLayer.createFromSld(
            mDoc.elementsByTagName('PolygonSymbolizer').item(0).toElement())

        mExpectedValue = type(QgsSVGFillSymbolLayer(""))
        mValue = type(mSymbolLayer)
        mMessage = 'Expected "%s" got "%s"' % (mExpectedValue, mValue)
        assert mExpectedValue == mValue, mMessage

        mExpectedValue = 'accommodation_camping.svg'
        mValue = os.path.basename(mSymbolLayer.svgFilePath())
        mMessage = 'Expected "%s" got "%s"' % (mExpectedValue, mValue)
        assert mExpectedValue == mValue, mMessage

        mExpectedValue = 6
        mValue = mSymbolLayer.patternWidth()
        mMessage = 'Expected "%s" got "%s"' % (mExpectedValue, mValue)
        assert mExpectedValue == mValue, mMessage
    def testSvgFillDefault(self):
        symbol = QgsSVGFillSymbolLayer('test/star.svg', 10, 45)
        symbol.setSvgOutlineWidth(3)

        dom, root = self.symbolToSld(symbol)
        # print ("Svg fill mm: \n" + dom.toString())

        self.assertStaticRotation(root, '45')
        self.assertStaticSize(root, '36')
        # width of the svg outline
        self.assertStrokeWidth(root, 1, 11)
        # width of the polygon outline
        self.assertStrokeWidth(root, 3, 1)
    def testSvgFillPixel(self):
        symbol = QgsSVGFillSymbolLayer('test/star.svg', 10, 45)
        symbol.setSvgOutlineWidth(3)
        symbol.setOutputUnit(QgsUnitTypes.RenderPixels)

        dom, root = self.symbolToSld(symbol)
        # print ("Svg fill px: \n" + dom.toString())

        self.assertStaticRotation(root, '45')
        self.assertStaticSize(root, '10')
        # width of the svg outline
        self.assertStrokeWidth(root, 1, 3)
        # width of the polygon outline
        self.assertStrokeWidth(root, 3, 0.26)
    def testSvgFillPixel(self):
        symbol = QgsSVGFillSymbolLayer('test/star.svg', 10, 45)
        symbol.setSvgFillColor(QColor('blue'))
        symbol.setSvgStrokeWidth(3)
        symbol.setOutputUnit(QgsUnitTypes.RenderPixels)
        symbol.subSymbol().setWidth(10)

        dom, root = self.symbolToSld(symbol)
        # print ("Svg fill px: \n" + dom.toString())

        self.assertExternalGraphic(root, 0,
                                   'test/star.svg?fill=%230000ff&fill-opacity=1&outline=%23000000&outline-opacity=1&outline-width=3', 'image/svg+xml')
        self.assertExternalGraphic(root, 1,
                                   'test/star.svg', 'image/svg+xml')
        self.assertWellKnownMark(root, 0, 'square', '#0000ff', '#000000', 3)

        self.assertStaticRotation(root, '45')
        self.assertStaticSize(root, '10')
        # width of the polygon stroke
        lineSymbolizer = root.elementsByTagName('se:LineSymbolizer').item(0).toElement()
        self.assertStrokeWidth(lineSymbolizer, 1, 10)
Ejemplo n.º 5
0
    def testSvgFillDefault(self):
        symbol = QgsSVGFillSymbolLayer('test/star.svg', 10, 45)
        symbol.setSvgFillColor(QColor('blue'))
        symbol.setSvgOutlineWidth(3)
        symbol.setSvgOutlineColor(QColor('yellow'))
        symbol.subSymbol().setWidth(10)

        dom, root = self.symbolToSld(symbol)
        # print ("Svg fill mm: \n" + dom.toString())

        self.assertExternalGraphic(root, 0,
                                   'test/star.svg?fill=%230000ff&fill-opacity=1&outline=%23ffff00&outline-opacity=1&outline-width=11', 'image/svg+xml')
        self.assertExternalGraphic(root, 1,
                                   'test/star.svg', 'image/svg+xml')
        self.assertWellKnownMark(root, 0, 'square', '#0000ff', '#ffff00', 11)

        self.assertStaticRotation(root, '45')
        self.assertStaticSize(root, '36')
        # width of the polygon outline
        lineSymbolizer = root.elementsByTagName('se:LineSymbolizer').item(0).toElement()
        self.assertStrokeWidth(lineSymbolizer, 1, 36)
Ejemplo n.º 6
0
Archivo: qgis.py Proyecto: zyxgis/slyr
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)