How to add layer to TOC after using MakeFeatureLayer (management)

1977
11
Jump to solution
07-13-2017 06:08 AM
VishalShah2
Occasional Contributor II

So I'm working on a custom tool that generates KMZs for reports to go out ot various different departments. One thing that we do is turn certain fields off not to show every attribute in the table. Thus, I settled on using MakeFeatureLayer_management tool to create a new feature layer with only the fields I want shown in the report. Here is what my code looks like:

visibleFields = ["Field1", "Field2", "Field3", "Field4", "Field4"]
field_info = arcpy.Describe('Input Feature Layer').fieldInfo

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

When I run this in the python module in ArcMap, the output feature layer is automatically added to the Table of Contents. But when I run this in my custom tool, the output feature layer isn't added to the Table of Contents and my custom tool spits out an error because the tool cannot progress as the output feature layer does not exist in the Table of Contents and cannot be referred on. So how would I be able to add the newly created feature layer to the table of contents in the script when the feature layer is created in program memory?

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

It is helpful to share some basic properties of the script tool because they can affect how certain tasks are achieved.  For example, are you using default settings of "Always run in foreground" and "Run Python script in process"?  What input and output parameters are set?

Assuming you are using all defaults, one way of adding the newly created layer back in is to utilize Layer—Help | ArcGIS Desktop  and AddLayer—Help | ArcGIS Desktop.

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
visibleFields = ["Field1", "Field2", "Field3", "Field4", "Field4"]
field_info = arcpy.Describe('Input Feature Layer').fieldInfo

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

If the script is running in process, the newly created "Output Feature Layer" name will remain in use after the script tool is complete, so running the tool again using a different input and exactly the same name for the feature layer (e.g., "Output Feature Layer") will generate an error.

View solution in original post

11 Replies
JoshuaBixby
MVP Esteemed Contributor

It is helpful to share some basic properties of the script tool because they can affect how certain tasks are achieved.  For example, are you using default settings of "Always run in foreground" and "Run Python script in process"?  What input and output parameters are set?

Assuming you are using all defaults, one way of adding the newly created layer back in is to utilize Layer—Help | ArcGIS Desktop  and AddLayer—Help | ArcGIS Desktop.

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
visibleFields = ["Field1", "Field2", "Field3", "Field4", "Field4"]
field_info = arcpy.Describe('Input Feature Layer').fieldInfo

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

If the script is running in process, the newly created "Output Feature Layer" name will remain in use after the script tool is complete, so running the tool again using a different input and exactly the same name for the feature layer (e.g., "Output Feature Layer") will generate an error.

VishalShah2
Occasional Contributor II

Joshua,

I believe this is "Run Python script in Process" for the custom tool. The input layer for creating a new feature layer is always set as that is being pulled from a database we use. The output layer is whatever I choose to name it (which i try to keep it the same as the original except for one addition at the end - which department it's for). That is a small change but is being referenced later in the script. It works in the python module, but not in the custom script. I ran the script with the addition you recommended but it gets the error saying that there is an error in running:

arcpy.mapping.AddLayer(df, addLayer)

any ideas?

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

What is the specific error message?

0 Kudos
VishalShah2
Occasional Contributor II

File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\utils.py", line 182, in fn_ return fn(*args, **kw)

File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\mapping.py", line 48, in AddLayer assert isinstance(data_frame, DataFrame)

AssertionError

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

The error is stating the data frame argument you are passing is not an instance of a DataFrame, which is needs to be.

0 Kudos
VishalShah2
Occasional Contributor II

How would I go about in doing that?

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I assume my code snippet was incorporated into additional code, and possibly modified.  It would be best to post the script so that I can look at exactly what calls are being made.

0 Kudos
VishalShah2
Occasional Contributor II

So here's the code and all of the inputs with it

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

# if conditional for report type when FS is working
for index in xrange(0, field_info.count):
    if field_info.getfieldname(index) not in visibleFields:
        field_info.setvisible(index,"HIDDEN")
arcpy.Delete_management('Fiber Segments NOC',"")
arcpy.MakeFeatureLayer_management('Fiber Segments','Fiber Segments NOC',"","",field_info)
addLayer = arcpy.mapping.Layer('Fiber Segments NOC')
arcpy.mapping.AddLayer(df, addLayer)
arcpy.RefreshTOC()
arcpy.RefreshActiveView()
0 Kudos
VishalShah2
Occasional Contributor II

I think I figured out what the issue was....if you see the df variable, it has .name at the end. I use that variable later for MapToKML tool. so i need to make that a local variable instead of an universal one. and for addLayer, df variable shouldn't have .name at the end. Is that correct? I ran the script, and it did add the layer this time.

0 Kudos