Symbology Layer Not Applying to New Feature Layer

843
5
07-13-2017 09:11 AM
VishalShah2
Occasional Contributor II

So my custom tool script first creates a new feature layer based on an existing feature layer using the MakeFeature_management function with just the certain fields I want showing. After that, the tool has a variable called inputLayer = to the raw string of the newly created Feature Layer. Here's what the script looks like so far:

import arcpy
import arcpy.mapping as map

mxd = arcpy.mapping.MapDocument("Current")

visibleFields = ["Field1", "Field2", "Field3", "Field4", "Field5"]
field_info = arcpy.Describe('Fiber Segments').fieldInfo
df = arcpy.mapping.ListDataFrames(mxd)[0]

for index in xrange(0, field_info.count):
    if field_info.getfieldname(index) not in visibleFields:
        field_info.setvisible(index,"HIDDEN")
arcpy.Delete_management('Output Feature',"")
arcpy.MakeFeatureLayer_management('Input Feature','Output Feature Layer',"","",field_info)
addLayer = arcpy.mapping.Layer('Output Feature Layer')
arcpy.mapping.AddLayer(df, addLayer)
arcpy.RefreshTOC()
arcpy.RefreshActiveView()

inputLayer = r'Output Feature Layer'
symbologyLayer = r'C:\pathway\Output Feature Layer symbology.lyr'

arcpy.ApplySymbologyFromLayer_management (inputLayer, symbologyLayer)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

The issue here is that the symbology isn't applying to the Output Feature Layer when the entire script is run. If you run these lines individually in the python module in ArcMap, they run fine and apply the symbology. Any thoughts on this here?

0 Kudos
5 Replies
MitchHolley1
MVP Regular Contributor

Is the 'Input Layer' in lines 13 and 14 the same layer?  Cause you're deleting one layer in line 13 then creating a feature layer from it on line 14. 

0 Kudos
VishalShah2
Occasional Contributor II

Ah, line 13 is actually Output Feature Class. I'm running this tool over and over again to test it. Just using line 13 to delete the traces of the last Output Feature Class.

0 Kudos
MitchHolley1
MVP Regular Contributor

Are all the field names and types matching between the .lyr file and the Input Layer?

0 Kudos
VishalShah2
Occasional Contributor II

Yes. I actually figured it out. I'll add that to the comments to show the solution.

0 Kudos
VishalShah2
Occasional Contributor II

Figured it out and here's the solution:

import arcpy
import arcpy.mapping as map

mxd = arcpy.mapping.MapDocument("Current")

visibleFields = ["Field1", "Field2", "Field3", "Field4", "Field5"]
field_info = arcpy.Describe('Fiber Segments').fieldInfo
df = arcpy.mapping.ListDataFrames(mxd)[0]

for index in xrange(0, field_info.count):
    if field_info.getfieldname(index) not in visibleFields:
        field_info.setvisible(index,"HIDDEN")
arcpy.Delete_management('Output Feature',"")
arcpy.MakeFeatureLayer_management('Input Feature','Output Feature Layer',"","",field_info)
addLayer = arcpy.mapping.Layer('Output Feature Layer')
arcpy.mapping.AddLayer(df, addLayer)
arcpy.RefreshTOC()
arcpy.RefreshActiveView()

inputLayer = arcpy.mapping.ListLayers(mxd, 'Output Feature Layer')[0]
symbologyLayer = r'C:\pathway\Output Feature Layer symbology.lyr'

arcpy.ApplySymbologyFromLayer_management (inputLayer, symbologyLayer)

This is working well for me without any errors. Not entirely sure why the raw string didn't work but the way it's set equal to inputLayer seems to do the trick.

0 Kudos