Toggling Fields within a Feature Class On and Off

1702
8
Jump to solution
07-12-2017 07:13 AM
VishalShah2
Occasional Contributor II

So I've been doing some research into this but have gotten conflicting outputs on whether or not it is possible to turn fields on and off using python in custom tools. I am working on a custom tool that generates KMZs on a state by state level for different departments within the company, but we do not let them see all the attributes within each feature class, thus turning some fields off before we use Map to KML tool. Since I am currently trying to automate this process, I want the custom tool to first turn the fields I do not want shown in the report off before the tool gets to the actual execution in regards to generating the KMZs. One such discussion I looked at was this on titled Turn fields off by script. While Wayne's solution seemed to work for the OP, there were parts of that solution I did not understand. Is there an easier way to approach this or if someone can help break down Wayne's solution, that would be appreciated. I look forward to seeing the ideas everyone has for this. 

For reference, here's what the solution to the other discussion:

import arcpy
from arcpy import env
arcpy.env.overwriteOutput = True
arcpy.env.addOutputsToMap = False
mxd = arcpy.mapping.MapDocument('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd, '*')[0]
LayerNeedsFieldsTurnedOff = arcpy.mapping.ListLayers(mxd, 'your target layer name', df)[0]
# fill in your desired fields to remain visible 
desiredFields = ['fieldname0', 'fieldname1', 'fieldname2', 'etc']
field_info = arcpy.Describe(LayerNeedsFieldsTurnedOff).fieldInfo 
for i in range(field_info.count):
    if field_info.getfieldname(i) not in desiredFields:
        field_info.setvisible(i, 'HIDDEN')
arcpy.MakeFeatureLayer_management(LayerNeedsFieldsTurnedOff, 'temp_layer', '', '', field_info)
refLyr = arcpy.mapping.Layer('temp_layer')
# rename the ref layer the same as your target layer
refLyr.name = 'your target layer name'  
arcpy.ApplySymbologyFromLayer_management(refLyr, LayerNeedsFieldsTurnedOff)
arcpy.mapping.UpdateLayer(df, LayerNeedsFieldsTurnedOff, refLyr, False)
mxd.save()  
print 'cleaning up-' 
if arcpy.Exists('temp_layer'):
    print '\'temp_layer\' still in memory...deleting now...'
arcpy.Delete_management('temp_layer')
print 'deleting obj refs...'
del mxd,LayerNeedsFieldsTurnedOff, refLyr print 'done.' 
0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

The Setting Field Visibility  discussion offers a very similar code sample, but the accepted answer provides a bit more explanation.  Setting field visibility is a bit convoluted, I don't know why Esri made it so difficult, but there really isn't a simpler approach than the one being suggested in the discussions you and I reference.

I assume this is for ArcGIS Desktop/ArcMap.  I haven't tried modifying field visibility in ArcGIS Pro.

View solution in original post

8 Replies
JoshuaBixby
MVP Esteemed Contributor

The Setting Field Visibility  discussion offers a very similar code sample, but the accepted answer provides a bit more explanation.  Setting field visibility is a bit convoluted, I don't know why Esri made it so difficult, but there really isn't a simpler approach than the one being suggested in the discussions you and I reference.

I assume this is for ArcGIS Desktop/ArcMap.  I haven't tried modifying field visibility in ArcGIS Pro.

VishalShah2
Occasional Contributor II

Thanks Joshua. This is for ArcGIS DesktopArcMap. It definitely was much more easier to read then the discussion I had posted! Also was easily able t follow it it. I'll have to try that with my script. Thanks again! Do you know if you have to pass another line to pull in the new feature class or it automatically does it when you run the arcpy.MakeFeatureLayer_management line?

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I would experiment.  From the interactive command prompt it definitely creates the layer and adds it to the TOC, but I can't remember how it is handled in script tools.

VishalShah2
Occasional Contributor II

So far I've tried it in the interactive command prompt and it definitely added the new layer to the TOC. I'll have to test it out in the script tool and post my results here. From my understanding, is this a temporary layer? So tat if I closed my mxd, that would disappear? Or would I have to add a line at the end to delete the new layer that was created after my tool is done running?

0 Kudos
VishalShah2
Occasional Contributor II

Joshua, when you use:

arcpy.MakeFeatureLayer_management('input feature','output feature',"","",field_info)

do, you know where the new feature is automatically created? Trying to run tests but get an error since it already exists in it's location. Need to be able to change where it is saved and all. Also need to delete the original.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Use arcpy.Delete_management to remove the feature layer.  If you are making feature layers in a standalone or out of process script , they are created in program memory, you can't really save them in different places.  Unless the script adds the feature layer to a map document, there is no clean way to just list all the feature layers that have been created.  I think the expectation is that a user should track or know what feature layers they have made in their own script.

VishalShah2
Occasional Contributor II

How do you remove them from program memory? I did use arcpy.Delete_management to remove the feature layer (which did it make it disappear from TOC and data frame, but now when I try to test my script as a custom tool, I get an error saying that feature layer already exists. The goal is to run this tool at least 4 times a year. I was hoping that the naming of the new feature layer didn't have to change. I currently have at the end of the tool arcpy.Delete_management to delete the newly created feature layer every time to ensure that it can be used again when generating a report.

0 Kudos
VishalShah2
Occasional Contributor II

Joshua,

I figured out how to delete the program memory with the following line:

arcpy.Delete_management('in_memory',"")

I have that line at the beginning of my script to clear the memory and then at the end to clean it up before exiting the custom tool. This way no errors may occur with the temp data being used. Thanks for your help Joshua!

0 Kudos