How to add Symbology to a Feature Class??

363
3
03-30-2019 10:42 AM
AdamArcichowski1
New Contributor II

I don't understand how to add a simple symbology to a feature class created in the the script and add it to a new map. Well I kind od did figure it out but this my code seems ridiculously long just to add symbology from a existing layer.

I thought up to line 21 would be good enough to set the symbology I need. However, i had to add line 22,23,24 to get what I want. 

Basically I want the user to enter in a feature class, set a SQL expression and isolate the feature they want. Take that feature save it, add it to a different mxd, apply the symbology from an existing layer and save the mxd. 

I've heard of Derived and SetOutputParameters and apply the symbology there but I dont understand how to write that in the code. 

import arcpy, datetime, os, sys, string
date = datetime.datetime.now()
namefile = arcpy.GetParameterAsText(0)
prjbdy = arcpy.GetParameterAsText(1)
express = arcpy.GetParameterAsText(2)
mxdpath = "C:\Users\Adam\Desktop\Test5.mxd"

arcpy.CreateFileGDB_management(namefile, "\\SlopeAdam" + date.strftime("%Y%m%d")+ ".gdb")
fgdb = namefile + "\\Slope" + date.strftime("%Y%m%d")+ ".gdb"
arcpy.env.workspace = fgdb


mxd = arcpy.mapping.MapDocument(mxdpath)
df = arcpy.mapping.ListDataFrames(mxd)[0]

arcpy.MakeFeatureLayer_management(prjbdy, "prjbdy_Layer",express)
arcpy.CopyFeatures_management("prjbdy_Layer", "test3")
arcpy.MakeFeatureLayer_management("test3", "test4")
arcpy.ApplySymbologyFromLayer_management('test4', "C:\Users\Adam\Desktop\SlopeAnalysis\prjbdy_Layer.lyr")
arcpy.SaveToLayerFile_management('test4',"C:\Users\Adam\Desktop\SlopeAnalysis\prjbdyoutada")
addLayer = arcpy.mapping.Layer('test4')
arcpy.mapping.AddLayer(df, addLayer, "AUTO_ARRANGE")
arcpy.AddMessage("Finished Adding raster & applying symbology.")


mxd.save()
del mxd‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Thanks! 

0 Kudos
3 Replies
AdamArcichowski1
New Contributor II

hmm

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

That issue aside, must be arcmap and python 2.7 because it throws an error because your paths aren't raw encoded, in python 3.  Not a bad idea even in 2.7

"C:\Users\Adam\Desktop\SlopeAnalysis\prjbdy_Layer.lyr"
  File "<ipython-input-8-9a872039ced2>", line 1
    "C:\Users\Adam\Desktop\SlopeAnalysis\prjbdy_Layer.lyr"
                                                          ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

r"C:\Users\Adam\Desktop\SlopeAnalysis\prjbdy_Layer.lyr"

'C:\\Users\\Adam\\Desktop\\SlopeAnalysis\\prjbdy_Layer.lyr'
0 Kudos
AdamArcichowski1
New Contributor II

Yes Dan, 

I usually do, but i guess i forgot. 

And yes I'm using 2.7 and arcmap 10.5.1

Thanks,