3

I'm trying to find an example of a python script for ArcGIS 10.1 that will get count of a feature class (FC) in my ArcSDE and get count of a FC in an incoming file geodatabase update and then write the difference to a log file. We get monthly updates from a vendor. For instance, wells FC, and want to be able to compare record counts and log diff (a-b=C). Right now I have a script that will record start time, get count, truncate, append, rebuild index.

8

Here's a one-liner for getting the count of records in a specific FC or table:

count = sum(1 for row in arcpy.da.SearchCursor(r'C:\PATH\TO\FC', ['OID@']))
4

You could try GetCount_management.

import arcpy
from arcpy import env

env.workspace = "C:/Path/to.gdb"
count = int(arcpy.GetCount_management("FC").getOutput(0))
2

I wrote a script that uses the feature compare tool across two databases as a means of comparing them. It will not only display the count for feature classes if they have changed, but it will also return any changes in attribution, geometry, spatial reference, or schema. I'm new at this too, so if anyone has improvements, let me know please.

import arcpy
from os import mkdir
from shutil import rmtree

def compare():
    old = arcpy.GetParameterAsText(0)
    old_path = old + '\\'
    arcpy.env.workspace = old
    old_fds = arcpy.ListDatasets()

    new = arcpy.GetParameterAsText(1)
    arcpy.env.workspace = new
    new_path = new + '\\'
    new_fds = arcpy.ListDatasets()

    for old_fd, new_fd in zip(old_fds, new_fds):
        old_fcs = arcpy.ListFeatureClasses(feature_dataset = old_fd)
        new_fcs = arcpy.ListFeatureClasses(feature_dataset = new_fd)
        base = old_path + old_fd + '\\'
        test = new_path + new_fd + '\\'
        for old_fc, new_fc in zip(old_fcs, new_fcs):
            base_features = base + old_fc
            test_features = test + new_fc
            sort_field = 'OBJECTID'
            compare_type = 'ALL'
            xy_tolerance = '0.001 FEET'
            m_tolerance = 0
            z_tolerance = 0
            attribute_tolerance = ''
            omit_field = ''
            continue_compare = 'CONTINUE_COMPARE'
            compare_file = scratch_dir + '\\' + new_fc + '.txt'

            compare_result = arcpy.FeatureCompare_management(base_features, test_features, sort_field, compare_type, '', xy_tolerance, m_tolerance, z_tolerance, attribute_tolerance, omit_field, continue_compare, compare_file)

            in_rows = scratch_dir + '\\' + new_fc + '.txt'
            out_path = scratch_dir 
            out_name = new_fc + '.dbf'

            dbfs = arcpy.TableToTable_conversion(in_rows, out_path, out_name)
            for dbf in dbfs:
                field = 'FEATURECL'
                newfield = arcpy.AddField_management(dbf, field, 'TEXT', '', '', 100)
                cursor = arcpy.UpdateCursor(newfield)
                for row in cursor:
                    row.setValue(field, new_fc)
                    cursor.updateRow(row)

def listmerge():    
    arcpy.env.workspace = scratch_dir
    listTable = arcpy.ListTables('*.dbf', 'dBASE')
    arcpy.Merge_management(listTable, final_dir + '\\' + "output.dbf")
    rmtree(scratch_dir)     

output_dir = arcpy.GetParameterAsText(2)
scratch_dir = output_dir + '\\' + "scratch"
final_dir = output_dir + '\\' + "output"

mkdir(scratch_dir)
mkdir(final_dir)            
compare()
listmerge() 

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.