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)
This comment has been removed by the author.
ReplyDeleteGreat site!
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis 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