Tuesday, April 25, 2017

Using Field Info to Limit Fields When Field Mapping Isn't Available

I work with feature classes that can have several dozens of fields, sometimes numbering in the triple digits. Once I start a geoprocessing workflow, I'm often only interested in one or two fields from my input feature classes for my analysis. Usually field mappings are the way to go to limit fields. However, certain tools don't have field mappings as an input, so on its own the user is stuck keeping all fields of all inputs involved. Instead of deleting unwanted fields after a geoprocessing it's possible to limit fields by first creating layers with hidden fields using field info. In the below example I've written a function that creates feature layers with unwanted fields set to hidden. I then use these layers in an intersect, which produces a feature class whose attribute table contains only the fields that were set to visible.
The code:

import arcpy

def ApplyFldInfo (fc, flds):
    """
    create layer with desired fields hidden
    fc: the full path of the input feature class
    flds: a python list of fields to remain visible
    emilharold@gmail.com
    """
    #create layer with unique name
    layer = "layer"
    i = 0
    while arcpy.Exists (layer):
            layer = "layer_{}".format (i)
            i += 1
    arcpy.MakeFeatureLayer_management (fc, layer)
    #get field info
    fldInfo = arcpy.Describe (layer).fieldInfo
    #set fields no in input list to hidden
    for i in range (fldInfo.count):
            fldName = fldInfo.getFieldName (i)
            if not fldName in flds:
                    fldInfo.setVisible (i, "HIDDEN")
    #create new layer with field info applied
    outLayer = "outLayer"
    i = 0
    while arcpy.Exists (outLayer):
            outLayer = "outLayer_{}".format (i)
            i += 1
    arcpy.MakeFeatureLayer_management (layer, outLayer,
                                       field_info = fldInfo)
    #delete initial layer
    arcpy.Delete_management (layer)
    return outLayer

#first feature class
fc1 = r"C:\Path\To\Fc1"
#keep field in first feature class
fld1 = "field1"
#second feature class
fc2 = r"C:\Path\To\Fc2"
#keep fields in second feature class
fld2 = "field2"
fld3 = "field3"

#create layers with fieldinfo
layer1 = ApplyFldInfo (fc1, [fld1])
layer2 = ApplyFldInfo (fc2, [fld2, fld3])

#intersect
outFc = r"C:\Path\To\Output"
arcpy.Intersect_analysis ([layer1, layer2], outFc)

#clean up
for layer in [layer1, layer2]:
    arcpy.Delete_management (layer)
profile for Emil Brundage at Geographic Information Systems Stack Exchange, Q&A for cartographers, geographers and GIS professionals

4 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
  3. This is my first-time i visit here. I found so many entertaining stuff in your blog, especially its discussion. From the a great deal of comments in your articles, I suppose I am not the only person having all the leisure here! Continue the superb work.solve your afp problems

    ReplyDelete