Changing Symbology Value Field for FC in TOC using Python

240
1
05-15-2018 08:04 AM
anTonialcaraz
Occasional Contributor II

Hi there

I've spent quite a while searching online and ArcGIS/ArcPy Help for a solution with no success, so I wonder if someone could think of a way around this issue:

I would like to change the symbology Value Field of a Feature Class within an MXD. I need first to set the symbology of the FC to "Match to symbols in a style" using a Value Field and then use Python to change the Value Field keeping the same symbology style. I know that ArcGIS does not support "Match to symbols in a style" symbology Type but I wonder if there is away around this.

It would be something like the script below but replacing the symbology Type with "something else". Hope I explained the issue clearly. Many thanks.

import arcpy

mxd = arcpy.mapping.MapDocument("CURRENT")

lyrlist = arcpy.mapping.ListLayers(mxd)

for lyr in lyrlist:

    if lyr.symbologyType == "UNIQUE_VALUES":

        lyr.symbology.valueField = "DESCRIPTION"
        lyr.symbology.addAllValues()

arcpy.RefreshActiveView()
arcpy.RefreshTOC()

del mxd
0 Kudos
1 Reply
anTonialcaraz
Occasional Contributor II

Hi again,

Thanks to everyone that have had a look into this. I guess it is a tricky one.

Just to add on my question/query:

I have a macro written in VB by someone at the company quite a while ago that does what I intend to do (improve) in Python. I have no knowledge of VB so not really sure how to translate this into Python. I assume that if this can be done in VB it should also by possible in Python? Please see the VB code below and if you can help with this it would be greatly appreciated.

Const STYLE_FIELD = "CSID"
Const LABEL_FIELD = "Description"
' --------------------------------------------


Public Sub LabelStylesFromAnotherField()
Dim pMxDoc As IMxDocument
Dim pGeoFtrLyr As IGeoFeatureLayer
Dim pLegInfo As ILegendInfo
Dim pLegGrp As ILegendGroup
Dim pLegCls As ILegendClass
Dim f As Long

    Set pMxDoc = ThisDocument
    Set pGeoFtrLyr = pMxDoc.FocusMap.Layer(0)
    Set pLegInfo = pGeoFtrLyr.Renderer
    Set pLegGrp = pLegInfo.LegendGroup(0)
   
    For f = 0 To pLegGrp.ClassCount - 1
        Set pLegCls = pLegGrp.Class(f)
        pLegCls.Label = GetStyleLabel(pLegCls.Label, STYLE_FIELD, LABEL_FIELD, pGeoFtrLyr)
    Next f
   
    pMxDoc.UpdateContents
   
    MsgBox "Updated Symbol Labels", vbInformation, ""
   
End Sub



Private Function GetStyleLabel(sStyle As String, sStyleField As String, sLabelField As String, pGeoFtrLyr As IGeoFeatureLayer) As String
Dim pFtrCls As IFeatureClass
Dim lStyFldIdx As Long
Dim lLblFldIdx As Long
Dim lID As Long
Dim pQryFltr As IQueryFilter

    Set pFtrCls = pGeoFtrLyr.DisplayFeatureClass
    lStyFldIdx = pFtrCls.FindField(sStyleField)
    lLblFldIdx = pFtrCls.FindField(sLabelField)

    If lStyFldIdx = -1 Or lLblFldIdx = -1 Then
        GetStyleLabel = sStyle
        Exit Function
    End If
   
    Set pQryFltr = New QueryFilter
    pQryFltr.WhereClause = sStyleField + " = '" + sStyle + "'"
   
    lID = pFtrCls.Select(pQryFltr, esriSelectionTypeIDSet, esriSelectionOptionOnlyOne, Nothing).IDs.Next
   
    If lID = -1 Then
        GetStyleLabel = sStyle
        Exit Function
    End If
   
    GetStyleLabel = CStr(pFtrCls.GetFeature(lID).Value(lLblFldIdx))
   
End Function
0 Kudos