[GIS] Using Unique Values Symbology in ArcPy

arcgis-10.1arcpysymbology

I am using ArcGIS for Desktop 10.1 and Server 10.1 on my machine Windows 7.

I am newbie in python & running simple python script for UniqueValuesSymbology it running fine if I use mxd as “current” but if I update with full mxd path then it’s not updating the layers symbology and did not show any error.

JSFiddle code 1 (using mxd path as current)

import arcpy
mxd = arcpy.mapping.MapDocument("current")
lyr = arcpy.mapping.ListLayers(mxd, "Population")[0]
if lyr.symbologyType == "UNIQUE_VALUES":
  lyr.symbology.valueField = "SUB_REGION"
  lyr.symbology.addAllValues()
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
del mxd

JSFiddle code 2 (using complete mxd path )

import arcpy
mxd = arcpy.mapping.MapDocument("D:\\ArcGIS_Data\\data\\y1.mxd")
lyr = arcpy.mapping.ListLayers(mxd, "Population")[0]
if lyr.symbologyType == "UNIQUE_VALUES":
  lyr.symbology.valueField = "SUB_REGION"
  lyr.symbology.addAllValues()
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
del mxd

I researched on Google, web help and ArcPy syntax for mxd_path still not getting any clue & Python path is perfect in environmental variables

The reason of using mxd path is that I want to make this script automatic using windows scheduler.

Best Answer

Try this code and then re-open your MXD in ArcMap and I think you should see your new symbology.

import arcpy
mxd = arcpy.mapping.MapDocument("D:\\ArcGIS_Data\\data\\y1.mxd")
lyr = arcpy.mapping.ListLayers(mxd, "Population")[0]
if lyr.symbologyType == "UNIQUE_VALUES":
  lyr.symbology.valueField = "SUB_REGION"
  lyr.symbology.addAllValues()
mxd.save()
del mxd

As @Roland commented you were running your code as a process separate from your server, which was reading the map document off disk. Although your code modified the document, it hadn't saved the changes to the mxd file (which is what the server would show).