0

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)
  • The symbology only exists within ArcMap or a layer file. Easy way: you need to set the symbology in ArcMap and then save from there, then you can copy the symbology to a new layer.. Hard way: access to the renderers exist in python but you need to hard code everything; takes me back to the old ArcPlot days where maps were made by AML scripts. I've not had any experience with representatins but do know what there're about, why do you want a representation? – Michael Stimson Mar 3 '15 at 23:12
4

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!

  • It did work, thanks! but now I am having an odd problem with the resulting layers. The first layer (DD) shows up in the layer file as PlayAndTest_DD but when pulled into the map the name goes back to PlayAndTest (Like the feature class). The same thing happens to the second layer (WM) but it is also pointing to the file in the geodatabase rather than the one in the SDE (from which it was created). Is this because of arcpy.ApplySymbologyFromLayer_management() or have I missed something in my code? – Tiffany Mar 4 '15 at 17:29
  • Since this is a different issue than the original question, feel free to post a new question with a bit of your current code and I'll take a look. – Emil Brundage Mar 4 '15 at 17:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.