def getDefaultSymbol(size): symbol = QgsMarkerSymbol() for i in range(symbol.symbolLayerCount()): symbol.takeSymbolLayer(0) symbolLayer = QgsSvgMarkerSymbolLayer( os.path.join(os.path.dirname(__file__), 'svg', 'question.svg')) symbolLayer.setSizeUnit(3) symbolLayer.setSize(size) symbol.insertSymbolLayer(0, symbolLayer) return symbol
def append_PictureMarkerSymbolLayer(symbol, layer: PictureMarkerSymbolLayer, context: Context): """ Appends a PictureMarkerSymbolLayer 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): 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) out = QgsSvgMarkerSymbolLayer(svg_path, context.convert_size(layer.size), layer.angle) out.setSizeUnit(context.units) out.setEnabled(layer.enabled) out.setLocked(layer.locked) out.setOffset( adjust_offset_for_rotation( QPointF(context.convert_size(layer.x_offset), -context.convert_size(layer.y_offset)), layer.angle)) out.setOffsetUnit(context.units) symbol.appendSymbolLayer(out)
def getSymbolLayer(folder, svg, size): svg = svg + '.svg' root = os.path.join(os.path.dirname(__file__), 'svg', folder) filepath = None for base, dirs, files in os.walk(root): matching = fnmatch.filter(files, svg) if matching: filepath = os.path.join(base, matching[0]) break if filepath is not None: symbolLayer = QgsSvgMarkerSymbolLayer(filepath) symbolLayer.setSizeUnit(3) symbolLayer.setSize(size) return symbolLayer else: return None
def append_CharacterMarkerSymbolLayerAsSvg(symbol, layer, context: Context): # pylint: disable=too-many-locals """ Appends a CharacterMarkerSymbolLayer to a symbol, rendering the font character to an SVG file. """ font_family = layer.font character = chr(layer.unicode) color = symbol_color_to_qcolor(layer.color) angle = convert_angle(layer.angle) font = QFont(font_family) font.setPointSizeF(layer.size) # Using the rect of a painter path gives better results then using font metrics path = QPainterPath() path.setFillRule(Qt.WindingFill) path.addText(0, 0, font, character) rect = path.boundingRect() font_bounding_rect = QFontMetricsF(font).boundingRect(character) # adjust size -- marker size in esri is the font size, svg marker size in qgis is the svg rect size scale = rect.width() / font_bounding_rect.width() gen = QSvgGenerator() svg_path = symbol_name_to_filename(context.symbol_name, context.picture_folder, 'svg') gen.setFileName(svg_path) gen.setViewBox(rect) painter = QPainter(gen) painter.setFont(font) # todo -- size! if context.parameterise_svg: painter.setBrush(QBrush(QColor(255, 0, 0))) else: painter.setBrush(QBrush(color)) painter.setPen(Qt.NoPen) painter.drawPath(path) painter.end() if context.parameterise_svg: with open(svg_path, 'r') as f: t = f.read() t = t.replace('#ff0000', 'param(fill)') t = t.replace('fill-opacity="1" ', 'fill-opacity="param(fill-opacity)"') t = t.replace( 'stroke="none"', 'stroke="param(outline)" stroke-opacity="param(outline-opacity) 1" stroke-width="param(outline-width) 0"' ) with open(svg_path, 'w') as f: f.write(t) svg_path = context.convert_path(svg_path) out = QgsSvgMarkerSymbolLayer(svg_path) out.setSizeUnit(context.units) out.setSize(context.convert_size(scale * rect.width())) out.setAngle(angle) out.setFillColor(color) out.setStrokeWidth(0) out.setEnabled(layer.enabled) out.setLocked(layer.locked) out.setOffset( adjust_offset_for_rotation( QPointF(context.convert_size(layer.x_offset), -context.convert_size(layer.y_offset)), layer.angle)) out.setOffsetUnit(context.units) symbol.appendSymbolLayer(out)