Select to view content in your preferred language

Python: Set Definition Query and addAllValues Using UniqueValuesSymbology Class

1862
3
08-23-2012 08:10 AM
MikeMacRae
Frequent Contributor
I'm trying to set a definition query on a layer in a map using python. I want to further use the UniqueValuesSymbology class to addAllValues.

When I do this manually, (i.e. set a definition query and then add all values in the symbology tab on the layer) the values that get added are limited to what's in the definition query, which is what I want. I wrote a script and it sets the definition query as I'd like it, but it doesn't seem to add all the values to the symbology properties dialogue box afterwards.

In my script, I've tried to set the definition query first, refresh the activeview and TOC (I'm assuming this virtually clicks the "Apply" button in the layers properties dialogue box) and then I addAllValues and refresh again. The values aren't getting added. My original Unique Values list remains. Can anyone see what the problem might be?

Thanks,
Mike


   #Set mxd
#Set dataframe
#Loop layers
sList = []
if lyr.name == "Legend":
        arcpy.SelectLayerByLocation_management(lyr, "INTERSECT", "Boundary")
        for row in arcpy.SearchCursor(lyr):
               soilList.append("'{}'".format(row.SMU))
        lyr.definitionQuery = '"SMU" in ({})'.format(", ".join(set(sList)))
        arcpy.RefreshActiveView()
        arcpy.RefreshTOC()                            

        if lyr.symbologyType == "UNIQUE VALUES":
               lyr.symbology.valueField = "SMU"
               lyr.symbology.addAllValues()
        arcpy.RefreshActiveView()
        arcpy.RefreshTOC()                              
        arcpy.SelectLayerByLocation_management(lyr, "CLEAR_SELECTION")
Tags (2)
0 Kudos
3 Replies
JeffMoulds
Esri Contributor
Its working for me...however, I dont have your data. I am using the Continents feature class from here:

C:\Program Files (x86)\ArcGIS\Desktop10.1\TemplateData\TemplateData.gdb\World\continent

The syntax for those IN queries can get complicated. Does it work if you use a simpler def query? For example:

import arcpy.mapping as ma
mxd = ma.MapDocument('current')
l = ma.ListLayers(mxd)[0]
l.definitionQuery = '"shape_area" > 1500'
arcpy.RefreshActiveView()
l.symbology.valueField = 'CONTINENT'
l.symbology.addAllValues()
arcpy.RefreshActiveView()


And here it is with my IN def query...does it work if you dont build the query on the fly?

mxd = ma.MapDocument('current')
l = ma.ListLayers(mxd)[0]
l.definitionQuery = '"CONTINENT" IN (' + "'Africa', 'Asia', 'Europe')"
arcpy.RefreshActiveView()
l.symbology.valueField = 'CONTINENT'
l.symbology.addAllValues()
arcpy.RefreshActiveView()


P.S. You dont need arcpy.RefreshTOC() at 10.1 anymore. Arcpy.RefreshActiveView() does it all.
0 Kudos
ChristopherThompson
Frequent Contributor
Just a thought but you initialize an empty list sList and then it looks like you are using values from that for your def query.  Except that I don't see where you are adding values to sList.  I do see you adding values to a list called soilList however.  Is it possible you've got the wrong list names in places? should soilList in the line below be sList instead?
soilList.append("'{}'".format(row.SMU))
0 Kudos
MikeMacRae
Frequent Contributor
Hey guys,

Thanks for responding.

The issue I figured out was checking for symbology type
if lyr.symbologyType == "UNIQUE VALUES":
I forgot to add the underscore in the "UNIQUE_VALUES" keyword.

Jeff, thanks for the heads up on refreshing. I didn't realize
arcpy.RefreshTOC() 
is redundant when using
arcpy.RefreshActiveView()


Christopher, thanks for pointing out the error. I edited my script when I posted, but my original script was used sList throughout.
0 Kudos