[GIS] Copying only certain Fields/Columns from shapefile into new shapefile using Modelbuilder in ArcCatalog

arcgis-10.2arcgis-desktopmodelbuilder

I am trying to take a shapefile, with way too many fields/columns, and copy over only certain fields into a new shapefile.

Normally, I would just delete the fields I don't need from the original shapefile, but I don't want to come across an instance where the GIS operator decides to remove a field that my automated .py scripts is trying to delete, causing the program to fail.

At this point, I have not physically tried anything because every tool I have come across I have been able to find documentation proving it's lack of usefulness (Fields -> Transpose Field and General -> Copy).

Transpose Field

Fields -> Transpose Field does copy individual fields but it transposes them from a column into a row appendage, which does not help.

General -> Copy copies the entire shapefile and will not let me choose only certain columns. What I want is something similar to this tool but one that will allow me to choose only certain fields to include in the copy.

I use "Feature Class To Shapefile (multiple)" to convert my features into shapefiles, and I tried looking into a "Feature to Shapefile" solo Tool that would let me pick the columns/attributes of the feature to convert. That has so far turned up empty as well.

I am using the ArcCatalog ModelBuilder to perform this task, not raw Python scripts. I know enough Python to go that route if needed, but a Toolbox Tool is the preferred goal. This task will be part of a larger whole that will be run via a batch file calling the exported Python code from the Models. It has to be something that can be run automatically, so manually copying/pasting is not applicable.

As for a screenshot of the Model, my current setup has each task as individual models: one model copies rows, one creates shapefile from feature class, one adds a field, one populates that field, etc. I'm sure this is probably inefficient but this task has been passed on to me from someone else and this was how it was done. I can add screenshots of the other models I have but I'm not sure what use that will provide, as it doesn't give much of a context.

Basically, I'm looking to do something like Copying data from one attribute field to another using ArcGIS Field Calculator? but with about 10 different fields. I don't want to do them individually, but using one or maybe two tools.

Best Answer

You can take advantage of using a FieldMappings parameter type. There are many tools that support this, you probably want to use the Feature Class To Feature Class tool (which also works on shapefiles). You can alter the field map there for use in Model Builder.

If you are using Python, you can pass in a list of fields that you want to keep like this:

import arcpy
import os

def copy_with_fields(in_fc, out_fc, keep_fields, where=''):
    """
    Required:
        in_fc -- input feature class
        out_fc -- output feature class
        keep_fields -- names of fields to keep in output

    Optional:
        where -- optional where clause to filter records
    """
    fmap = arcpy.FieldMappings()
    fmap.addTable(in_fc)

    # get all fields
    fields = {f.name: f for f in arcpy.ListFields(in_fc)}

    # clean up field map
    for fname, fld in fields.iteritems():
        if fld.type not in ('OID', 'Geometry') and 'shape' not in fname.lower():
            if fname not in keep_fields:
                fmap.removeFieldMap(fmap.findFieldMapIndex(fname))

    # copy features
    path, name = os.path.split(out_fc)
    arcpy.conversion.FeatureClassToFeatureClass(in_fc, path, name, where, fmap)
    return out_fc

if __name__ == '__main__':

    fc = r'C:\TEMP\test2.gdb\Rental_Licenses'
    new = r'C:\TEMP\test2.gdb\Rental_Licenses_filtered'
    fields = ['Match_addr', 'PROPERTY_O', 'OWNER_ADDR'] #only these fields and other required fields will be included
    copy_with_fields(fc, new, fields)

I know you can also use the DeleteField tool, but I have found that to be very slow in tables that have lots of fields.

Related Question