4

I would like to append the data from one feature class to another after confirming that the schemas match in both feature classes.

I can append the data from one FC to another by using this simple code:

    arcpy.DeleteFeatures_management(outFeatureClass)
    arcpy.Append_management("FC", outFeatureClass, "TEST", "")

...but what I would like to do is stop execution of this code completely if the schemas have changed...in my example above, the data has already been deleted by the time that the schema change parameter ("TEST) fails. I am using 'Append...' b/c these feature classes are connected to map services.

Ideally, I would like to first compare the schemas, and then continue to delete/append if they match. If they do not, I would like to send an email, and end the script.

I have tried using the FeatureCompare tool, which will print a message if the schemas do not match, but I haven't figured out if or how I can use the data in this message as a variable in my code:

 compare_result = arcpy.FeatureCompare_management(inFC, outFC, "OBJECTID", "SCHEMA_ONLY", "","","","","","","CONTINUE_COMPARE")
 print arcpy.GetMessages()

When I run this code, one line of the output message reads:

Table: Tables have different number of fields (Base: 48, Test: 47).

...if there has been a change, or

Field: Field properties are the same.

...if the schemas match.

Is there a way I can somehow use this information to set a flag or boolean in my code?

I also considered using a SearchCursor, but I am not so sure that is the right idea.

I am currently able to send an email confirming that the script was successful or if there was an error, but I would prefer to send an email before an error has already resulted in a faulty map service.

1 Answer 1

2

The FeatureCompare tool stops at the first mismatch encountered, by default. You can change the 11th parameter to CONTINUE_COMPARE. This will report all mismatch messages. Also, the Result object from each geoprocessing tool has many properties that you can explore. The maxSeverity property reports one of 3 conditions, 0 - informal message, 1 - warning message, 2 - error message. You should be able to key on the maxSeverity or other result messages to exit the script.

EDIT: The last parameter of Feature Compare defines the output file, which is a text file.
Both similiarities and differences are reported to the output file.
You can use this function to query the feature compare output file for errors.
Here is sample logic for sending an email when the schema doesn't match,
If hasSchemaError(outputfile):
# create email
# send email
# system.exit()

import csv
import os

def hasSchemaError(textfile):
    ret = False
    if os.path.isfile(mycsv):
        source_file = open(textfile,'rb')
        reader = csv.DictReader(source_file, delimiter=',')

        for line in reader:
            if line['Has_error'] == 'true':
                ret = True
        source_file.close()
    return ret
2
  • thanks..you'll see from my code snippet above that I had already added the 'CONTINUE_COMPARE' parameter. I understand the maxSeverity parameter; what I don't understand is how to use any message output as variables in my code, or if that is even possible. I think you allude to this when you say 'key on the ... result messages to exit the script?' This is what I do not understand how to do; if you can tell me I would appreciate it! In my case, as I try to explain above, I specifically would like to add text to an email and exit the script if the tables do not match.
    – JasonBK
    Dec 30, 2013 at 14:08
  • I see you edited your answer, and I checked it as accepted. I had to change the 'mycsv' variable to match the input parameter (textfile), and I added another conditional for my specific need:if line[' Message'] == ' "Tables have different number of fields"': ... and now it works...thanks!
    – JasonBK
    Dec 31, 2013 at 18:24

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.