def createFormatSettings(self): s = QgsTextFormat() s.setBuffer(self.createBufferSettings()) s.setBackground(self.createBackgroundSettings()) s.setShadow(self.createShadowSettings()) s.setFont(getTestFont()) s.setNamedStyle('Roman') s.setSize(5) s.setSizeUnit(QgsUnitTypes.RenderPoints) s.setSizeMapUnitScale(QgsMapUnitScale(1, 2)) s.setColor(QColor(255, 0, 0)) s.setOpacity(0.5) s.setBlendMode(QPainter.CompositionMode_Difference) s.setLineHeight(5) return s
def createFormatSettings(self): s = QgsTextFormat() s.setBuffer(self.createBufferSettings()) s.setBackground(self.createBackgroundSettings()) s.setShadow(self.createShadowSettings()) s.setFont(getTestFont()) s.setNamedStyle('Roman') s.setSize(5) s.setSizeUnit(QgsUnitTypes.RenderPoints) s.setSizeMapUnitScale(QgsMapUnitScale(1, 2)) s.setColor(QColor(255, 0, 0)) s.setOpacity(0.5) s.setBlendMode(QPainter.CompositionMode_Difference) s.setLineHeight(5) return s
def createFormatSettings(self): s = QgsTextFormat() s.setBuffer(self.createBufferSettings()) s.setBackground(self.createBackgroundSettings()) s.setShadow(self.createShadowSettings()) font = getTestFont() font.setKerning(False) s.setFont(font) s.setNamedStyle('Roman') s.setSize(5) s.setSizeUnit(QgsUnitTypes.RenderPoints) s.setSizeMapUnitScale(QgsMapUnitScale(1, 2)) s.setColor(QColor(255, 0, 0)) s.setOpacity(0.5) s.setBlendMode(QPainter.CompositionMode_Difference) s.setLineHeight(5) s.setOrientation(QgsTextFormat.VerticalOrientation) s.setPreviewBackgroundColor(QColor(100, 150, 200)) return s
def grid_labeler(self, coord_base_x, coord_base_y, px, py, u, t, dx, dy, vAlign, hAlign, desc, fSize, fontType, expression_str, trLLUTM, trUTMLL, llcolor, utmcheck, scale): if utmcheck: pgrid = QgsPoint(coord_base_x + px * u, coord_base_y + py * t) pgrid.transform(trLLUTM) pgrid = QgsPoint(pgrid.x() + dx, pgrid.y() + dy) else: pgrid = QgsPoint(coord_base_x + px * u + dx, coord_base_y + py * t + dy) #Label Format Settings settings = QgsPalLayerSettings() settings.Placement = QgsPalLayerSettings.Free settings.isExpression = True textprop = QgsTextFormat() textprop.setColor(llcolor) textprop.setSizeUnit(1) textprop.setSize(fSize * scale * 1.324) textprop.setFont(QFont(fontType)) textprop.setLineHeight(1) settings.setFormat(textprop) settings.fieldName = expression_str #Label Name and Position datadefined = QgsPropertyCollection() datadefined.setProperty(9, pgrid.x()) datadefined.setProperty(10, pgrid.y()) if not (hAlign == ''): datadefined.setProperty(11, hAlign) if not (vAlign == ''): datadefined.setProperty(12, vAlign) datadefined.setProperty(20, 1) #Creating and Activating Labeling Rule settings.setDataDefinedProperties(datadefined) rule = QgsRuleBasedLabeling.Rule(settings) rule.setDescription(desc) rule.setActive(True) return rule
def grid_labeler(self, coord_base_x, coord_base_y, px, py, u, t, dx, dy, desc, fSize, fontType, expression_str, trLLUTM, llcolor, layer_bound, trUTMLL): pgrid = QgsPoint(coord_base_x + px * u, coord_base_y + py * t) pgrid.transform(trLLUTM) pgrid = QgsPoint(pgrid.x() + dx, pgrid.y() + dy) if layer_bound.crs().isGeographic() == True: pgrid.transform(trUTMLL) #Label Format Settings settings = QgsPalLayerSettings() settings.placement = 1 settings.isExpression = True textprop = QgsTextFormat() textprop.setColor(llcolor) textprop.setSizeUnit(4) textprop.setSize(fSize * 2.8346) textprop.setFont(QFont(fontType)) textprop.setLineHeight(1) settings.setFormat(textprop) settings.fieldName = expression_str #Label Position settings.geometryGeneratorEnabled = True settings.geometryGenerator = ("make_point({}, {})".format( pgrid.x(), pgrid.y())) datadefined = QgsPropertyCollection() datadefined.property(20).setExpressionString('True') datadefined.property(20).setActive(True) datadefined.property(15).setExpressionString('True') datadefined.property(15).setActive(True) datadefined.property(77).setExpressionString('2') datadefined.property(77).setActive(True) #Creating and Activating Labeling Rule settings.setDataDefinedProperties(datadefined) rule = QgsRuleBasedLabeling.Rule(settings) rule.setDescription(desc) rule.setActive(True) return rule
def text_symbol_to_qgstextformat( text_symbol: TextSymbol, # pylint: disable=too-many-locals,too-many-branches,too-many-statements context, reference_scale=None): """ Converts TextSymbol to QgsTextFormat """ text_format = QgsTextFormat() font = TextSymbolConverter.std_font_to_qfont(text_symbol.font) if context.unsupported_object_callback and font.family( ) not in QFontDatabase().families(): if isinstance(text_symbol, TextSymbol): context.unsupported_object_callback( 'Font {} not available on system'.format( text_symbol.font.font_name)) if Qgis.QGIS_VERSION_INT >= 30900: font.setKerning(text_symbol.kerning) conversion_factor = reference_scale * 0.000352778 if reference_scale is not None else 1 # why 5.55? why not! It's based on rendering match with ArcGIS -- there's no documentation # about what the ArcGIS character spacing value actually means! font.setLetterSpacing( QFont.AbsoluteSpacing, conversion_factor * (text_symbol.character_spacing or 0) / 5.55) # may need tweaking font.setWordSpacing(conversion_factor * ((text_symbol.word_spacing / 100) - 1)) if isinstance(text_symbol, TextSymbol): font.setCapitalization( TextSymbolConverter.CAPITALIZATION_MAP[text_symbol.case]) if Qgis.QGIS_VERSION_INT >= 31600: text_format.setCapitalization( TextSymbolConverter.TEXT_CAPITALIZATION_MAP[ text_symbol.case]) if isinstance(text_symbol, TextSymbol): text_format.setLineHeight(1 + text_symbol.leading / text_symbol.font_size) text_format.setFont(font) if reference_scale is None: text_format.setSize(text_symbol.font_size) text_format.setSizeUnit(QgsUnitTypes.RenderPoints) else: text_format.setSize(text_symbol.font_size * reference_scale * 0.000352778) # todo - use normal map units text_format.setSizeUnit(QgsUnitTypes.RenderMetersInMapUnits) if Qgis.QGIS_VERSION_INT >= 32300 and text_symbol.character_width != 100: text_format.setStretchFactor(text_symbol.character_width) opacity = 1 if isinstance(text_symbol, TextSymbol): color = ColorConverter.color_to_qcolor(text_symbol.color) # need to move opacity setting from color to dedicated setter opacity = color.alphaF() color.setAlpha(255) text_format.setColor(color) text_format.setOpacity(opacity) # shadow if text_symbol.shadow_x_offset or text_symbol.shadow_y_offset: shadow = QgsTextShadowSettings() shadow.setEnabled(True) shadow_color = ColorConverter.color_to_qcolor( text_symbol.shadow_color) # need to move opacity setting from color to dedicated setter shadow_opacity = shadow_color.alphaF() shadow_color.setAlpha(255) shadow.setColor(shadow_color) shadow.setOpacity(shadow_opacity) shadow_angle = math.degrees( math.atan2(text_symbol.shadow_y_offset, text_symbol.shadow_x_offset)) shadow_dist = math.sqrt(text_symbol.shadow_x_offset**2 + text_symbol.shadow_y_offset**2) shadow.setOffsetAngle(int(round(90 - shadow_angle))) if reference_scale is None: shadow.setOffsetDistance(context.convert_size(shadow_dist)) shadow.setOffsetUnit(context.units) else: shadow.setOffsetDistance(shadow_dist * reference_scale * 0.000352778) shadow.setOffsetUnit(QgsUnitTypes.RenderMetersInMapUnits) shadow.setBlendMode(QPainter.CompositionMode_SourceOver) # arc has no option for blurring shadows - we convert with a slight blur (because it's UGLY if we don't, # but use a lower blur then the default to give a somewhat closer match) shadow.setBlurRadius(0.5) text_format.setShadow(shadow) # halo buffer = QgsTextBufferSettings() if isinstance(text_symbol, TextSymbol): buffer.setEnabled(text_symbol.halo_enabled) else: buffer.setEnabled(bool(text_symbol.halo_symbol)) if reference_scale is None: buffer.setSize(context.convert_size(text_symbol.halo_size)) buffer.setSizeUnit(context.units) else: buffer.setSize(2 * text_symbol.halo_size * reference_scale * 0.000352778) buffer.setSizeUnit(QgsUnitTypes.RenderMetersInMapUnits) # QGIS has no option for halo symbols. Instead, we just get the color from the symbol if text_symbol.halo_symbol: from .symbols import SymbolConverter # pylint: disable=import-outside-toplevel,cyclic-import halo_symbol = SymbolConverter.Symbol_to_QgsSymbol( text_symbol.halo_symbol, context) if halo_symbol: buffer_color = halo_symbol.color() # need to move opacity setting from color to dedicated setter buffer_opacity = buffer_color.alphaF() buffer_color.setAlpha(255) buffer.setColor(buffer_color) # in ArcMap buffer inherits text opacity, shadow does not buffer.setOpacity(buffer_opacity * opacity) text_format.setBuffer(buffer) if text_symbol.background_symbol: background = TextSymbolConverter.convert_background_symbol( text_symbol.background_symbol, context, reference_scale) if background: text_format.setBackground(background) if isinstance(text_symbol, TextSymbol): if text_symbol.cjk_orientation: if Qgis.QGIS_VERSION_INT < 30900: if context.unsupported_object_callback: context.unsupported_object_callback( 'Vertical text orientation requires QGIS 3.10 or later', level=Context.WARNING) else: raise NotImplementedException( 'Vertical text orientation requires QGIS 3.10 or later' ) else: text_format.setOrientation( QgsTextFormat.VerticalOrientation) else: pass return text_format