Making fields hidden/visible using MakeFeatureLayer help

8554
9
06-29-2011 02:53 AM
HalilSiddique
Occasional Contributor II
Hi all,

I have an SDE layer, that I am using in my script, that becomes a Feature layer (to be used later in my script). Of the fields within the feature class, I only need a few, and I have reflected this in the script but it still appears to keep all the fields.

    
# Create Feature Layer from BLPU point
    gp.makeFeatureLayer_management(inputPoint, "point_lyr", "", "","UPRN UPRN VISIBLE; ADDRESS ADDRESS VISIBLE")
    print "1 - ", inputPoint, "is now a feature layer"
    
    # Export Joined Point layer
    gp.copyFeatures_management("point_lyr", "READY_FOR_SDE_test")
    print "14 - ", "Joined points has been exported to", datadump
Any ideas on how to get this working?

Cheers

Halil
Tags (2)
0 Kudos
9 Replies
ChrisSnyder
Regular Contributor III
Here is some code that does a similar thing... been working great for years...

fieldInfo = ""
fieldList = gp.listfields(layerPath)
for field in fieldList:
    if field.name == "PARCEL_SYST_ID":
        fieldInfo = fieldInfo + field.Name + " " + "PARCEL_ID" + " VISIBLE;"
    elif field.name == "SURFACE_TRUST_CD":
        fieldInfo = fieldInfo + field.Name + " " + "SUR_OWN_CD" + " VISIBLE;"
    elif field.name == "SURFACE_TRUST_NM":
        fieldInfo = fieldInfo + field.Name + " " + "SUR_OWN_NM" + " VISIBLE;"
    elif field.name == "TIMBER_TRUST_CD":
        fieldInfo = fieldInfo + field.Name + " " + "TIM_OWN_CD" + " VISIBLE;"
    elif field.name == "TIMBER_TRUST_NM":
        fieldInfo = fieldInfo + field.Name + " " + "TIM_OWN_NM" + " VISIBLE;"
    else:
        fieldInfo = fieldInfo + field.Name + " " + field.Name + " HIDDEN;"
gp.MakeFeatureLayer_management(layerPath, "feature_layer", "", "", fieldInfo[:-1]); showGpMessage()
gp.MultipartToSinglepart_management("feature_layer", fgdbPath + "\\" + layerName); showGpMessage()
0 Kudos
ChrisSnyder
Regular Contributor III
The issue you are having is that you need to list EVERY field in the table and indicate "ORIGINAL_NAME NEW_NAME HIDDEN/VISIBLE"

Maybe something like this:

keepFieldList = ("FIELD1","FIELD2")
fieldInfo = ""
fieldList = gp.listfields(layerPath)
for field in fieldList:
    if field.name in keepFieldList:
        fieldInfo = fieldInfo + field.Name + " " + field.name + " VISIBLE;"
    else:
        fieldInfo = fieldInfo + field.Name + " " + field.Name + " HIDDEN;"
gp.MakeFeatureLayer_management(layerPath, "feature_layer", "", "", fieldInfo[:-1]); showGpMessage()
0 Kudos
MarcusSilva1
New Contributor II
Hi All,

What's the use of fieldinfo's method "setVisible (index, visible)" worth for? It seems it doesn't work or I'm doing something wrong.  I'm trying to use

        while index < fieldInfo.count:
            arcpy.AddMessage(fieldInfo.GetVisible(index))         
            fieldInfo.setVisible(index, "HIDDEN")
            arcpy.AddMessage('fieldInfo.GetVisible(index))         
            index += 1

in my python script but nothing happens. Any ideas?

Thanks,

Marcus
0 Kudos
ChrisSnyder
Regular Contributor III
I also couldn't get the fieldInfo() methods to work... For example:

>>> fc = r"C:\csny490\test.gdb\haul_routes_lt_flat_l_1_3"
>>> arcpy.MakeFeatureLayer_management(fc,"fl")
>>> dsc = arcpy.Describe("fl")
>>> dsc.FieldInfo.exportToString ()
u'OBJECTID OBJECTID VISIBLE NONE;Shape Shape VISIBLE NONE;FacilityID FacilityID VISIBLE NONE;ALTERNATIVE ALTERNATIVE VISIBLE NONE;DECADE DECADE VISIBLE NONE;SPATIAL_ID SPATIAL_ID VISIBLE NONE;MBF MBF VISIBLE NONE;Shape_Length Shape_Length VISIBLE NONE'
>>> dsc.FieldInfo.setFieldName(3, "ALT") #This should rename ALTERANTIVE to ALT
>>> dsc.FieldInfo.setVisible(4, "HIDDEN") #This should make DECADE to HIDDEN
>>> dsc.FieldInfo.exportToString ()
u'OBJECTID OBJECTID VISIBLE NONE;Shape Shape VISIBLE NONE;FacilityID FacilityID VISIBLE NONE;ALTERNATIVE ALTERNATIVE VISIBLE NONE;DECADE DECADE VISIBLE NONE;SPATIAL_ID SPATIAL_ID VISIBLE NONE;MBF MBF VISIBLE NONE;Shape_Length Shape_Length VISIBLE NONE'
>>> arcpy.CopyFeatures_management("fl",r"C:\csny490\test.gdb\test333")
<Result 'C:\\csny490\\test.gdb\\test333'>

The changes I made to the feature layer were also not reflected when I exported it out to a FeatureClass (test333).

Hmmm... I must be missing something here too.

I'll bet it still works if you build a proper text sting and feed it into the MakeFeatureLayer tool.
0 Kudos
ChrisSnyder
Regular Contributor III
I also couldn't get the fieldInfo() methods to work... For example:

>>> fc = r"C:\csny490\test.gdb\haul_routes_lt_flat_l_1_3"
>>> arcpy.MakeFeatureLayer_management(fc,"fl")
>>> dsc = arcpy.Describe("fl")
>>> dsc.FieldInfo.exportToString ()
u'OBJECTID OBJECTID VISIBLE NONE;Shape Shape VISIBLE NONE;FacilityID FacilityID VISIBLE NONE;ALTERNATIVE ALTERNATIVE VISIBLE NONE;DECADE DECADE VISIBLE NONE;SPATIAL_ID SPATIAL_ID VISIBLE NONE;MBF MBF VISIBLE NONE;Shape_Length Shape_Length VISIBLE NONE'
>>> dsc.FieldInfo.setFieldName(3, "ALT") #This should rename ALTERANTIVE to ALT
>>> dsc.FieldInfo.setVisible(4, "HIDDEN") #This should make DECADE to HIDDEN
>>> dsc.FieldInfo.exportToString ()
u'OBJECTID OBJECTID VISIBLE NONE;Shape Shape VISIBLE NONE;FacilityID FacilityID VISIBLE NONE;ALTERNATIVE ALTERNATIVE VISIBLE NONE;DECADE DECADE VISIBLE NONE;SPATIAL_ID SPATIAL_ID VISIBLE NONE;MBF MBF VISIBLE NONE;Shape_Length Shape_Length VISIBLE NONE'
>>> arcpy.CopyFeatures_management("fl",r"C:\csny490\test.gdb\test333")
<Result 'C:\\csny490\\test.gdb\\test333'>


The changes I made to the feature layer were also not reflected when I exported it out to a FeatureClass (test333).

Hmmm... I must be missing something here too.

I'll bet it still works if you build a proper text sting and feed it into the MakeFeatureLayer tool.
0 Kudos
curtvprice
MVP Esteemed Contributor
I also couldn't get the fieldInfo() methods to work


In your code example you are trying to modify a read-only describe object, not the layer the describe object was created from.

I think the most straighforward way would be to create a new layer with MakeFeatureLayer, setting the field properties in the arguments.

A less straightforward way would be to dive into arcpy.mapping's layer object. (Suggestions from arcpy.mapping mavens to this thread are welcome!)
0 Kudos
ChrisSnyder
Regular Contributor III
I would think the set*() methods under fieldInfo() are meant to be writeable.... The get*() methods of course are read only...

From the documentation:
-------------------------

setFieldName (index, field_name): Sets the field name into the table.
setNewName (index, new_field_name): Sets the new field name into the table.
setSplitRule (index, rule): Sets the split rule into the table.
setVisible (index, visible): Set the visible flag of a field on the table.
0 Kudos
KellyWhitehead
New Contributor II
I've been having similar frustrations with FieldInfo in my script as well. I ran a quick test in the python window:

>>> test = arcpy.FieldInfo()
>>> test.addField("Name","NewName","VISIBLE","NONE")
>>> test.setVisible(0,"HIDDEN")
>>> test.getVisible(0)
u'VISIBLE'
>>> test.getSplitRule(0)
u'HIDDEN'

The setVisible method appears to be changing the SplitRule instead.

Looks like a bug to me...
0 Kudos
kevindevine
New Contributor

This is what I used to get this working from the examples above.  After modifying the fieldInfo for the previously created memorynet feature layer, I created another feature layer from that and then used the "in_memory//" to create yet another layer which had the correctly modified fields. 

keepFieldList = ("Field1","Field2", "Field3", "Field4", "Field5")
fieldInfo = ""
fieldList = gp.listfields(memorynet)
for field in fieldList:
 if field.name in keepFieldList:
 print field.name
 fieldInfo = fieldInfo + field.Name + " " + field.name + " VISIBLE;"
 else:
 fieldInfo = fieldInfo + field.Name + " " + field.Name + " HIDDEN;"
print fieldInfo
gp.MakeFeatureLayer_management(memorynet, "feature_layer", "", "", fieldInfo[:-1])
memfldNet = "in_memory//memfldNet"
arcpy.CopyFeatures_management("feature_layer", memfldNet)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos