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 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 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)
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)
def testSvgFillPixel(self): symbol = QgsSVGFillSymbolLayer("test/star.svg", 10, 45) symbol.setSvgFillColor(QColor("blue")) symbol.setSvgOutlineWidth(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 outline lineSymbolizer = root.elementsByTagName("se:LineSymbolizer").item(0).toElement() self.assertStrokeWidth(lineSymbolizer, 1, 10)
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)
def __getQgsPolygonSymbolLayer(self, msstyle): props = {} type_marker = '' symbol = msstyle.get('symbol', False) if symbol: (type_marker, symbolname, msSymbol, props) = self.__getMsSymbol(symbol, props, True) opacity = self.__getMsOpacity(msstyle) color = self.__getMsColor(msstyle, props, opacity) self.__getMsOutlinecolor(msstyle, props, opacity) self.__getMsLinecap(msstyle, props) self.__getMsLinejoin(msstyle, props) self.__getMsPattern(msstyle, props) self.__getMsWidth(msstyle, props) self.__getMsOutline(msstyle, props) self.__getMsScale(msstyle, props) offset = self.__getMsOffset(msstyle, props) if not type_marker: #-linea sencilla exterior-# #-relleno simple (sin estilo de relleno preconfigurados)-# if not color: self.deleteProperties(props, _qgis.SIMPLE_LINE_SYMBOL_LAYER) qgsSymbol = QgsSimpleLineSymbolLayerV2.create(props) else: self.deleteProperties(props, _qgis.SIMPLE_FILL_SYMBOL_LAYER) qgsSymbol = QgsSimpleFillSymbolLayerV2.create(props) #-relleno de gradientes-# #el gradiente es para renderer por categorias continuas #no aplica, porque aca es para gradiente de un simbolo else: props_parent = {} self.__setMsOffsetXY(offset, props) self.__getMsAngle(msstyle, props, props_parent) gap = self.__getMsGap(msstyle) size = self.__getMsSize(msstyle, props) self.__getMarkerDisplacementAndRotate(msstyle, gap, size, props_parent) geomtransform = msstyle.get('geomtransform', '').lower() if geomtransform == 'centroid': #-relleno de centroides-# self.__getMsAnchorpoint(msSymbol, props) qgsSubSymbol = self.__getQgsMarkerSubSymbol( type_marker, msSymbol, size, props) #self.deleteProperties(props_parent, _qgis.CENTROID_FILL_SYMBOL_LAYER) qgsSymbol = QgsCentroidFillSymbolLayerV2.create({}) qgsSymbol.setSubSymbol(qgsSubSymbol) elif not color: #-Linea exterior:linea de marcador-# self.__getMsAnchorpoint(msSymbol, props) qgsSubSymbol = self.__getQgsMarkerSubSymbol( type_marker, msSymbol, size, props) self.deleteProperties(props_parent, _qgis.MARKER_LINE_SYMBOL_LAYER) qgsSymbol = QgsMarkerLineSymbolLayerV2.create(props_parent) qgsSymbol.setSubSymbol(qgsSubSymbol) elif type_marker == _ms.MS_SYMBOL_HATCH: if not props['use_custom_dash']: #-relleno simple (con estilo de relleno)-# self.deleteProperties(props, _qgis.SIMPLE_LINE_SYMBOL_LAYER) qgsSymbol = QgsSimpleLineSymbolLayerV2.create(props) else: #-Patron de relleno de linea-# self.deleteProperties(props, _qgis.SIMPLE_LINE_SYMBOL_LAYER) qgsSubSymbol = self.__getSubSymbol( QgsLineSymbolV2, QgsSimpleLineSymbolLayerV2, QGis.Line, props) self.deleteProperties(props_parent, _qgis.LINE_PATTERN_FILL_SYMBOL_LAYER) qgsSymbol = QgsLinePatternFillSymbolLayer.create( props_parent) qgsSymbol.setSubSymbol(qgsSubSymbol) elif type_marker == _ms.MS_SYMBOL_PIXMAP: #-relleno de imagen raster-# self.deleteProperties(props, _qgis.RASTER_FILL_SYMBOL_LAYER) qgsSymbol = QgsRasterFillSymbolLayer.create(props) elif type_marker == _ms.MS_SYMBOL_SVG: #-relleno SVG-# self.deleteProperties(props, _qgis.SIMPLE_LINE_SYMBOL_LAYER) qgsSubSymbol = self.__getSubSymbol(QgsLineSymbolV2, QgsSimpleLineSymbolLayerV2, QGis.Line, props) self.deleteProperties(props_parent, _qgis.SVG_FILL_SYMBOL_LAYER) qgsSymbol = QgsSVGFillSymbolLayer.create(props_parent) qgsSymbol.setSubSymbol(qgsSubSymbol) else: #-patron de relleno de puntos-# self.__getMsAnchorpoint(msSymbol, props) qgsSubSymbol = self.__getQgsMarkerSubSymbol( type_marker, msSymbol, size, props) self.deleteProperties(props_parent, _qgis.POINT_PATTERN_FILL_SYMBOL_LAYER) qgsSymbol = QgsPointPatternFillSymbolLayer.create(props_parent) qgsSymbol.setSubSymbol(qgsSubSymbol) #qgsSymbol.setAlpha((opacity*1.0)/100) #print(props) return qgsSymbol