[GIS] Saving Feature Class Symbology into Layer or Object to apply to another Feature Class

arcgis-desktoparcpyfeature-classlayerssymbology

I am creating a tool to automate a data standardization process. Part of this process is saving the feature class (variable "filename") as a layer file, projecting it into another coordinate system (variable "wmsde"), and then saving that one as a copy as well. I want the user to symbolize the feature class and then run the script/tool which would take the symbology they designated and apply it to the layers created. I realize it is incredibly easy to right click the layer and click Save As Layer, but I want as much as possible of this process to be automated. I am trying to use representations but a layer with the symbology needs to exist before the tool is used, which would be fine if I could find a way to save the symbology to a layer file. I have tried arcpy's Make Feature Layer and Save to Layer File (Management) to create a layer from the data but it does not save the symbology. I read on an answer to another question on here that this may be normal. I think the representation tool is working because the second layer's symbology is the same color as the first's (and they are usually assigned random colors). However the Drop Representation tool is failing and I'm not sure why. I get error 000252. Here is the (edited) code I have for it. Other tools that create symbology for a layer such as Apply Symbology From Layer (Data Management) also requires a symbolized layer.

Does anybody have any ideas as to what I can do to save the user's symbology designations or even another route aside from representations?

arcpy.env.workspace = r'C:\Data\Data.gdb'
worksde = r"C:\Users\me\AppData\Roaming\ESRI\Desktop10.2\ArcCatalog\DatabaseName.sde"
lyrFolder = r"C:\Layers"
ws = arcpy.env.workspace
filename = r"PlayAndTest"
sdefilename = r"PlayAndTest"
infile = os.path.join(ws, filename)
sdefile = os.path.join(worksde, sdefilename)

# Create a DD layer file
print "Creating DD LYR"
DDlyr = os.path.join(lyrFolder, filename + "_DD.lyr")
arcpy.SaveToLayerFile_management(filename, DDlyr)

# Reproject as WM
print "Projecting to WM"
wmsde = sdefile + "_WM"
prjfile = os.path.join(arcpy.GetInstallInfo()["InstallDir"],
                   "Coordinate Systems\Projected Coordinate Systems\World\WGS 1984 Web Mercator (auxiliary sphere).prj")
spatialRef = arcpy.SpatialReference(prjfile)
arcpy.Project_management(infile, wmsde, spatialRef)

# Make the WM Layer
WMlyr = os.path.join(lyrFolder, sdefilename + "_WM.lyr")
arcpy.SaveToLayerFile_management(sdefilename, WMlyr)
arcpy.ApplySymbologyFromLayer_management(WMlyr,DDlyr)

Best Answer

First, feature classes don't have symbology attributes. Symbology is attributed to layers in ArcGIS. When you add a feature class to a table of contents in ArcMap, a layer is created, though not saved anywhere as a .lyr file.

If you are having your users add a feature class to ArcMap that they then symbolize as desired, below is the way of saving that symbolization to a layer file (.lyr). The variable infile is a string of the layer name as it appears in ArcMap's table of contents.

# Create a DD layer file
print "Creating DD LYR"
DDlyr = os.path.join(lyrFolder, filename + "_DD.lyr")
arcpy.SaveToLayerFile_management(infile, DDlyr)

Also note that if you don't want to save an actual file, you can use DDlyr = arcpy.mapping.Layer (infile). Then the variable DDlyr will store a layer object with symbology.

This is all redundant however, since, as mentioned, ArcMap creates a layer whenever a feature class is added to a table of contents. Because this is the case, you can simply use the layer name as it appears in the table of contents to tell arcpy which layer to use as your input. Thus, assuming once again that the variable infile is a string of the layer's name as it appears in the table of contents, you can bypass creating another layer object and simply use the following as your AddRepresentation code:

arcpy.AddRepresentation_cartography(wmsde, represent, "RuleID","Override", "STORE_CHANGE_AS_OVERRIDE", infile, "Assign")

I hope this helps!

Related Question