Using python to change vector style

المشرف العام

Administrator
طاقم الإدارة
I am trying to change a qml style using pyqgis.The style is rule based with 12 rules each with different symbology.All the symbols are two layers (one is 3 layers).In two of the rules, I would like to refer to a user selected option from a combo box from a qwidget form.Since I couldn't figure out how to include a reference to the qwidget in qgis styles, I tried to rebuild the style in code.So far I am able to recreate the style rules using rendererV2(), but then I lose the symbology of the table.So I tried saving the symbols of the layer before recreating the rules.But redoing the rules destroys the parent style and the saved rule objects are no longer viable.Then I tried creating a temporary layer and then recreating new rules on it, and then adding the symbols to the newly created styles on the temporary layer from the saved symbol objects, but the symbols are not matching the original layer.Here is a snapshot of both layers in qgis along with the form I am trying to use the acquisition id in the layer styles:







I thought I would first create the temp_segments_layer with the correct style, and then copy it to the segments_layer, but as you can see it loses the symbols.

Here is the code I am using:

def resetSymbology(self):# Tempory layer for new symbologytemp_segment_layer = QgsVectorLayer("LineString?crs=epsg:4326" "&field=id:integer" "&field=road_type:String" "&field=road_name:String" "&field=from_node:integer" "&field=to_node:integer" "&field=obsolete:String" "&field=omitted:String" "&field=flag:integer" "&field=earliest_reacq_date:String" "&field=covlnk_id_acquisition:integer" "&field=covlnk_prod_unit:String", "temp_segment_layer", "memory")QgsMapLayerRegistry.instance().addMapLayer(temp_segment_layer)# Save symbols from old table# I don't think this is necessary anymore since I am using a temporary table.symbol = {}for i in range(len(self.segments_layer.rendererV2().symbols())): symbol_layer = [] for x in range(5): if self.segments_layer.rendererV2().symbols().symbolLayer(x) != None: symbol_layer.append(self.segments_layer.rendererV2().symbols().symbolLayer(x)) symbol = symbol_layer# define some rules: label, expression, color name, (min scale, max scale)# Although the original segment_layer has 12 rules, I am just trying two for now.acquisitionID = self.prodUnits.cboAcquisition.currentText()exprMotorways = "\"obsolete\"=0 " \ "AND \"omitted\"is null " \ "AND \"covlnk_id_acquisition\"= "+acquisitionID+\ " AND \"covlnk_prod_unit\"is null " \ "AND \"road_type\"!='not_controlled_access'"exprTrunks = "\"obsolete\"=0 " \ "AND \"omitted\"is null " \ "AND \"covlnk_id_acquisition\"="+acquisitionID+\ "AND \"covlnk_prod_unit\"is null " \ "AND \"road_type\"='not_controlled_access'"road_rules = ( ('ProdUnit None: Motorways', exprMotorways, 'red,green', None), ('ProdUnit None: Trunks', exprTrunks, 'blue', None),)# create a new rule-based renderernewSymbol = QgsSymbolV2.defaultSymbol(temp_segment_layer.geometryType())renderer = QgsRuleBasedRendererV2(newSymbol)# get the "root" ruleroot_rule = renderer.rootRule()for label, expression, color_name, scale in road_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)) # set the scale limits if they have been specified if scale is not None: rule.setScaleMinDenom(scale[0]) rule.setScaleMaxDenom(scale[1]) # append the rule to the list of rules root_rule.appendChild(rule)# delete the default ruleroot_rule.removeChildAt(0)# apply the renderer to the layertemp_segment_layer.setRendererV2(renderer)# Add symbols to new styles on temp_segments_layer.for i in range(len(temp_segment_layer.rendererV2().symbols())): for x in range(len(symbol)): temp_segment_layer.rendererV2().symbols().changeSymbolLayer(x, symbol[x])Using PyQGIS, I would like to add a reference to the user form in the segments_layer symbology by either manipulate an existing qml file or recreating the style rules without losing the symbology.



أكثر...
 
أعلى