Trouble Merging Tables that are Outputs from Tool

1770
5
12-22-2016 12:46 PM
PatrickMcKinney1
Occasional Contributor III

I'm building an ArcGIS toolbox tool from a python script.  I'll provide a summary of the tool, the problem I'm having, and the code sample:

Tool Outline:

1.  Pre-defined feature classes in an SDE database are merged together.  The output of the merge is user-defined.

2. The output from step 1 (merged feature classes) is converted to a file geodatabase table.

3. 2 pre-defined feature classes are converted to a table.  The output geodatabase is user-defined.

4. The 3 tables are merged together, with the output being user-defined.

I am getting an error when the tool tries to merge the three tables together.  In the toolbox, the parameter is data type = Table and direction = output.  My initial thoughts from the error message is that the tool is not recognizing the input tables to the merge, which are the outputs of previous steps.  Any and all help is greatly appreciated.

Error message:

Failed at step
Line 94
Failed to execute. Parameters are not valid.
ERROR 000732: Input Datasets: Dataset 'F:\Departments\Public Safety\NARM\2016 Join\NARM_2016_Join';'F:\Departments\Public Safety\NARM\2016 Join\NARM_2016_Join';'F:\Departments\Public Safety\NARM\2016 Join\NARM_2016_Join' does not exist or is not supported
Failed to execute (Merge).

Code:

# Import system modules
import arcpy
import sys, traceback, string

# Run geoprocessing tools within Try statement block
# 1. Merge point layers together
# 2. Convert merged point layers to a table
# 3. Convert Parks layer to table
# 4. Convert Pipeline layer to table
# 5. Merge tables together
try:
    # Set workspace to SDE user for CCGIS SDE database
    arcpy.env.workspace = r"Database Connections\SDE@CCGIS on NCGSSR04.sde"

    # Variables for Output
    # Output for merged point layers
    pointLayersMerged = arcpy.GetParameterAsText(0)

    # Output Location for Tables
    outputLocation =  arcpy.GetParameterAsText(1)

    # Output for table export of merged point layers
    #pointLayersTable = arcpy.GetParameterAsText(1)

    # Name for Green_Parks table export
    #parksTable = arcpy.GetParameterAsText(2)

    # Name for EOC_Pipeline table export
    #pipelineTable = arcpy.GetParameterAsText(3)

    # Name for merged tables
    tablesMergeOutput = arcpy.GetParameterAsText(2)



    # Point layers to merge
    layersToMerge = ['CCGIS.SDE.Site_Education', 'CCGIS.SDE.EOC_Daycare', 'CCGIS.SDE.Site_HealthMedical', 'CCGIS.SDE.EOC_MHIDD_Facility', 'CCGIS.SDE.Site_GovernmentMilitary', 'CCGIS.SDE.Site_EmergencyResponseLawEnforcement', 'CCGIS.SDE.EOC_AssistedLiving', 'CCGIS.SDE.Site_PublicAttractionLandmarks', 'CCGIS.SDE.EOC_CommunityCenters', 'CCGIS.SDE.EOC_PublicShelters', 'CCGIS.SDE.EOC_SARA', 'CCGIS.SDE.EOC_WasteWaterTreatment']

    # 1. Merge point layers
    result1 = arcpy.Merge_management(layersToMerge,pointLayersMerged)

    # Write result message
    while result1.status < 4:
        time.sleep(0.2)
    resultValue1 = result1.getMessages()
    arcpy.AddMessage(str(resultValue1))
    arcpy.AddMessage("Completed Merging point layers")

    # 2. Convert merged point layers to a table
    result2 = arcpy.TableToDBASE_conversion(result1, outputLocation)

    # Write result message
    while result2.status < 4:
        time.sleep(0.2)
    resultValue2 = result2.getMessages()
    arcpy.AddMessage(str(resultValue2))
    arcpy.AddMessage("Completed converting Merged Point Layers feature class to a database table")

    # 3. Convert Parks layer to table
    result3 = arcpy.TableToDBASE_conversion('CCGIS.SDE.Green_Parks', outputLocation)

    # Write result message
    while result3.status < 4:
        time.sleep(0.2)
    resultValue3 = result3.getMessages()
    arcpy.AddMessage(str(resultValue3))
    arcpy.AddMessage("Completed converting Parks feature class to a database table")

    # 4. Convert Pipeline layer to table
    result4 = arcpy.TableToDBASE_conversion('CCGIS.SDE.EOC_PIPELINE', outputLocation)

    # Write result message
    while result4.status < 4:
        time.sleep(0.2)
    resultValue4 = result4.getMessages()
    arcpy.AddMessage(str(resultValue4))
    arcpy.AddMessage("Completed converting Pipeline feature class to a database table")

    # 5. Merge tables together
    # Input tables
    tablesMergeInput = [result2, result3, result4]

    # Run Merge tool
    result5 = arcpy.Merge_management(tablesMergeInput,tablesMergeOutput)

    # print result messages
    while result5.status < 4:
        time.sleep(0.2)
    resultValue5 = result5.getMessages()
    arcpy.AddMessage(str(resultValue5))
    arcpy.AddMessage("Completed merging tables together")

except Exception, e:
    # If an error occurred, write line number and error message to log
    tb = sys.exc_info()[2]
    arcpy.AddError("Failed at step \n" "Line %i" % tb.tb_lineno)
    arcpy.AddError(e.message)
0 Kudos
5 Replies
ClintonDow1
Occasional Contributor II

Currently, the Table to dBASE tool's result returns the same output folder path, rather than the name of the created table.

r = arcpy.TableToDBASE_conversion('roads',"C:\\Projects\\MyProject")
r
# <Result 'C:\\Projects\\MyProject'>

This seems erroneous, I'll check with the tool author to see if it should be the path to the dBASE table which is returned. This is why in your error message output, the folder path is listed three times as the input datasets for your merge. 

In the meantime, you'll have to manually concatenate the outputFolder path, the original table name (ie: Green_Parks) and the expected extension (.dbf) for each table that you're converting, then use those strings in your tablesMergeInput list. Once its the three paths to your datasets that you're passing, the merge tool should work as expected and your script should complete successfully. 

PatrickMcKinney1
Occasional Contributor III

Thanks for the help!

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I suggest using the Table to Table tool, it supports exporting to DBF as well as many other formats.  Additionally, the Table to Table tool allows you to change the name of the outputted DBF table.

ClintonDow1
Occasional Contributor II

Just for a follow up, since the TableToDBASE tool can take one or many input tables the result returns the folder path where all of the converted tables would end up - this is the correct behavior so I was mistaken, however the suggested fix should still work for your script. In the TableToTable tool which Joshua mentioned, you can only use a single input at a time and therefore the result will be the path to the single converted table and may in fact be the simpler fix to implement in your case. 

PatrickMcKinney1
Occasional Contributor III

CDow-esristaff‌, your suggestion for the Table to dBASE worked.  However, I'm running into issues with field names and length of fields.

I think I'll try Table to Table, as I can use Field Mapping to control that aspect of the merging.

Thanks everyone for your suggestions.

0 Kudos