I'm trying to use ArcPy in order to change the layout of many Imagine files in my MXD.
Import Symbology from a layerfile does not seem very straightforward to me when using ArcPy (see this earlier question) so I'd like to try and use 'Apply symbology from layer' and base the symbology on a single img file for which I change the symbology by hand.
The script runs without raising any errors, but my symbology does not change. I figured this might be because the Imagine files are automatically interpreted as 'stretched' when imported, but the symbology I want is based on 'Unique values'.
Is there an easy way to compute these unique values using ArcPy?
I know how to do compute them in the Properties box, but I'd rather include it in my script, since there are many Imagine files and its likely that I'll have to rerun it with new files that have different filenames.
The script adds data by looping over a bunch of files and it would be great if the symbology is automatically applied right away.
~~~
# import modules
import os, arcpy
~~~
# One-off actions
DIR = r"C:\imagine"
mxd = arcpy.mapping.MapDocument("CURRENT")
data_frame = arcpy.mapping.ListDataFrames(mxd, "Map series")[0]
target_group_layer = arcpy.mapping.ListLayers(mxd, "Geological Maps", data_frame)[0]
symbologyLayer = "example_symbology.img"
~~~
# Actions for each grid
for filename in os.listdir(DIR):
if filename[-3:] == 'img':
# importing the data
add_layer = arcpy.mapping.Layer(filename)
arcpy.mapping.AddLayerToGroup(data_frame, target_group_layer, add_layer, "BOTTOM")
# setting the symbology
arcpy.ApplySymbologyFromLayer_management(add_layer, symbologyLayer)
I'm running my code from the Python window within ArcMap 10.6.1.
1 Answer 1
You can build an attribute table and read the values into a set using da.SearchCursor:
arcpy.BuildRasterAttributeTable_management(r"C:\GIS\data\testdata\Classes.tif")
values = {i[0] for i in arcpy.da.SearchCursor(r"C:\GIS\data\testdata\Classes.tif","Value")}
#arcpy.DeleteRasterAttributeTable_management(r"C:\GIS\data\testdata\Classes.tif")
values
set([0, 1, 2, 3, 4, 5, 8])
#values = list(values) #Or whataver data type you want