def testConvertFromGraduatedRenderer(self): # Test converting graduated renderer to rule based # First, try with a field based category (id) ranges = [] ranges.append(QgsRendererRangeV2(0, 1, QgsMarkerSymbolV2(), "0-1")) ranges.append(QgsRendererRangeV2(1, 2, QgsMarkerSymbolV2(), "1-2")) g = QgsGraduatedSymbolRendererV2("id", ranges) r = QgsRuleBasedRendererV2.convertFromRenderer(g) self.assertEqual(r.rootRule().children()[0].filterExpression(), '"id" >= 0.000000 AND "id" <= 1.000000') self.assertEqual(r.rootRule().children()[1].filterExpression(), '"id" > 1.000000 AND "id" <= 2.000000') # Next try with an expression based range ranges = [] ranges.append(QgsRendererRangeV2(0, 1, QgsMarkerSymbolV2(), "0-1")) ranges.append(QgsRendererRangeV2(1, 2, QgsMarkerSymbolV2(), "1-2")) g = QgsGraduatedSymbolRendererV2("id / 2", ranges) r = QgsRuleBasedRendererV2.convertFromRenderer(g) self.assertEqual(r.rootRule().children()[0].filterExpression(), '(id / 2) >= 0.000000 AND (id / 2) <= 1.000000') self.assertEqual(r.rootRule().children()[1].filterExpression(), '(id / 2) > 1.000000 AND (id / 2) <= 2.000000') # Last try with an expression which is just a quoted field name ranges = [] ranges.append(QgsRendererRangeV2(0, 1, QgsMarkerSymbolV2(), "0-1")) ranges.append(QgsRendererRangeV2(1, 2, QgsMarkerSymbolV2(), "1-2")) g = QgsGraduatedSymbolRendererV2('"id"', ranges) r = QgsRuleBasedRendererV2.convertFromRenderer(g) self.assertEqual(r.rootRule().children()[0].filterExpression(), '"id" >= 0.000000 AND "id" <= 1.000000') self.assertEqual(r.rootRule().children()[1].filterExpression(), '"id" > 1.000000 AND "id" <= 2.000000')
def setUp(self): myShpFile = os.path.join(TEST_DATA_DIR, 'rectangles.shp') layer = QgsVectorLayer(myShpFile, 'Points', 'ogr') QgsMapLayerRegistry.instance().addMapLayer(layer) # Create rulebased style sym1 = QgsFillSymbolV2.createSimple({'color': '#fdbf6f'}) sym2 = QgsFillSymbolV2.createSimple({'color': '#71bd6c'}) sym3 = QgsFillSymbolV2.createSimple({'color': '#1f78b4'}) self.r1 = QgsRuleBasedRendererV2.Rule(sym1, 0, 0, '"id" = 1') self.r2 = QgsRuleBasedRendererV2.Rule(sym2, 0, 0, '"id" = 2') self.r3 = QgsRuleBasedRendererV2.Rule(sym3, 0, 0, 'ELSE') self.rootrule = QgsRuleBasedRendererV2.Rule(None) self.rootrule.appendChild(self.r1) self.rootrule.appendChild(self.r2) self.rootrule.appendChild(self.r3) self.renderer = QgsRuleBasedRendererV2(self.rootrule) layer.setRendererV2(self.renderer) self.mapsettings = CANVAS.mapSettings() self.mapsettings.setOutputSize(QSize(400, 400)) self.mapsettings.setOutputDpi(96) self.mapsettings.setExtent(QgsRectangle(-163, 22, -70, 52)) rendered_layers = [layer.id()] self.mapsettings.setLayers(rendered_layers)
def testConvertFromCategorisedRenderer(self): # Test converting categorised renderer to rule based # First, try with a field based category (id) cats = [] cats.append(QgsRendererCategoryV2(1, QgsMarkerSymbolV2(), "id 1")) cats.append(QgsRendererCategoryV2(2, QgsMarkerSymbolV2(), "id 2")) cats.append( QgsRendererCategoryV2('a\'b', QgsMarkerSymbolV2(), "id a'b")) cats.append( QgsRendererCategoryV2('a\nb', QgsMarkerSymbolV2(), "id a\\nb")) cats.append( QgsRendererCategoryV2('a\\b', QgsMarkerSymbolV2(), "id a\\\\b")) cats.append( QgsRendererCategoryV2('a\tb', QgsMarkerSymbolV2(), "id a\\tb")) c = QgsCategorizedSymbolRendererV2("id", cats) r = QgsRuleBasedRendererV2.convertFromRenderer(c) self.assertEqual(r.rootRule().children()[0].filterExpression(), '"id" = 1') self.assertEqual(r.rootRule().children()[1].filterExpression(), '"id" = 2') self.assertEqual(r.rootRule().children()[2].filterExpression(), '"id" = \'a\'\'b\'') self.assertEqual(r.rootRule().children()[3].filterExpression(), '"id" = \'a\\nb\'') self.assertEqual(r.rootRule().children()[4].filterExpression(), '"id" = \'a\\\\b\'') self.assertEqual(r.rootRule().children()[5].filterExpression(), '"id" = \'a\\tb\'') # Next try with an expression based category cats = [] cats.append(QgsRendererCategoryV2(1, QgsMarkerSymbolV2(), "result 1")) cats.append(QgsRendererCategoryV2(2, QgsMarkerSymbolV2(), "result 2")) c = QgsCategorizedSymbolRendererV2("id + 1", cats) r = QgsRuleBasedRendererV2.convertFromRenderer(c) self.assertEqual(r.rootRule().children()[0].filterExpression(), 'id + 1 = 1') self.assertEqual(r.rootRule().children()[1].filterExpression(), 'id + 1 = 2') # Last try with an expression which is just a quoted field name cats = [] cats.append(QgsRendererCategoryV2(1, QgsMarkerSymbolV2(), "result 1")) cats.append(QgsRendererCategoryV2(2, QgsMarkerSymbolV2(), "result 2")) c = QgsCategorizedSymbolRendererV2('"id"', cats) r = QgsRuleBasedRendererV2.convertFromRenderer(c) self.assertEqual(r.rootRule().children()[0].filterExpression(), '"id" = 1') self.assertEqual(r.rootRule().children()[1].filterExpression(), '"id" = 2')
def certificate_preprocess(plot, plots): """ Utility function that loads and renders plots that belong to a specific scheme. """ scheme_plot_layer = lht_plot_layer(plot.scheme_id, CERTIFICATE_PLOT) QgsMapLayerRegistry.instance().addMapLayer(scheme_plot_layer) # Get the EPSG code of the plot epsg_code = plot.cb_check_lht_plot_crs.value # Setting the project CRS variable QgsExpressionContextUtils.setProjectVariable('flts_source_crs', epsg_code) # Styling reference plot using primary key filter_exp = '"id" = ' + str(plot.id) scheme_symbol = QgsSymbolV2.defaultSymbol(scheme_plot_layer.geometryType()) # Rule-based rendering rule_renderer = QgsRuleBasedRendererV2(scheme_symbol) root_rule = rule_renderer.rootRule() # Rule for highlighting reference plot scheme_rule = root_rule.children()[0].clone() scheme_rule.setLabel('Reference Plot') scheme_rule.setFilterExpression(filter_exp) scheme_symbol_layer = scheme_rule.symbol().symbolLayer(0) scheme_symbol_layer.setFillColor(Qt.yellow) scheme_symbol_layer.setOutlineColor(Qt.black) scheme_symbol_layer.setBorderWidth(0.5) root_rule.appendChild(scheme_rule) # Rule for other plots def_rule = root_rule.children()[0].clone() def_rule.setLabel('Plots') def_rule.setIsElse(True) def_symbol_layer = def_rule.symbol().symbolLayer(0) def_symbol_layer.setFillColor(Qt.transparent) def_symbol_layer.setOutlineColor(Qt.black) root_rule.appendChild(def_rule) # Remove default rule root_rule.removeChildAt(0) # Set renderer scheme_plot_layer.setRendererV2(rule_renderer) # Enable labeling scheme_plot_layer.setCustomProperty("labeling", "pal") scheme_plot_layer.setCustomProperty("labeling/enabled", "true") scheme_plot_layer.setCustomProperty("labeling/fontFamily", "Arial") scheme_plot_layer.setCustomProperty("labeling/fontSize", "5.5") scheme_plot_layer.setCustomProperty("labeling/fieldName", "plot_number") scheme_plot_layer.setCustomProperty("labeling/placement", "1") scheme_plot_layer.setCustomProperty("labeling/centroidInside", "true") scheme_plot_layer.setCustomProperty("labeling/centroidWhole", "false") scheme_plot_layer.triggerRepaint() iface.mapCanvas().setExtent(scheme_plot_layer.extent()) QgsApplication.processEvents() return True
def setStyleGrilleControle(layerGrille, idList): # Symbologie: cellule a controler props1 = {'color': '241,241,241,0', 'size': '0', 'color_border': '255,0,0'} symbol1 = QgsFillSymbolV2.createSimple(props1) #props2 = {'color': '255,127,0,0', 'size':'0', 'color_border' : '255,127,0', 'width_border':'1'} #symbol2 = QgsFillSymbolV2.createSimple(props2) # Symbologie: cellule a griser props3 = { 'color': '180,180,180', 'size': '1', 'color_border': '180,180,180' } symbol3 = QgsFillSymbolV2.createSimple(props3) symbol3.setAlpha(0.70) # On definit les règles de symbologie txtRule = ' in (' for i in range(len(idList)): id = idList[i] txtRule = txtRule + str(id) + ', ' txtRule = txtRule[0:len(txtRule) - 2] txtRule = txtRule + ')' cell_rules = (('A controler', CONST_ATTRIBUT_ID + ' ' + txtRule, symbol1), ('Pass', CONST_ATTRIBUT_ID + ' not ' + txtRule, symbol3)) # create a new rule-based renderer symbol = QgsSymbolV2.defaultSymbol(layerGrille.geometryType()) renderer = QgsRuleBasedRendererV2(symbol) # get the "root" rule root_rule = renderer.rootRule() for label, expression, symbol in cell_rules: # create a clone (i.e. a copy) of the default rule rule = root_rule.children()[0].clone() # set the label, expression and color rule.setLabel(label) rule.setFilterExpression(expression) # rule.symbol().setColor(QColor(color_name)) rule.setSymbol(symbol) # append the rule to the list of rules root_rule.appendChild(rule) # delete the default rule root_rule.removeChildAt(0) # apply the renderer to the layer layerGrille.setRendererV2(renderer) return layerGrille
def testRefineWithRanges(self): #Test refining rule with ranges (refs #10815) #First, try with a field based category (id) ranges = [] ranges.append(QgsRendererRangeV2(0, 1, QgsMarkerSymbolV2(), "0-1")) ranges.append(QgsRendererRangeV2(1, 2, QgsMarkerSymbolV2(), "1-2")) g = QgsGraduatedSymbolRendererV2("id", ranges) QgsRuleBasedRendererV2.refineRuleRanges(self.r2, g) assert self.r2.children()[0].filterExpression() == '"id" >= 0.0000 AND "id" <= 1.0000' assert self.r2.children()[1].filterExpression() == '"id" > 1.0000 AND "id" <= 2.0000' #Next try with an expression based range ranges = [] ranges.append(QgsRendererRangeV2(0, 1, QgsMarkerSymbolV2(), "0-1")) ranges.append(QgsRendererRangeV2(1, 2, QgsMarkerSymbolV2(), "1-2")) g = QgsGraduatedSymbolRendererV2("id / 2", ranges) QgsRuleBasedRendererV2.refineRuleRanges(self.r1, g) assert self.r1.children()[0].filterExpression() == '(id / 2) >= 0.0000 AND (id / 2) <= 1.0000' assert self.r1.children()[1].filterExpression() == '(id / 2) > 1.0000 AND (id / 2) <= 2.0000' #Last try with an expression which is just a quoted field name ranges = [] ranges.append(QgsRendererRangeV2(0, 1, QgsMarkerSymbolV2(), "0-1")) ranges.append(QgsRendererRangeV2(1, 2, QgsMarkerSymbolV2(), "1-2")) g = QgsGraduatedSymbolRendererV2('"id"', ranges) QgsRuleBasedRendererV2.refineRuleRanges(self.r3, g) assert self.r3.children()[0].filterExpression() == '"id" >= 0.0000 AND "id" <= 1.0000' assert self.r3.children()[1].filterExpression() == '"id" > 1.0000 AND "id" <= 2.0000'
def testRefineWithCategories(self): # Test refining rule with categories (refs #10815) # First, try with a field based category (id) cats = [] cats.append(QgsRendererCategoryV2(1, QgsMarkerSymbolV2(), "id 1")) cats.append(QgsRendererCategoryV2(2, QgsMarkerSymbolV2(), "id 2")) c = QgsCategorizedSymbolRendererV2("id", cats) QgsRuleBasedRendererV2.refineRuleCategories(self.r2, c) assert self.r2.children()[0].filterExpression() == '"id" = 1' assert self.r2.children()[1].filterExpression() == '"id" = 2' # Next try with an expression based category cats = [] cats.append(QgsRendererCategoryV2(1, QgsMarkerSymbolV2(), "result 1")) cats.append(QgsRendererCategoryV2(2, QgsMarkerSymbolV2(), "result 2")) c = QgsCategorizedSymbolRendererV2("id + 1", cats) QgsRuleBasedRendererV2.refineRuleCategories(self.r1, c) assert self.r1.children()[0].filterExpression() == 'id + 1 = 1' assert self.r1.children()[1].filterExpression() == 'id + 1 = 2' # Last try with an expression which is just a quoted field name cats = [] cats.append(QgsRendererCategoryV2(1, QgsMarkerSymbolV2(), "result 1")) cats.append(QgsRendererCategoryV2(2, QgsMarkerSymbolV2(), "result 2")) c = QgsCategorizedSymbolRendererV2('"id"', cats) QgsRuleBasedRendererV2.refineRuleCategories(self.r3, c) assert self.r3.children()[0].filterExpression() == '"id" = 1' assert self.r3.children()[1].filterExpression() == '"id" = 2'
def testConvertFromCategorisedRenderer(self): # Test converting categorised renderer to rule based # First, try with a field based category (id) cats = [] cats.append(QgsRendererCategoryV2(1, QgsMarkerSymbolV2(), "id 1")) cats.append(QgsRendererCategoryV2(2, QgsMarkerSymbolV2(), "id 2")) cats.append(QgsRendererCategoryV2('a\'b', QgsMarkerSymbolV2(), "id a'b")) cats.append(QgsRendererCategoryV2('a\nb', QgsMarkerSymbolV2(), "id a\\nb")) cats.append(QgsRendererCategoryV2('a\\b', QgsMarkerSymbolV2(), "id a\\\\b")) cats.append(QgsRendererCategoryV2('a\tb', QgsMarkerSymbolV2(), "id a\\tb")) c = QgsCategorizedSymbolRendererV2("id", cats) r = QgsRuleBasedRendererV2.convertFromRenderer(c) self.assertEqual(r.rootRule().children()[0].filterExpression(), '"id" = 1') self.assertEqual(r.rootRule().children()[1].filterExpression(), '"id" = 2') self.assertEqual(r.rootRule().children()[2].filterExpression(), '"id" = \'a\'\'b\'') self.assertEqual(r.rootRule().children()[3].filterExpression(), '"id" = \'a\\nb\'') self.assertEqual(r.rootRule().children()[4].filterExpression(), '"id" = \'a\\\\b\'') self.assertEqual(r.rootRule().children()[5].filterExpression(), '"id" = \'a\\tb\'') # Next try with an expression based category cats = [] cats.append(QgsRendererCategoryV2(1, QgsMarkerSymbolV2(), "result 1")) cats.append(QgsRendererCategoryV2(2, QgsMarkerSymbolV2(), "result 2")) c = QgsCategorizedSymbolRendererV2("id + 1", cats) r = QgsRuleBasedRendererV2.convertFromRenderer(c) self.assertEqual(r.rootRule().children()[0].filterExpression(), 'id + 1 = 1') self.assertEqual(r.rootRule().children()[1].filterExpression(), 'id + 1 = 2') # Last try with an expression which is just a quoted field name cats = [] cats.append(QgsRendererCategoryV2(1, QgsMarkerSymbolV2(), "result 1")) cats.append(QgsRendererCategoryV2(2, QgsMarkerSymbolV2(), "result 2")) c = QgsCategorizedSymbolRendererV2('"id"', cats) r = QgsRuleBasedRendererV2.convertFromRenderer(c) self.assertEqual(r.rootRule().children()[0].filterExpression(), '"id" = 1') self.assertEqual(r.rootRule().children()[1].filterExpression(), '"id" = 2')
def testRefineWithRanges(self): # Test refining rule with ranges (refs #10815) # First, try with a field based category (id) ranges = [] ranges.append(QgsRendererRangeV2(0, 1, QgsMarkerSymbolV2(), "0-1")) ranges.append(QgsRendererRangeV2(1, 2, QgsMarkerSymbolV2(), "1-2")) g = QgsGraduatedSymbolRendererV2("id", ranges) QgsRuleBasedRendererV2.refineRuleRanges(self.r2, g) assert self.r2.children()[0].filterExpression() == '"id" >= 0.0000 AND "id" <= 1.0000' assert self.r2.children()[1].filterExpression() == '"id" > 1.0000 AND "id" <= 2.0000' # Next try with an expression based range ranges = [] ranges.append(QgsRendererRangeV2(0, 1, QgsMarkerSymbolV2(), "0-1")) ranges.append(QgsRendererRangeV2(1, 2, QgsMarkerSymbolV2(), "1-2")) g = QgsGraduatedSymbolRendererV2("id / 2", ranges) QgsRuleBasedRendererV2.refineRuleRanges(self.r1, g) assert self.r1.children()[0].filterExpression() == '(id / 2) >= 0.0000 AND (id / 2) <= 1.0000' assert self.r1.children()[1].filterExpression() == '(id / 2) > 1.0000 AND (id / 2) <= 2.0000' # Last try with an expression which is just a quoted field name ranges = [] ranges.append(QgsRendererRangeV2(0, 1, QgsMarkerSymbolV2(), "0-1")) ranges.append(QgsRendererRangeV2(1, 2, QgsMarkerSymbolV2(), "1-2")) g = QgsGraduatedSymbolRendererV2('"id"', ranges) QgsRuleBasedRendererV2.refineRuleRanges(self.r3, g) assert self.r3.children()[0].filterExpression() == '"id" >= 0.0000 AND "id" <= 1.0000' assert self.r3.children()[1].filterExpression() == '"id" > 1.0000 AND "id" <= 2.0000'
def __getRuleBasedRenderer(self): """docstring for __getRuleBasedRenderer""" root_rule = QgsRuleBasedRendererV2.Rule(None) n = (len(self.expressions) - 1) i = 0 for exp, msclass in zip(self.expressions[::-1], self.mslayer["classes"][::-1]): Symbol = SymbolImport(msclass, self.qgslayer, self.geom_type, self.sizeunits, \ self.mssymbols, self.symbolsetpath, self.fontset) symbol = Symbol.getSymbol() scalemindenom = self.__getScaledenom(msclass, 'minscaledenom', 'styles') scalemaxdenom = self.__getScaledenom(msclass, 'maxscaledenom', 'styles') label = msclass.get("name", exp[1]) state = False if msclass.get("status", 'on').lower() == 'off' else True filterexp = '' if exp[0] == Expression.TYPE_STRING and self.has_classitem: filterexp = "\"{}\" = '{}'".format(self.has_classitem, exp[1]) elif exp[0] == Expression.TYPE_LIST or exp[ 0] == Expression.TYPE_BETWEEN or exp[ 0] == Expression.TYPE_LOGICAL: filterexp = exp[1] description = '' #Si no tiene expresion y si es la ultima regla setear a True, tener en cuenta que se puede declarar al reves #elserule = True if exp[0] == 'unknown' and exp[1] == '' and i == n else False elserule = False rule = QgsRuleBasedRendererV2.Rule(symbol, scalemindenom, scalemaxdenom, filterexp, label, description, elserule) rule.setActive(state) root_rule.appendChild(rule) i += 1 return QgsRuleBasedRendererV2(root_rule)
def setStyleGrilleSaisie(layerGrille, currid): props1 = { 'color': '241,241,241,0', 'size': '0', 'color_border': '255,0,0', 'width_border': '0.5' } symbol1 = QgsFillSymbolV2.createSimple(props1) props2 = {'color': '255,127,0,0', 'size': '0', 'color_border': '255,127,0'} symbol2 = QgsFillSymbolV2.createSimple(props2) # On definit les règles de symbologie cell_rules = (('Cellule en cours', CONST_ATTRIBUT_ID + ' = ' + str(currid), symbol1), ('Autre cellule', CONST_ATTRIBUT_ID + ' != ' + str(currid), symbol2)) # create a new rule-based renderer symbol = QgsSymbolV2.defaultSymbol(layerGrille.geometryType()) renderer = QgsRuleBasedRendererV2(symbol) # get the "root" rule root_rule = renderer.rootRule() for label, expression, symbol in cell_rules: # create a clone (i.e. a copy) of the default rule rule = root_rule.children()[0].clone() # set the label, expression and color rule.setLabel(label) rule.setFilterExpression(expression) # rule.symbol().setColor(QColor(color_name)) rule.setSymbol(symbol) # append the rule to the list of rules root_rule.appendChild(rule) # delete the default rule root_rule.removeChildAt(0) # apply the renderer to the layer layerGrille.setRendererV2(renderer) return layerGrille
def make_OD_markers(nb, xo, yo, xd, yd, list_coords=None): """ Prepare the Origin (green), Destination (red) and Intermediates (grey) markers. """ OD_layer = QgsVectorLayer( "Point?crs=epsg:4326&field=id_route:integer&field=role:string(80)", "markers_osrm{}".format(nb), "memory") features = [] fet = QgsFeature() fet.setGeometry(QgsGeometry.fromPoint(QgsPoint(float(xo), float(yo)))) fet.setAttributes([nb, 'Origin']) features.append(fet) fet = QgsFeature() fet.setGeometry(QgsGeometry.fromPoint(QgsPoint(float(xd), float(yd)))) fet.setAttributes([nb, 'Destination']) features.append(fet) marker_rules = [ ('Origin', '"role" LIKE \'Origin\'', '#50b56d', 4), ('Destination', '"role" LIKE \'Destination\'', '#d31115', 4), ] if list_coords: for i, pt in enumerate(list_coords): fet = QgsFeature() fet.setGeometry( QgsGeometry.fromPoint(QgsPoint(float(pt[0]), float(pt[1])))) fet.setAttributes([nb, 'Via point n°{}'.format(i)]) features.append(fet) marker_rules.insert( 1, ('Intermediate', '"role" LIKE \'Via point%\'', 'grey', 2)) OD_layer.dataProvider().addFeatures(features) symbol = QgsSymbolV2.defaultSymbol(OD_layer.geometryType()) renderer = QgsRuleBasedRendererV2(symbol) root_rule = renderer.rootRule() for label, expression, color_name, size in marker_rules: rule = root_rule.children()[0].clone() rule.setLabel(label) rule.setFilterExpression(expression) rule.symbol().setColor(QtGui.QColor(color_name)) rule.symbol().setSize(size) root_rule.appendChild(rule) root_rule.removeChildAt(0) OD_layer.setRendererV2(renderer) return OD_layer
def Mod3Render(iface, styleManager, parent=None): cellLayer = getLayerByName(u"小区", iface) # 模三规则 rules = (('0', ' "PCI" % 3 = 0', 'red'), ('1', ' "PCI" % 3 = 1', 'yellow'), ('2', ' "PCI" % 3 = 2', '#47d54c')) sym_pci = QgsSymbolV2.defaultSymbol( cellLayer.geometryType()) # 小区feature重置默认样式 rend_pci = QgsRuleBasedRendererV2(sym_pci) # 设置为基于规则样式 root_rule = rend_pci.rootRule() for label, exp, color, in rules: # 根据规则渲染 rule = root_rule.children()[0].clone() rule.setLabel(label) rule.setFilterExpression(exp) rule.symbol().setColor(QColor(color)) root_rule.appendChild(rule) cellLayer.setRendererV2(rend_pci) iface.actionDraw().trigger() legend = Mod3Legend(iface, styleManager, parent) legend.show() legend.exec_()
def get_style(self): """Get map styles. Get Fill color, label font, outline color. This function takes layer as input and configures style dictionary which is sent as HTTP request in order to adequatly represent map style on GIS Cloud. """ LOGGER.debug('Started map_styles function') if ISQGIS3: self.scale_pixels = \ iface.mapCanvas().mapSettings().outputDpi() / 72 else: self.scale_pixels = \ iface.mapCanvas().mapRenderer().outputDpi() / 72 self.unit_to_px = { "MM": 3.78 * self.scale_pixels, "Point": 1.33 * self.scale_pixels, "Inch": 96 * self.scale_pixels, # these two aren't yet supported by GC rendering, # so defaulting them to value of 1 px "MapUnit": None, "RenderMetersInMapUnits": None } layer_fromlevel = 0 layer_tolevel = 0 if self.qgis_layer.hasScaleBasedVisibility(): dpi = iface.mainWindow().physicalDpiX() max_scale_per_pixel = 156543.04 inches_per_meter = 39.37 factor = dpi * inches_per_meter * max_scale_per_pixel if self.qgis_layer.minimumScale() > 0: layer_fromlevel = int( round( math.log((factor / self.qgis_layer.minimumScale()), 2), 0)) if self.qgis_layer.maximumScale() > 0: layer_tolevel = int( round( math.log((factor / self.qgis_layer.maximumScale()), 2), 0)) if not ISQGIS3: # QGis2 has oposite logic with min/max scales # so we need to switch them (layer_tolevel, layer_fromlevel) = \ (layer_fromlevel, layer_tolevel) styles = [] tmp_dir = self.gc_api.qgis_api.tmp_dir if ISQGIS3: renderer = QgsRuleBasedRenderer.convertFromRenderer( self.qgis_layer.renderer()) else: renderer = QgsRuleBasedRendererV2.convertFromRenderer( self.qgis_layer.rendererV2()) for rule in renderer.rootRule().children(): symbol = rule.symbol() sym_size = 0 if self.layer.type[0] == "point": for layer_sym in symbol.symbolLayers(): temp_style = layer_sym.properties() self.convert_units_to_px(temp_style) if "size" in temp_style and \ float(temp_style["size"]) > sym_size: sym_size = float(temp_style["size"]) is_first_sym = True index = symbol.symbolLayerCount() while index > 0: index = index - 1 layer_sym = symbol.symbolLayer(index) temp_style = layer_sym.properties() self.convert_units_to_px(temp_style) val_label = None # in case of multiple symbolLayers() # labels should be set only once if is_first_sym: if ISQGIS3: if self.qgis_layer.labelsEnabled(): val_label = self.qgis_layer.labeling().settings() else: val_label = QgsPalLayerSettings() val_label.readFromLayer(self.qgis_layer) style = {} line_style = "line_style" line_width = 0 if self.layer.type[0] == "point": size = int(round(sym_size)) + 2 md5 = hashlib.md5() properties = str(temp_style) + self.dump_symbol_properties( layer_sym.subSymbol()) md5.update(properties.encode('utf-8')) symbol_file = "{}_{}.png".format(self.layer.id, md5.hexdigest()) style['iconsoverlap'] = 2 style['url'] = { "full_path": tmp_dir + '/' + symbol_file, "file": symbol_file, "symbol": symbol.clone(), "size": QSize(size, size) } elif self.layer.type[0] == "line": LOGGER.info('entered line_type part of function') LOGGER.info(temp_style) try: if u'line_color' in temp_style: style['color'] = ','.join( temp_style[u'line_color'].split(',')[0:3]) style['bordercolor'] = style['color'] if u'line_width' in temp_style: style['width'] = temp_style[u'line_width'] else: style['width'] = '1' line_width = float(style['width']) except Exception: LOGGER.info( 'Failed while mapping style for line vector layer', exc_info=True) if ('color' or 'bordercolor') not in style: style['color'] = '0,0,0' style['bordercolor'] = '0,0,0' LOGGER.info('Style is{}'.format(style)) # VectorPolygonLayer styles -> dashed line # and offset possibilities elif self.layer.type[0] == "polygon": line_style = "outline_style" has_border = not ("outline_style" in temp_style and temp_style["outline_style"] == "no") if layer_sym.layerType() == 'SimpleFill': if u'outline_color' in temp_style and has_border: style['bordercolor'] = \ ','.join( temp_style[u'outline_color'] .split(',')[0:3]) if u'outline_width' in temp_style and has_border: style['borderwidth'] = temp_style[u'outline_width'] if u'color' in temp_style and \ "style" in temp_style and \ temp_style["style"] == "solid": style['color'] = ','.join( temp_style[u'color'].split(',')[0:3]) elif layer_sym.layerType() == 'SimpleLine': if u'line_color' in temp_style: style['bordercolor'] = \ ','.join( temp_style[u'line_color'] .split(',')[0:3]) if u'line_width' in temp_style: style['line_width'] = temp_style[u'line_width'] elif u'color1' in temp_style: style['color'] = ','.join( temp_style[u'color1'].split(',')[0:3]) style['borderwidth'] = '1' if has_border: style['bordercolor'] = '0,0,0' else: style['bordercolor'] = '0,0,0' if has_border: style['borderwidth'] = '1' style['color'] = '0,0,0' if "borderwidth" in style: line_width = float(style['borderwidth']) if (layer_sym.layerType() != "SimpleFill" and layer_sym.layerType() != "SimpleLine") or \ ("style" in temp_style and not temp_style["style"] in ["solid", "no"]): if layer_sym.layerType() != "SimpleFill": temp_symbol = symbol.clone() tmp_sym_layer = temp_symbol.symbolLayer(index) while temp_symbol.symbolLayerCount() > 1: if temp_symbol.symbolLayer(0) == tmp_sym_layer: temp_symbol.deleteSymbolLayer(1) else: temp_symbol.deleteSymbolLayer(0) else: temp_style_hatch = temp_style.copy() temp_style_hatch["outline_style"] = "no" if ISQGIS3: temp_symbol = QgsFillSymbol.createSimple( temp_style_hatch) else: temp_symbol = QgsFillSymbolV2.createSimple( temp_style_hatch) properties = self.dump_symbol_properties(temp_symbol) md5 = hashlib.md5() md5.update(properties.encode('utf-8')) symbol_file = "{}_{}.png"\ .format(self.layer.id, md5.hexdigest()) style['hatchUrl'] = { "full_path": tmp_dir + '/' + symbol_file, "file": symbol_file, "symbol": temp_symbol, "size": QSize(64, 64) } if "use_custom_dash" in temp_style and \ temp_style["use_custom_dash"] == '1': style['dashed'] = temp_style[u'customdash'].replace( ';', ',') if ("dashed" not in style and line_style in temp_style and not temp_style[line_style] in ["solid", "no"]): process_dash_param(temp_style[line_style], line_width, style) if ISQGIS3: if val_label is not None: label_format = val_label.format() style['fontsize'] = label_format.size() style['labelfield'] = val_label.fieldName.lower() style['fontcolor'] = \ rgb_int2tuple(label_format.color().rgb()) if label_format.buffer().enabled(): style['outline'] = \ rgb_int2tuple( label_format.buffer().color().rgb()) if self.qgis_layer.geometryType() == 1: style['labelfield'] = '' style['textfield'] = val_label.fieldName.lower() if str(label_format.font().family()) in \ self.supported_fonts: style['fontname'] = label_format.font().family() else: style['fontname'] = 'Arial' LOGGER.info( ("Choosen font is not supported, " + "so every font style has been changed " + "to {0}").format(style['fontname'])) self.setup_label_offset(val_label, style) else: if val_label is not None and val_label.enabled: style['fontsize'] = val_label.textFont.pointSize() style['labelfield'] = val_label.fieldName.lower() style['fontcolor'] = rgb_int2tuple( val_label.textColor.rgb()) if val_label.bufferDraw: style['outline'] = rgb_int2tuple( val_label.bufferColor.rgb()) if self.qgis_layer.geometryType() == 1: style['labelfield'] = '' style['textfield'] = val_label.fieldName.lower() if str(val_label.textFont.family()) in \ self.supported_fonts: style['fontname'] = val_label.textFont.family() else: style['fontname'] = 'Arial' LOGGER.info("Choosen font is not supported, so " + "every font style has been changed " + " to {0}".format(style['fontname'])) self.setup_label_offset(val_label, style) if rule.filterExpression(): style['expression'] = rule.filterExpression().replace( '"', '') expression = self.qgis_layer.subsetString().replace('"', '') if expression and expression != '': if 'expression' in style and style['expression'] != '': style['expression'] = "(" + \ style['expression'] + \ ") AND (" + expression + ")" else: style['expression'] = expression if rule.label(): style['label'] = rule.label() style['showlabel'] = 't' \ if val_label is not None and \ 'labelfield' in style \ else 'f' style['visible'] = '1' if self.qgis_layer.hasScaleBasedVisibility(): factor = dpi * inches_per_meter * max_scale_per_pixel if ISQGIS3 and rule.minimumScale() > 0: style['fromlevel'] = \ int(round( math.log((factor / rule.minimumScale()), 2), 0)) elif layer_fromlevel > 0: style['fromlevel'] = layer_fromlevel if ISQGIS3 and rule.maximumScale() > 0: style['tolevel'] = \ int(round( math.log((factor / rule.maximumScale()), 2), 0)) elif layer_tolevel > 0: style['tolevel'] = layer_tolevel if 'borderwidth' in styles and \ style['borderwidth'] and \ float(style['borderwidth']) < 1: style['borderwidth'] = '1' key = "hatchUrl" if "hatchUrl" in style else "url" if key in style: asset = style[key] self.layer.assets.append(asset) LOGGER.info('URL for image upload: {}'.format( asset["file"])) style[key] = '/{}/qgis/map{}/{}'.format( self.gc_api.user.user_md5, self.gc_api.map.map_id, asset["file"]) styles.append(style) is_first_sym = False # all point styles are merged into one as we export the symbol # so it's not required to iterrate symbolLayers() if self.layer.type[0] == "point": break LOGGER.info('Styles function output {}'.format(styles)) LOGGER.debug('Finished map_styles function') return styles
{ "color": "#000000", "color_border": "#000000", "width_border": "0.25", "style": "no", } ) highlight_style = QgsFillSymbolV2().createSimple( { "color": "#000000", "color_border": "#ff0000", "width_border": "0.85", "style": "no", } ) highlight_rule = QgsRuleBasedRendererV2.Rule(highlight_style) highlight_rule.setFilterExpression("$id = @atlas_featureid") highlight_renderer = QgsRuleBasedRendererV2(normal_style) highlight_renderer.rootRule().appendChild(highlight_rule) original_renderer = vector_layer.rendererV2() vector_layer.setRendererV2(highlight_renderer) vector_layer.triggerRepaint() # Produce the reports atlas.beginRender() for i in range(atlas.numFeatures()): atlas.prepareForFeature(i) region_id = str(atlas.feature().attribute("aoi_id")) if region_id not in report_tables.keys(): continue styled_df = report_tables[region_id].style.format("{:.2f}")
def extract_layers(self, tree): """Return a list of RFU layers.""" # Create vector layers.. l_vertex = QgsVectorLayer(r"Point?crs=epsg:4326&index=yes", u"Sommet RFU", r"memory") l_edge = QgsVectorLayer(r"LineString?crs=epsg:4326&index=yes", u"Limite RFU", r"memory") p_vertex = l_vertex.dataProvider() p_edge = l_edge.dataProvider() # Define default style renderer.. renderer_vertex = QgsRuleBasedRendererV2(QgsMarkerSymbolV2()) vertex_root_rule = renderer_vertex.rootRule() vertex_rules = ( ((u"Borne, borne à puce, pierre, piquet, clou ou broche"), (u"$id >= 0 AND \"som_nature\" IN ('Borne'," u"'Borne à puce', 'Pierre', 'Piquet', 'Clou ou broche')"), r"#EC0000", 2.2), ((u"Axe cours d'eau, axe fossé, haut de talus, pied de talus"), (u"$id >= 0 AND \"som_nature\" IN ('Axe cours d\'\'eau'," u"'Axe fossé', 'Haut de talus', 'Pied de talus')"), r"#EE8012", 2.2), ((u"Angle de bâtiment, axe de mur, angle de mur, " u"angle de clôture, pylône et toute autre valeur"), (u"$id >= 0 AND \"som_nature\" NOT IN ('Borne'," u"'Borne à puce', 'Pierre', 'Piquet', 'Clou ou broche'," u"'Axe cours d\'\'eau', 'Axe fossé', 'Haut de talus'," u"'Pied de talus')"), r"#9784EC", 2.2), (u"Temporaire", r"$id < 0", "cyan", 2.4)) for label, expression, color, size in vertex_rules: rule = vertex_root_rule.children()[0].clone() rule.setLabel(label) rule.setFilterExpression(expression) rule.symbol().setColor(QColor(color)) rule.symbol().setSize(size) vertex_root_rule.appendChild(rule) vertex_root_rule.removeChildAt(0) l_vertex.setRendererV2(renderer_vertex) renderer_edge = QgsRuleBasedRendererV2(QgsLineSymbolV2()) edge_root_rule = renderer_edge.rootRule() edge_rules = ((r"Limite", r"$id >= 0", "#0A0AFF", 0.5), (r"Temporaire", r"$id < 0", "cyan", 1)) for label, expression, color, width in edge_rules: rule = edge_root_rule.children()[0].clone() rule.setLabel(label) rule.setFilterExpression(expression) rule.symbol().setColor(QColor(color)) rule.symbol().setWidth(width) edge_root_rule.appendChild(rule) edge_root_rule.removeChildAt(0) l_edge.setRendererV2(renderer_edge) # Add fields.. p_vertex.addAttributes([ QgsField(r"@id_noeud", QVariant.Int), # QgsField(r"@changeset", QVariant.Int), # QgsField(r"@timestamp", QVariant.Date), QgsField(r"@version", QVariant.Int), QgsField(r"som_ge_createur", QVariant.String), QgsField(r"som_nature", QVariant.String), QgsField(r"som_precision_rattachement", QVariant.Int), QgsField(r"som_coord_est", QVariant.Double), QgsField(r"som_coord_nord", QVariant.Double), QgsField(r"som_representation_plane", QVariant.String), # QgsField(r"date_creation", QVariant.Date) ]) p_edge.addAttributes([ QgsField(r"@id_arc", QVariant.Int), # QgsField(r"@id_noeud_debut", QVariant.Int), # QgsField(r"@id_noeud_fin", QVariant.Int), # QgsField(r"@changeset", QVariant.Int), # QgsField(r"@timestamp", QVariant.Date), QgsField(r"@version", QVariant.Int), QgsField(r"lim_ge_createur", QVariant.String), # QgsField(r"lim_date_creation", QVariant.Date) ]) # Add features from xml tree.. # ..to vertex layer.. fts_vertex = [] for e in tree.findall(r"sommet"): ft_vertex = QgsFeature() ft_vertex.setGeometry(QgsGeometry.fromWkt(e.attrib[r"geometrie"])) _id_noeud = int(e.attrib[r"id_noeud"]) # _changeset = int(e.attrib[r"changeset"]) # _timestamp = QDateTime(datetime.strptime( # e.attrib[r"timestamp"], r"%Y-%m-%d %H:%M:%S.%f")) _version = int(e.attrib[r"version"]) som_ge_createur = unicode(e.find(r"./som_ge_createur").text) som_nature = unicode(e.find(r"./som_nature").text) som_prec_rattcht = int( e.find(r"./som_precision_rattachement").text) som_coord_est = float(e.find(r"./som_coord_est").text) som_coord_nord = float(e.find(r"./som_coord_nord").text) som_repres_plane = unicode( e.find(r"./som_representation_plane").text) # som_date_creation = QDate(datetime.strptime( # e.find(r"./som_date_creation").text, r"%Y-%m-%d").date()) ft_vertex.setAttributes([ _id_noeud, # _changeset, # _timestamp, _version, som_ge_createur, som_nature, som_prec_rattcht, som_coord_est, som_coord_nord, som_repres_plane, # som_date_creation ]) fts_vertex.append(ft_vertex) # ..to edge layer.. fts_edge = [] for e in tree.findall(r"limite"): ft_edge = QgsFeature() ft_edge.setGeometry(QgsGeometry.fromWkt(e.attrib[r"geometrie"])) _id_arc = int(e.attrib[r"id_arc"]) # _id_noeud_debut = int(e.attrib[r"id_noeud_debut"]) # _id_noeud_fin = int(e.attrib[r"id_noeud_fin"]) # _changeset = int(e.attrib[r"changeset"]) # _timestamp = QDateTime(datetime.strptime( # e.attrib[r"timestamp"], r"%Y-%m-%d %H:%M:%S.%f")) _version = int(e.attrib[r"version"]) lim_ge_createur = unicode(e.find(r"./lim_ge_createur").text) # lim_date_creation = QDate(datetime.strptime( # e.find(r"./lim_date_creation").text, r"%Y-%m-%d").date()) ft_edge.setAttributes([ _id_arc, # _id_noeud_debut, # _id_noeud_fin, # _changeset, # _timestamp, _version, lim_ge_createur, # lim_date_creation ]) fts_edge.append(ft_edge) # Add features to layers.. p_vertex.addFeatures(fts_vertex) p_edge.addFeatures(fts_edge) # Update fields.. l_vertex.updateFields() l_edge.updateFields() # Update layer's extent.. l_vertex.updateExtents() l_edge.updateExtents() # Check if valid.. if not l_vertex.isValid() or not l_edge.isValid(): raise Exception( u"Une erreur est survenue lors du chargement de la couche.") # Set labelling... palyr = QgsPalLayerSettings() palyr.enabled = True # palyr.readFromLayer(l_vertex) palyr.fieldName = r"$id" # Expression $id palyr.placement = 1 # ::OverPoint palyr.quadOffset = 2 # ::QuadrantAboveRight palyr.setDataDefinedProperty(80, True, True, r"1", "") # ::OffsetUnits -> ::MM palyr.xOffset = 2.0 palyr.yOffset = -1.0 palyr.writeToLayer(l_vertex) # Then return layers.. return [l_vertex, l_edge]