Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am working on a data integration project and am trying to single out feature classes which have a schema that varies from two base feature classes (one for point, one for polyline features).

I've played around with the Feature Compare tool to compare schemas, but find it tedious to use this tool, even in batch mode, because it outputs a single text file with a few values for each comparison performed.

I started writing a script in Python to loop through a list of features in a list and compare their schema based on geometry type. However, the problem of either integrating the output into a single text file persists. Is there a way to obtain this value and append it to an existing text file?

(Messy) code so far as follows:

# Import arcpy module
import arcpy

# Import environments; set workspace
from arcpy import env

# Set workspace
env.workspace = "Data_Comparison.gdb"

# List features to be compared
fclist = arcpy.ListFeatureClasses()
print fclist

# Loop through list to separate point and line feature classes
for fc in fclist:
    desc = arcpy.Describe(fc)
    name = desc.file
    geo = desc.shapeType

    print ("Feature name: " + name)
    print ("Feature geometry: " + geo)

    if geo == "Point":
        arcpy.FeatureCompare_management("swr_net_nodal", fc, "ENTID", "SCHEMA_ONLY", "", "", "", "", "", "", "CONTINUE_COMPARE", "fc + '_compare'")
        output = arcpy.GetMessages()
        print output
share|improve this question

1 Answer 1

up vote 1 down vote accepted

One possible solution is to create a text file ahead of time and to read the compare_file line by line and append it to the text file.

    # import arcpy module
    import arcpy

    # import os module
    import os

    # set workspace
    workspace = r"some\path"
    arcpy.env.workspace = workspace

    # prepare compare file
    out_compare_file = r"some\path\to\file.txt"

    # list of features to compare
    fclist = arcpy.ListFeatureClasses()

    # loop through all feature class in workspace
    for fc in fclist:
        fc_desc = arcpy.Describe(fc)
        # getting name without extention
        fc_name = os.path.splitext(fc_desc.file)[0]
        fc_shape_type = fc_desc.shapeType

        if fc_shape_type == 'Point':
            # Set variables
            base_features = r"some\path\to\base_feature"
            test_features = fc
            sort_field = 'some_field'
            compare_type = "SCHEMA_ONLY"
            ignore_option = "#"
            xy_tolerance = "#"
            m_tolerance = "#"
            z_tolerance = "#"
            attribute_tolerance = "#"
            omit_field = "#"
            continue_compare = "CONTINUE_COMPARE"
            compare_file = workspace + "\\" + fc_name + "_compare.txt"
            try:
                compare_result = arcpy.FeatureCompare_management(
                    base_features,
                    test_features,
                    sort_field,
                    compare_type,
                    ignore_option,
                    xy_tolerance,
                    m_tolerance,
                    z_tolerance,
                    attribute_tolerance,
                    omit_field,
                    continue_compare,
                    compare_file
                )
                print compare_result.getOutpuput(1)

                # open prepared text file to append
                with open(out_compare_file, 'a') as outfile:
                    # open compare file to read
                    with open(compare_file) as infile:
                        for line in infile:
                            outfile.write(infile)
                        # do not forget to close file handlers
                        infile.close()
                    outfile.close()                           
            except:
                print arcpy.GetMessages()
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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