2

Here is the edited code:

import arcpy
import os
from arcpy import env

#set the environment settings
env.workspace = "C:\RG\Kings County\Test.gdb"
#fcs = arcpy.ListFeatureClasses("Sub_NVF_*")
output = "C:\RG\Kings County\Test.gdb\Merge3FC"

arcpy.env.overwriteOutput = 1 # this code overwrites existing output
try:

        FC1 = "Sub_NVF_050413"
        FC2 = "Sub_NVF_052503"
        FC3 = "Sub_NVF_052509"

        fldMap_Mean = arcpy.FieldMap()
        fldMap_NDVI = arcpy.FieldMap()
        fldMap_ID = arcpy.FieldMap()
        fieldMappings = arcpy.FieldMappings()

        Mean1 = "M050413"
        Mean2 = "M052503"
        Mean3 = "M052509"
        NDVI1 = "NDVI050413"
        NDVI2 = "NDVI052503"
        NDVI3 = "NDVI052509"
        Fld_ID = "Field_ID"

        fldMap_Mean.addInputField(FC1,Mean1)
        fldMap_Mean.addInputField(FC2, Mean2)
        fldMap_Mean.addInputField(FC3, Mean3)

        fldMap_NDVI.addInputField(FC1, NDVI1)
        fldMap_NDVI.addInputField(FC2, NDVI2)
        fldMap_NDVI.addInputField(FC3, NDVI3)

        fldMap_ID.addInputField(FC1, Fld_ID)
        fldMap_ID.addInputField(FC2, Fld_ID)
        fldMap_ID.addInputField(FC3, Fld_ID)

        #set name of new output field
        Mean_type = fldMap_Mean.outputField
        Mean_type.name = 'Mean'
        Mean_type.aliasName = 'Mean'
        fldMap_Mean.outputField = Mean_type
        fieldMappings.addFieldMap(fldMap_Mean)

        NDVI_type = fldMap_NDVI.outputField
        NDVI_type.name = 'NDVI'
        NDVI_type.aliasName = 'NDVI'
        fldMap_NDVI.outputField = NDVI_type
        fieldMappings.addFieldMap(fldMap_NDVI)

        FieldID_type = fldMap_ID.outputField
        FieldID_type.name = 'Field_ID'
        FieldID_type.aliasName = 'Field_ID'
        fldMap_ID.outputField = FieldID_type
        fieldMappings.addFieldMap(fldMap_ID)

        arcpy.Merge_management([FC1, FC2, FC3], output, fieldMappings)

except Exception as e:
    print e

Above code works fine. Although mean and NDVI field name are feature class specific, it perfectly does the job. However, when I use for loop and try to read field name as as Mean = "M"+ fname (as shown below), it gives me an error saying "Error in adding input field to field map"

import arcpy
import os
from arcpy import env


env.workspace = "C:\RG\Kings County\Test.gdb"
fcs = arcpy.ListFeatureClasses("Sub_NVF_*")
output = "C:\RG\Kings County\Test.gdb\MergeAllV1"


try:
    for fc in fcs:
        fcname = fc.split('_', 1)[1]

        fldMap_Mean = arcpy.FieldMap()
        fldMap_ID = arcpy.FieldMap()
        fieldMappings = arcpy.FieldMappings()

        Mean = "M"+fcname
        Fld_ID = "Field_ID"

        fldMap_Mean.addInputField(fc, Mean)
        fldMap_ID.addInputField(fc, Fld_ID)


        Mean_type = fldMap_Mean.outputField
        Mean_type.name = 'Mean'
        Mean_type.aliasName = 'Mean'
        fldMap_Mean.outputField = Mean_type
        fieldMappings.addFieldMap(fldMap_Mean)

        FieldID_type = fldMap_ID.outputField
        FieldID_type.name = 'Field_ID'
        FieldID_type.aliasName = 'Field_ID'
        fldMap_ID.outputField = FieldID_type
        fieldMappings.addFieldMap(fldMap_ID)

    arcpy.Merge_management(fc, output, fieldMappings)
    print ("done")

except Exception as e:
    print e

However, if I try to merge all feature classes using fields whose name is same in all files (as shown below), it writes only the last file. Any clue?

import arcpy
import os
from arcpy import env

env.workspace = "C:\RG\Kings County\Test.gdb"
fcs = arcpy.ListFeatureClasses("Sub_NVF_*")
output = "C:\RG\Kings County\Test.gdb\MergeAll"

try:
    for fc in fcs:

        fldMap_ID = arcpy.FieldMap()
        fldMap_Name = arcpy.FieldMap()
        fieldMappings = arcpy.FieldMappings()

        Fld_ID = "Field_ID"
        F_Name = "File_Name"

        fldMap_ID.addInputField(fc, Fld_ID)
        fldMap_Name.addInputField(fc, F_Name)

        FieldID_type = fldMap_ID.outputField
        FieldID_type.name = 'Field_ID'
        FieldID_type.aliasName = 'Field_ID'
        fldMap_ID.outputField = FieldID_type
        fieldMappings.addFieldMap(fldMap_ID)


        FName_type = fldMap_Name.outputField
        FName_type.name = 'File_Name'
        FName_type.aliasName = 'File_Name'
        fldMap_Name.outputField = FName_type
        fieldMappings.addFieldMap(fldMap_Name)


    arcpy.Merge_management(fc, output, fieldMappings)


except Exception as e:
    print e
  • 1
    Are you able to cut down your code to a much simpler test that demonstrates your hypothesis that Merge_management may not be able to merge multiple files? – PolyGeo Sep 24 '13 at 21:51
  • 2
    Merge_management should be outside the For loop, not within it. Just remove 4 spaces in front of Merge_management. – klewis Sep 24 '13 at 22:16
  • 1
    A nitpicky question, but as I asked on a previous question, why are you calling the Spatial Analyst extension, when you are not using any raster functions here? Don't make your code more complicated than necessary. – Get Spatial Sep 24 '13 at 23:10
  • When I applied merge_management to only two feature classes, it did work. However, when I try to merge more than two feature classes (Sub_NVF_*) using for loop, I need to convert field name into variables, hence made Mean = "M"+fname. I guess, it may be the problem. Any idea? – Kuma Sep 24 '13 at 23:11
  • 1
    You need to move the Merge tool out of the for loop as klewis says. If you look at the Help for this tool it takes as input a LIST of featureclasses. Currently you are supplying one at a time within a for loop, you need to build a list then use that as the input parameter to the merge tool. – Hornbydd Sep 25 '13 at 1:03
1

The parameters are incorrect for the merge tool. In your example, you are adding only a single featureclass fcinto the merge tool:

arcpy.Merge_management(fc, output, fieldMappings)

Instead, merge requires a list of featureclasses in the inputs parameter, for example:

arcpy.Merge_management([featureclass1, featureclass2, featureclass3], output, fieldMappings)

Essentially, you need to take arcpy.Merge_management() out of the for loop and instead feed it a list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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