1

I'm going through a feature class and attempting to catch all the fields and their properties, and reuse them in a new output feature class. Because I'm doing some stuff to the original data in-between, arcpy.CopyFeatures_management() doesn't work for me here.

My code:

in_fc = <the feature class>
d = arcpy.Describe(in_fc)
in_sr = d.spatialReference
in_fc_name = d.baseName

out_path = "C:/Temp/temp.gdb"
out_fc_name = f"{d.baseName}_edit"

out_fields = []
fields = arcpy.ListFields(in_fc)
for f in fields:
    if f.type == "String":
        field = [f.baseName, f.type, f.aliasName, f.length]
        out_fields.append(field)
    else:
        field = [f.baseName, f.type, f.aliasName, f.precision]
        out_fields.append(field)

# sanity check
for field in out_fields:
    print(field)  # all looks fine

out_fc = arcpy.CreateFeatureclass_management(proj_db, out_fc_name, f"{d.shapeType.upper()}", spatial_reference=in_sr)  # works

arcpy.AddFields_management(out_fc, fields)  # doesn't work

Fails with a convertArcObjectToPythonObject(gp.AddFields_management(*gp_fixargs((in_table, field_description), True))) and a runtime error.

Since the fields appear correct, and since the ArcPy documentation indicates that the Field type property is mapped to the AddFields_management() type parameter, (meaning I should be able to pass in "String" and have it parsed as "TEXT") I'm not sure where this is going wrong.

1
  • 2
    Does your code work on a test feature class with one field?
    – PolyGeo
    Aug 2, 2021 at 20:14

2 Answers 2

1

It seems you can skip the arcpy.AddFields_management() step if you simply include the template parameter in the arcpy.CreateFeatureclass_management() step.

Here is my test code, modified:

import arcpy
import os

proj_db = r"C:\Temp\project_database.gdb"
in_fc = r"C:\Temp\test_test123.gdb\test"
d = arcpy.Describe(in_fc)
in_sr = d.spatialReference
in_fc_name = d.baseName

out_fc_name = f"{d.baseName}_edit"
    
out_fc = arcpy.CreateFeatureclass_management(
        proj_db, 
        out_fc_name, 
        f"{d.shapeType.upper()}", 
        spatial_reference=in_sr, 
        template=in_fc
        ) 
1
  • 1
    MUCH better than the duct-tapey way I was going about this, appreciate it!
    – auslander
    Aug 3, 2021 at 17:07
1

First of all you are are looping through the fields adding to a list called out_fields but then you feed in the list fields into the AddFields tool...

Look at the Parameters section of the help file for the AddFields tool. What are you adding (or should say had intended to add)? Now look again at the Parameters section of the Field Properties and what does it actually want? A ValueTable not a list of lists.

2
  • 2
    This is very helpful, I was unaware of the ValueTable object. Only reason I didn't mark it as the answer is because Keggering pointed out that I was doing it wrong from the beginning, and I should have been simply using feature class 1 as a template for feature class 2.
    – auslander
    Aug 3, 2021 at 17:06
  • 1
    @auslander I would not say you were doing it wrong (although you were ;) ), your approach allows you to say change an alias, bump up the field length of text field etc. Keggering's suggestion works if the template is already exactly what you want, which is often the case, but if you need to tweak things then your approach is the way to go.
    – Hornbydd
    Aug 3, 2021 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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