2

I am stuck on how to create multiple features classes from one feature class using a select few fields. I have the following attribute table for a shapefile. enter image description here

I would like to select the 'USAF', 'COUNT', 'ADMIN_1' fields and append one year field from 2000, 2001, 2002...2014), such that the attribute fields of the new feature classes will be:

  1. 'USAF', 'COUNT', 'ADMIN_1', '2000'

  2. 'USAF', 'COUNT', 'ADMIN_1', '2001'

  3. 'USAF', 'COUNT', 'ADMIN_1', '2002'

    . . .

Till 2014.

I have been trying to work it out using modelbuilder and a python script (separately) and haven't been close to successful. Here is a script I wrote to test on the first year, but the resultant shapefile is identical to the input

 #import the arcpy site-package
 import arcpy
 from arcpy import env
 env.workspace = "Z:\\GEOG517\\siu850437305\\Project\\Results" #set current workspace
 env.overwriteOutput = True #allow outputs to be overwritten

 #List variables
 in_file = "Z:\\GEOG517\\siu850437305\\Project\\shapefiles\\sahel_ppt.shp"

 arcpy.MakeFeatureLayer_management(in_file, "stationslyr.shp")# Make a layer   from the feature class
 stations = "stationslyr"

 #Create field list iterate through using for loop
 fields = arcpy.ListFields(stations)
 for field in fields:
     for field.name == '2000':
         if field.name == "2000":
             arcpy.SelectLayerByAttribute_management(stations, "new_selection" )
             arcpy.CopyFeatures_management(stations, "yr2000")# Write the selected features to a new featureclass
             print ("Created yr2000")
             break

Then tried to append the years on using:

field_names = []
fields = arcpy.ListFields(stations)
for field in fields:
     field_names.append(field.name)

but that failed too! I am not even sure there is a tool to do this. I am new to python scritping for geoprocessing tasks so I am not looking for a really complex method just a simple and straightforward way to do this.

2 Answers 2

1

I found the answer here, it requires field mapping:
copy feature class and retain selected fields with arcpy
The documentation pages (for 10.1) are here:
field mappings, which are a collection of...
field map objects

There seem to be some typo issues in that first link's answer (and I didn't find it sufficiently explained), so I got it to work this way:

# feature class I want to copy from
existingFC = r'T:\GIS\Config\DatabaseConnections\GISP ReadOnly on 58u.sde\AGL.GasFacility\AGL.GasMain'
# list of fields I want to keep (capitalization counts!)
myfields = ['DATECREATED', 'DATEMODIFIED', 'SUBTYPECD', 'MEASUREDLENGTH', 'TRANSMISSION', 'NOP', 'FACILITYID', 'DATEPOSTED']
# create an empty field mapping object
mapS = arcpy.FieldMappings()
# for each field, create an individual field map, and add it to the field mapping object
for field in myfields :
    map = arcpy.FieldMap()
    map.addInputField(gasMain, field)
    mapS.addFieldMap(map)
# copy the feature class using the fields you want
newCopy = arcpy.FeatureClassToFeatureClass_conversion(existingFC, myDataBase, "new feature class name", "#", mapS)

The alternative answer I've found (which doesn't satisfy my needs because the process takes to long for the datasets I'm working with) is this:
drop fields after copying

-1

If you're looking for something straight-forward the answer probably won't be scripting it. If I understand you correctly, you want to create a separate feature class for each year containing the same fields except for the year. I would just copy your feature class 14 times and then delete the fields you don't want. Name the FCs in whatever dataset you paste them to something like Example2000, Example 2001, etc. Then use the delete fields tool to quickly remove your unwanted to fields. That method should take you less than an hour!

1
  • 1
    Thank you Adam. I did that already and its fine but since I am trying to automate the entire process I was looking for a scripting solution that I could use instead.
    – clarebare
    Commented Mar 18, 2016 at 18:28

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.