Copy attribute fields from one feature class to New output feature class

10-30-2018 07:31 AM
Occasional Contributor

I have the following script - it creates lines from two points feature classes,  Both point feature classes  have a field
# that identifies the center point ID to relate them.

I would like to modify this script so I can I get all the fields from the related points  relfc to spiderfc

# Description: Creates spider diagram lines from two point feature classes.
# Note: You need one point feature class for the center points and another
#       for the related points.  Both point feature classes must have a field
#       that identifies the center point ID to relate them.
# ---------------------------------------------------------------------------

# Import modules
import arcpy

# Local variables
cenfc = r"\\GIS\starburst.gdb\CenterPoints"
cenfld = "SiteID"
relfc = r"\\GIS\starburst.gdb\EV102218" " copy all fields from here
relfld = "USER_SiteID"
spiderfc = r"\\GIS\starburst.gdb\Spider"  #  to here 

# Read in the ID values and coordinates of the center points
print "Reading in center point IDs and coordinates..."
cendict = {}
for row in arcpy.da.SearchCursor(cenfc, [cenfld, "SHAPE@XY"]):
  cendict[row[0]] = row[1]

# Read in the ID values and coordinates of the related points and create line geometry objects
print "Reading in related point IDs and coordinates..."
linelist = []
for row in arcpy.da.SearchCursor(relfc, [relfld, "SHAPE@XY"]):
  relxy = row[1]
  cenxy = cendict[row[0]]
  polyline = arcpy.Polyline(arcpy.Array([arcpy.Point(cenxy[0], cenxy[1]), arcpy.Point(relxy[0], relxy[1])]))
  linelist.append([row[0], polyline])

# Setup line feature for spider lines
print "Setting up line feature for spider lines..."
sr = arcpy.Describe(cenfc).spatialReference
tempfc = "in_memory/tempfc"
arcpy.CreateFeatureclass_management(tempfc.split("/")[0], tempfc.split("/")[1], "POLYLINE", "", "", "", sr)
field = arcpy.ListFields(cenfc, cenfld)
fielddict = {"SmallInteger": "SHORT", "Integer": "LONG", "Single": "FLOAT", "Double": "DOUBLE", "String": "TEXT", "Date": "DATE", "BLOB": "BLOB"}
fieldtype = fielddict.get(field[0].type)
if fieldtype == "TEXT":
  arcpy.AddField_management(tempfc, cenfld, fieldtype, "", "", str(field[0].length))
  arcpy.AddField_management(tempfc, cenfld, fieldtype, "", "", "", "", "NULLABLE")

# Add spider lines
print "Adding spider lines..."
cursor = arcpy.da.InsertCursor(tempfc, [cenfld, "SHAPE@"])
for line in linelist:
del cursor

# Save spider lines to spider line feature
print "Saving spider lines..."
arcpy.env.overwriteOutput = True
arcpy.CopyFeatures_management(tempfc, spiderfc)

# All done
print "All done!"‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Many thanks

Tags (2)
0 Kudos
6 Replies
MVP Honored Contributor

Where are you having trouble?

0 Kudos
Occasional Contributor

Thanks,  the script is working fine, but I need to copy all the attributes fields from relfc

to the new spiderfc  feature


0 Kudos
MVP Honored Contributor

Try Create Feature Class, using relfc as the template: Create Feature Class—Help | ArcGIS for Desktop 

0 Kudos
Occasional Contributor

Yes Darren,  this what I did in line 36, 

arcpy.CreateFeatureclass_management(tempfc.split("/")[0], tempfc.split("/")[1], "POLYLINE", "", "", "", sr)

but how to include all the fields from the relfc, in_memory then copy them as I did on line 55

0 Kudos
MVP Honored Contributor

Check the examples and help here: Create Feature Class—Help | ArcGIS for Desktop 

The template feature class is the fourth parameter - the new feature class will contain the fields in the template feature class.

Occasional Contributor

Thank you,  Yes I am able to use the template,  but i got only the empty schema, no data.

0 Kudos