Python arcpy.FieldMappings() Examples

The following are 6 code examples of arcpy.FieldMappings(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module arcpy , or try the search function .
Example #1
Source File: CreatePercentAccessPolygon.py    From public-transit-tools with Apache License 2.0 7 votes vote down vote up
def generate_field_map(in_time_lapse_polys, fields_to_preserve):
    '''Create a FieldMappings object to use in Spatial Join.  For our application, we only want to preserve a few fields
    for informational purposes.  We expect all these field values to be the same, so use the "First" rule so the output
    polygon will just keep the same value as the inputs.  All other fields in the input data will not be transferred to
    the output.
    Params:
    in_time_lapse_polys: Time lapse polygon feature class from which to retrieve the fields
    fields_to_preserve: A list of field names we want to keep for the output.
    '''
    field_mappings = arcpy.FieldMappings()
    for field in fields_to_preserve:
        fmap = arcpy.FieldMap()
        fmap.addInputField(in_time_lapse_polys, field) 
        fmap.mergeRule = "First"
        field_mappings.addFieldMap(fmap)
    return field_mappings 
Example #2
Source File: CreatePercentAccessPolygon.py    From public-transit-tools with Apache License 2.0 6 votes vote down vote up
def create_raw_cell_counts_fc(selected_time_lapse_polys, in_poly_raster_template, temp_spatial_join_fc, fmaps, 
                              match_option):
    '''Do a spatial join in order to count the number of time lapse polygons intersect each "cell" in the raster-like
    polylgon template.  We are effectively applying the template to a specific set of time lapse polygons, doing the
    count, and creating the raw output.  The result is a polygon feature class of raster-like cells with a field called
    Join_Count that shows the number of input time lapse polygons that intersect the cell using the specified
    match_option.
    Params:
    selected_time_lapse_polys: Set (or subset) of time lapse polygons to use
    in_poly_raster_template: The raster-like polygon feature class produced from create_polygon_raster_template()
    temp_spatial_join_fc: Path to a temporary output FC which we will overwrite each time this method is called and then
        delete at the end of the tool during clean-up
    fmaps: FieldMappings object indicating which fields to preserve
    match_options: match_options parameter for the Spatial Join tool
    '''

    arcpy.analysis.SpatialJoin(
        in_poly_raster_template,
        selected_time_lapse_polys,
        temp_spatial_join_fc,
        "JOIN_ONE_TO_ONE", # Output keeps only one copy of each "cell" when multiple time lapse polys intersect it
        "KEEP_COMMON", # Delete any "cells" that don't overlap the time lapse polys being considered
        field_mapping=fmaps, # Preserve some fields from the original data
        match_option=match_option
        ) 
Example #3
Source File: gptools.py    From utilities-solution-data-automation with Apache License 2.0 4 votes vote down vote up
def assignFieldsByIntersect(sourceFC, assignFC, fieldsToAssign, outputFC,report_areas_overlap):
    tempWorkspace = arcpy.env.scratchGDB

    assignFields = arcpy.ListFields(dataset=assignFC)
    assignFieldsNames = [f.name for f in assignFields]

    sourceFields = arcpy.ListFields(dataset=sourceFC)
    sourceFieldNames = [f.name for f in sourceFields]
    newFields = []

    fms = arcpy.FieldMappings()
    for fieldToAssign in fieldsToAssign:
        if fieldToAssign not in assignFieldsNames:
            raise ValueError("{0} does not exist in {1}".format(fieldToAssign,assignFC))
        outputField = fieldToAssign
        if fieldToAssign in sourceFieldNames + newFields:
            outputField = Common.uniqueFieldName(fieldToAssign, sourceFieldNames + newFields)

        newFields.append(outputField)

        fm = arcpy.FieldMap()
        fm.addInputField(assignFC, fieldToAssign)
        type_name = fm.outputField
        type_name.name = outputField
        fm.outputField = type_name
        fms.addFieldMap(fm)



    fieldmappings = arcpy.FieldMappings()
    #fieldmappings.addTable(assignFC)
    #fieldmappings.removeAll()
    fieldmappings.addTable(sourceFC)
    for fm in fms.fieldMappings:
        fieldmappings.addFieldMap(fm)

    if report_areas_overlap:
        join_operation = "JOIN_ONE_TO_MANY"
    else:
        join_operation = "JOIN_ONE_TO_ONE"
    outputLayer = arcpy.SpatialJoin_analysis(target_features=sourceFC,
                                             join_features=assignFC,
                                             out_feature_class=outputFC,
                                             join_operation=join_operation,
                               join_type="KEEP_COMMON",
                              field_mapping=fieldmappings,
                              match_option="HAVE_THEIR_CENTER_IN",
                              search_radius=None,
                              distance_field_name=None)[0]


    return outputLayer 
Example #4
Source File: maintain_attachments.py    From collector-tools with Apache License 2.0 4 votes vote down vote up
def enable_copy_attachments(input_fc, output_fc):
    # Check if the input feature class has attachments table
    input_attachment_table = input_fc + '__ATTACH'
    if not arcpy.Exists(input_attachment_table):
        desc = arcpy.Describe(input_fc)
        input_attachment_table = desc.Path.split('.')[0] + '.gdb\\' + desc.Name + '__ATTACH'
                            
        if not arcpy.Exists(input_attachment_table):
            arcpy.AddMessage("Unable to locate the attachment table for the input feature class.")

    else:

        # Enable Attachments
        arcpy.AddMessage("Enabling Attachments")
        arcpy.EnableAttachments_management(output_fc)
        arcpy.AddMessage("Enabled Attachments")

        # Copy Attachments from Input feature class to Temp feature class.
        arcpy.AddMessage("Copying Attachments..")

        outputTable = output_fc + '__ATTACH'

        try:
            # Check if the input feature class was related to the attachment tables via the ObjectID field.
            input_table_desc = arcpy.Describe(input_attachment_table)
            field_rel_objectID = [field for field in input_table_desc.fields if field.name.lower() == 'rel_objectid']

            # If the input attachment table has REL_OBJECTID field then remap GUID fields between input and output attachment table.
            if field_rel_objectID:
                field_rel_globalID = [field for field in input_table_desc.fields if field.type.lower() == 'guid']
                if field_rel_globalID:
                    output_field = field_rel_globalID[0]
                else:
                    arcpy.AddError("Can't copy attachments...")

                output_table_field_mappings = arcpy.FieldMappings()
                output_table_field_mappings.addTable(outputTable)

                input_table_field_mappings = arcpy.FieldMappings()
                input_table_field_mappings.addTable(input_attachment_table)

                output_table_globalID = [field for field in output_table_field_mappings.fields if field.type.lower() == 'guid'][0]
                field_index = output_table_field_mappings.findFieldMapIndex(output_table_globalID.name)
                fmap = output_table_field_mappings.fieldMappings[field_index]
                output_table_field_mappings.removeFieldMap(field_index)
                fmap.addInputField(input_attachment_table,output_field.name)
                output_table_field_mappings.addFieldMap(fmap)

                for input_field_map in input_table_field_mappings.fieldMappings:
                    output_table_field_mappings.addFieldMap(input_field_map)

                arcpy.Append_management(input_attachment_table, outputTable, 'NO_TEST', output_table_field_mappings)
            else:
                arcpy.Append_management(input_attachment_table, outputTable)
            arcpy.AddMessage("Copied Attachments..")
        except Exception as e:
            arcpy.AddError(e) 
Example #5
Source File: Select_Mapunits_by_Project.py    From geo-pit with GNU General Public License v2.0 4 votes vote down vote up
def GetFieldInfo(fc):

    # Create and return FieldMapping object containing valid project record fields. Fields
    # that are not part of project record feature class will not be appended to the project
    # record fc.

    try:

        outFields = ['AREASYMBOL','MUSYM','PROJECT_NAME','STATUS','RECERT_NEEDED']

        # Create required FieldMappings object and add the fc table as a
        # FieldMap object
        fms = arcpy.FieldMappings()
        fms.addTable(fc)

        # loop through each field in FieldMappings object
        for fm in fms.fieldMappings:

            # Field object containing the properties for the field (aliasName...)
            outFld = fm.outputField

            # Name of the field
            fldName = outFld.name

            # remove field from FieldMapping object if it is 'OID' or 'Geometry'
            # or not in dFieldInfo dictionary (SSURGO schema)
            if not fldName in outFields:
                fms.removeFieldMap(fms.findFieldMapIndex(fldName))

        for fldName in outFields:
            newFM = fms.getFieldMap(fms.findFieldMapIndex(fldName))
            fms.removeFieldMap(fms.findFieldMapIndex(fldName))
            fms.addFieldMap(newFM)

        return fms

    except:
        errorMsg()
        fms = arcpy.FieldMappings()
        return fms

## =================================================================================== 
Example #6
Source File: Select_Mapunits_by_Project_NATSYM.py    From geo-pit with GNU General Public License v2.0 4 votes vote down vote up
def GetFieldInfo(fc):

    # Create and return FieldMapping object containing valid project record fields. Fields
    # that are not part of project record feature class will not be appended to the project
    # record fc.

    try:

        outFields = ['AREASYMBOL','MUSYM','PROJECT_NAME','STATUS','RECERT_NEEDED']

        # Create required FieldMappings object and add the fc table as a
        # FieldMap object
        fms = arcpy.FieldMappings()
        fms.addTable(fc)

        # loop through each field in FieldMappings object
        for fm in fms.fieldMappings:

            # Field object containing the properties for the field (aliasName...)
            outFld = fm.outputField

            # Name of the field
            fldName = outFld.name

            # remove field from FieldMapping object if it is 'OID' or 'Geometry'
            # or not in dFieldInfo dictionary (SSURGO schema)
            if not fldName in outFields:
                fms.removeFieldMap(fms.findFieldMapIndex(fldName))

        for fldName in outFields:
            newFM = fms.getFieldMap(fms.findFieldMapIndex(fldName))
            fms.removeFieldMap(fms.findFieldMapIndex(fldName))
            fms.addFieldMap(newFM)

        return fms

    except:
        errorMsg()
        fms = arcpy.FieldMappings()
        return fms

## ===================================================================================