def defStyle(self, layer): # # fs=l.renderer().rootRule().children()[1].symbols()[0] # sl ==> QgsSimpleLineSymbolLayer # sl=fs.symbolLayers()[0] # symbol = QgsSymbol.defaultSymbol(layer.geometryType()) # Exporterle Style (.qml) pour determiner les attributs symb_def = { 'color': '53,227,47,77', 'joinstyle': 'bevel', 'outline_color': '0,0,0,255', 'outline_style': 'dash', 'outline_width': '0.5', 'outline_width_unit': 'MM', 'style': 'solid' } symbol = QgsFillSymbol.createSimple(symb_def) renderer = QgsRuleBasedRenderer(symbol) root_rule = renderer.rootRule() rule = root_rule.children()[0] rule.setLabel("Valide") rule.setFilterExpression( "\"valide\" is not null and \"supprime\" is null") # root_rule.appendChild(rule) symb_def = { 'capstyle': 'square', 'customdash': '5;2', 'customdash_unit': 'MM', 'customdash_unit': '0', 'line_style': 'dash', 'line_color': '200,194,194,255', 'joinstyle': 'bevel', 'line_width': '0.80', 'line_width_unit': 'MM', 'offset': '0', 'use_custom_dash': '1' } symbol = QgsFillSymbol.createSimple(symb_def) symbol.changeSymbolLayer(0, QgsSimpleLineSymbolLayer.create(symb_def)) rule = QgsRuleBasedRenderer.Rule(symbol) rule.setLabel("En attente") rule.setFilterExpression("\"valide\" is null and \"supprime\" is null") root_rule.appendChild(rule) layer.setRenderer(renderer)
def categorize_line_layer(self, layer=None, attr='FID_1', layer_name=None): """ :param attr: :return: """ if not layer: layer = self.get_layer_by_name(layer_name) try: attr_idx = layer.fields().indexFromName(attr) except: print('Could not get attr_idx, using 0.. correct?') attr_idx = 0 unique_values = layer.uniqueValues(attr_idx) categories = [] color_map = {0: '0, 0, 0', 1: '21, 153, 137'} color_map_hex = {0: '#000000', 1: '#159989'} for value in unique_values: symbol = QgsSymbol.defaultSymbol(layer.geometryType()) # configure a symbol layer layer_style = {'color': color_map[value], 'outline': color_map_hex[value]} symbol_layer = QgsSimpleLineSymbolLayer.create(layer_style) # replace default symbol layer with the configured one if symbol_layer is not None: symbol.changeSymbolLayer(0, symbol_layer) # create renderer object category = QgsRendererCategory(value, symbol, str(value)) # entry for the list of category items categories.append(category) # create renderer object renderer = QgsCategorizedSymbolRenderer(attr, categories) # assign the created renderer to the layer if renderer is not None: layer.setRenderer(renderer) layer.triggerRepaint()
def setStyleLayer(self, layer): if self.dlg.checkBox_mask.isChecked(): # only border if layer.geometryType( ) == QgsWkbTypes.PolygonGeometry or layer.geometryType( ) == QgsWkbTypes.LineGeometry: properties = { 'color': self.mColor.name(), 'outline_width': '0.6' } symbol_layer = QgsSimpleLineSymbolLayer.create(properties) layer.renderer().symbols( QgsRenderContext())[0].changeSymbolLayer(0, symbol_layer) elif layer.geometryType() == QgsWkbTypes.PointGeometry: properties = { 'size': '2', 'color': self.mColor.name(), 'outline_color': self.mColor.name(), 'outline_width': '0.6' } symbol_layer = QgsSimpleMarkerSymbolLayer.create(properties) layer.renderer().symbols( QgsRenderContext())[0].changeSymbolLayer(0, symbol_layer) else: if self.colorIndex > (len(self.colorValues) - 1): self.colorIndex = 0 color = QColor(self.colorValues[self.colorIndex]) self.colorIndex += 1 if layer.geometryType() == QgsWkbTypes.PolygonGeometry: layer.renderer().symbols( QgsRenderContext())[0].symbolLayer(0).setStrokeWidth(0.4) layer.renderer().symbols( QgsRenderContext())[0].symbolLayer(0).setBrushStyle( Qt.Dense4Pattern) layer.renderer().symbols( QgsRenderContext())[0].symbolLayer(0).setFillColor(color) layer.setFeatureBlendMode(13) self.iface.layerTreeView().refreshLayerSymbology(layer.id())