1

This is not a copy of previous questions like Selecting certain fields from feature class to create new feature class in ArcPy? but builds on them.

I have two approaches and two different issues.

Project: Take numerous feature classes in several SDE's and create a shapefile of each FC but only containing a selection of fields.

Method 1:

I copied the entire Feature Class to a new GDB, made a feature layer from that feature class and iterate through the layer to create a list of "delete fields" by excluding all of the already established "keep fields." I then create a new shapefile from the feature layer that deletes all fields I don't want. This works fine except when subtypes are discovered.

I wrote the following to handle subtypes:

subtypes = arcpy.da.ListSubtypes(tempLayer)
sKeys = subtypes.keys()
for keys in sKeys:
    if keys != 0:
        print("Subtype found at code {}.".format(keys))
        sys.stdout.flush()
        arcpy.RemoveSubtype_management(tempLayer,keys) 

However, I still get errors saying fields cannot be deleted due to subtypes. When I look into/print the subtypes found in arcpy.da.ListSubtypes(tempLayer) ...no subtypes are found. So, with Method 1: What am I missing in identifying and deleting subtypes?

2
  • Please ask only one question per Question. Shapefiles cannot preserve datetime values (dBase only supports YYYYMMDD), so this would be a lossy conversion. Working with 10.2, which was been retired since July, and used an older/buggy Python, isn't going to make it easier to get assistance. – Vince Mar 2 '20 at 16:29
  • If only it was easy to use a newer version. The existing geometric network used doesn't support 10.3 and makes navigating between all the different servers a pain unless 10.2 is used until everything gets properly updated to Pro or 10.6.1 – JackOfTales Mar 2 '20 at 17:33
1

Instead of copying and deleting, I like to create a feature layer and hide fields, then export. I've created a function for this task. Subtypes don't affect this method.

def CreateFieldInfoLayer (fc, lyrName, tempName, flds):
    """
    fc = input feature class
    lyrName = name of output layer
    tempName = name of temporary layer. It will be created and deleted
    flds = keep fields
    """
    #get field info
    tempName = arcpy.MakeFeatureLayer_management (fc, tempName) [0]
    fldInfo = arcpy.Describe (tempName).fieldInfo
    #set fields not 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
    lyrName = arcpy.MakeFeatureLayer_management (tempName, lyrName, field_info = fldInfo) [0]
    #delete initial layer
    arcpy.Delete_management (tempName)
    return lyrName

#input table
inFc = r"C:\Some\Featureclass"
#keep fields
keepFields = ["field1", "field2"]
#create layer with limited fields
lyr = CreateFieldInfoLayer (inFc, "layer", "templayer", keepFields)

#copy features
arcpy.CopyFeatures_management (lyr, r"C:\Some\Featureclass_output")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.