How to remove a specific unique value with arcpy

5889
14
05-11-2015 02:48 AM
Yaron_YosefCohen
Occasional Contributor II

Hi everyone

For disclosure, i asked this question in python - How to remove a specific unique value with arcpy - Geographic Information Systems Stack Exc... but didn't get useful answers.

using arcpy, i would like to remove from 50 mxd files, a specific unique value called "residence a" (exist in 3 layers) from the table of content.

enter image description here

The value of "residence a" is the number  "70"  in the attribute table (in field named "YEUD")

enter image description here

When i use this code:

import arcpy,os,sys
from arcpy import env

env.workspace = r"C:\Project"
for mxdname in arcpy.ListFiles("*.mxd"):
    mxd = arcpy.mapping.MapDocument(r"C:\Project\\" + mxdname)
    df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
    lyr = arcpy.mapping.ListLayers(mxd, "*")[0]
    if lyr.symbologyType == "UNIQUE_VALUES":
        vals = lyr.symbology.classLabels
        for v in vals:
            if v == "residence a":
                print mxdname
                print lyr.name
                print ("1 in layer " + lyr.name)
                arcpy.mapping.RemoveLayer(df, v)        
    mxd.save()
del mxd

i get en error:

>>> Project -.mxd mig1 1 in layer mig1  Traceback (most recent call last): File "C:/Users/yaron.KAYAMOT/Desktop/remove UNIQUE_VALUES in lyr.py", line 18, in    <module> arcpy.mapping.RemoveLayer(df, v) File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\utils.py", line 182, in fn_ return fn(*args, **kw) File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\mapping.py", line 1845,     in RemoveLayer assert isinstance(remove_layer, Layer) AssertionError >>> 

I search desperately a solution to my problem -thanks

0 Kudos
14 Replies
RichardFairhurst
MVP Honored Contributor

Python will not only modify the Unique Values listed in the layer, it will reset the symbology of every unique value in the list to random simple colors.  Python mapping is very limited in what it can do with symbols.  Therefore the picture you showed will never retain the symbol colors you showed if you use Python.  You would have to use .Net ArcObjects to maintain the rest of the symbols in the layer after removing one of the values from the list.  I am afraid you are wasting your time with Python if you require the symbols for the other Unique Values to remain unchanged.

0 Kudos
Yaron_YosefCohen
Occasional Contributor II

i just want to remove the value "residence a" from the layers. Right now the symbology isn't important to me.

0 Kudos
CarlSunderman
Occasional Contributor

You aren't trying to remove the FC, just a value in the symbology, correct?

Two simple options would be to either replace the layer file in each mxd with one excluding the the residence value and another option would be to apply  definition query to the FC and query out the values.

Below will replace a layer file in a directory of mxd's

import arcpy
import os
import glob

folder = <folder containing mxds>
layerName = <Existing layer name in mxd's to be replaced>
layerFile = <Layer file containing new symbology>

newLayer = arcpy.mapping.Layer(layerFile)

mxds = glob.glob(folder + '\\' + '*.mxd')
arcpy.gp.overwriteOutput = True

for mxdFile in mxds:
    mxd = arcpy.mapping.MapDocument(mxdFile)
   
    for df in arcpy.mapping.ListDataFrames(mxd):
        for lyr in arcpy.mapping.ListLayers(mxd, data_frame=df):
            if lyr.name == layerName:
                arcpy.mapping.UpdateLayer(df, lyr, newLayer, False)
    mxd.save()
del mxd, newLayer

hope this helps

Yaron_YosefCohen
Occasional Contributor II

Hi Carl,

How  can i  apply  definition query to the FC and query out the values in the code?

0 Kudos
CarlSunderman
Occasional Contributor

It would be

for lyr in arcpy.mapping.ListLayers(mxd, "*"): 
    if lyr.name == "<Feature Name>": 
          lyr.definitionQuery = '<Field Name> = \'<Field Value>\''
mxd.save()

for a shapefile, the format is slightly different