[GIS] Programmatically changing raster symbology after reclassification using ArcPy

arcpycolorreclassify

I have created a code in Python that I will use for a custom tool in ArcGIS, the code will create a Eucledian Distance of 6000m around a point and Reclassify the distance from 0-2000, 2000-4000, 4000-6000. The code works but what I was wondering was, is there a bit of code I can add on to make the 0-2000 area red, the 2000-4000 area to be orange and the 4000-6000 area to be yellow. As the code stands the colours are random i guess (blue, green etc.). I have been looking through a few books but could not find a solution.

from arcpy import env
from arcpy.sa import *

# Set environment settings
arcpy.env.workspace = "Z:\\GIS TEST\\Select_by_Location"
arcpy.env.overwriteOutput=1

# Check out any necessary licenses
arcpy.CheckOutExtension("spatial")


# Local variables:
Breakout_Location = "Breakout_Location.shp"
maxDistance = "6000"
Output_direction_raster = "Z:\\GIS TEST\\Codes\\Output"

# Set Extents
arcpy.env.extent = arcpy.Extent(31652.290929,28102.389486,269652.513962,212966.447186)

if arcpy.Exists(Output_direction_raster):
    arcpy.Delete_management(Output_direction_raster)
Euc_Dist1=EucDistance(Breakout_Location, 6000, 400, Output_direction_raster)


# Process: Reclassify
Reclass_Output_direction_raster = Reclassify(Euc_Dist1, "Value", RemapRange([[0,2000,1],[2000,4000,2],[4000,6000,3]]), "DATA")

arcpy.SetParameterAsText(0, Breakout_Location)


Reclass_Output_direction_raster.save("Z:\\GIS TEST\\Codes\\Output")

Best Answer

I recently ran into this limitation when creating an esri addin toolbar. I was limited to the option that Chris R mentioned, creating and saving a template layer with my python code. It was fairly simple and works remarkably well.

To not hard code the path to the symbology layer, I used os.path to get the path of the layer saved with the python code...

import os
Temp_Symbology = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'Symbology_Template.lyr')

Add raster to TOC

mxd = arcpy.mapping.MapDocument('current')
df = arcpy.mapping.ListDataFrames(mxd)[0]
add_layer = arcpy.mapping.Layer(Output_direction_raster)
arcpy.mapping.AddLayer(df, add_layer, "TOP")
reclass_raster_lyr = arcpy.mapping.ListLayers(mxd)[0]

Then apply the symbology and refresh active view...

# Apply symbology from .lyr file
arcpy.ApplySymbologyFromLayer_management(reclass_raster_lyr, Temp_Symbology)
arcpy.RefreshActiveView()
Related Question