1

I'm trying to save a symbology from a layer, but any way I try to do it, it just gives me a unsymbolised feature shapefile when I try to add the newly created .lyr file.

Anyone have any idea why this is?

I've tried using the ArcToolbox tool and it works fine, but the python snippet taken from that is a replic of what's in the script that's not working.

import arcpy
arcpy.env.overwriteOutput = True

head, wsName = os.path.split(r"D:\RS_Data\Workspace\Garden_CentralandNE")
mxd = os.path.join(r"D:\RS_Data\Workspace\Garden_CentralandNE",wsName + ".mxd")
outputFolder = os.path.join(r"D:\RS_Data\Workspace\Garden_CentralandNE", "gisdb")

mxd = arcpy.mapping.MapDocument(mxd)  # get MXD
df = arcpy.mapping.ListDataFrames(mxd)[0]  # get data frame
lyr = arcpy.mapping.ListLayers(mxd, "Service*", df)[0]

outLyr = os.path.join(outputFolder,"Symbology.lyr")

arcpy.SaveToLayerFile_management(lyr,outLyr,'#','CURRENT')  # Saves the file but without symbology
0
2

I think the issue is that you are trying to save a service layer. I couldn't tell you why it doesn't work through Python, but I have had similar issues with database joins and symbology. They work when I do it by hand, but not when I try and script it, it doesn't work. Even though it seems like it's the same task. In these situations I had to move to ArcObjects and comtypes to get a more consistent behaviour, but that's a steep learning curve that I don't recommend if you can help it.

See if you can change the symbology of the layer to something else, then use the script. Then you'll know if the issue is tied to the symbology provided by the service layer.

1

Does the problem also occur in the Python Window? Like:

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
df1 = arcpy.mapping.ListDataFrames(mxd)[0]
lyr = arcpy.mapping.ListLayers(mxd, "*", df1)[0]
arcpy.SaveToLayerFile_management(lyr,"""D:/test.lyr""",'#') 

(I am using 10.0 so there are only 3 arguments for SaveToLayerFile_management())

3
  • Interestingly that code works fine. Did some playing around and it stops working when I replace MapDocument("CURRENT") with MapDocument(path) - it makes the layer file but again loses the original symbology... need to work out how to get it to work when MXD is read as a path
    – Mike
    Apr 1 '15 at 14:58
  • Could it be that when it's a path, since the mapdocument application isn't open, there are some rendering events that aren't triggered, and so the symbology isn't coming down with the data? Assuming this is a service layer still...
    – cndnflyr
    Apr 2 '15 at 13:09
  • That does seem to be the problem, seems like it isn't possible by using a path. Dumb question here what actually is a service layer? This example is a layer called 'service location' which is a point feature class made from a table.
    – Mike
    Apr 2 '15 at 21:54

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.