Arcpy - Compare values from 2 feature classes

2262
2
07-24-2018 02:49 AM
timdunlevie1
New Contributor II

Hi all,

I'm trying to compare data values in 2 separate feature classes and find out what has changed.

FCnew | FIELDA (shapefile or feature class)

FCmaster | FIELDA (feature class in a fgdb)

FCnew is periodically being updated by external source - this can have new values or have original values removed.

FCmaster is the master table I manage. This consists of the original FCnew values, and will change periodically from the FCnew.

What I currently do in arcpy is join a master set of objects to FCnew attributes and then truncate FCmaster and append current records.

Now I want to show what has changed as well....ideally saved to a text file.

So all I want to do is compare the 2 fields (FIELDA) and print out what values are different.

I've had a look at table compare etc... but find it hard to just tell me what values have changed from one field.

I was thinking of building dictionaries of each set in arcpy and somehow comparing those lists?

Any help would be greatly appreciated.

Just put me onto the right course....!

Using ArcGIS Desktop 10.6.1

Thanks

0 Kudos
2 Replies
XanderBakker
Esri Esteemed Contributor

Probably the most simple way of doing this is using sets. Sets are comparable with lists,  but only contains unique values. To create a set from a field in a featureclass you can use something simple as:

import arcpy

fc = r'your path to the master or new featureclass'
fld_name = 'Your Field Name'

fld_set = set([r[0] for r in arcpy.da.SearchCursor(fc, (fld_name))])

If you do this for both featureclasses you end up with two sets of values. Once you have the sets, comparing is easy. For instance, look at the snippet below:

import arcpy

fc_master = r'your path to the master featureclass'
fld_name_master = 'Your Field Name'
fc_new = r'your path to the new featureclass'
fld_name_new = 'Your Field Name'

set_master = set([r[0] for r in arcpy.da.SearchCursor(fc_master, (fld_name_master))])
set_new = set([r[0] for r in arcpy.da.SearchCursor(fc_new, (fld_name_new))])

lst_in_new_not_in_master = list(set_new - set_master)
lst_in_master_not_in_new = list(set_master - set_new)
lst_in_both = list(set_master & set_new)

This will create 3 lists with:

  • those values that are in the new featureclass yet not in the master
  • those values that are in the master but not in the new featureclass
  • values that are both in the master and the new featureclass

I'm sure that once you have these lists you want to do something with it or perhaps you want to know which ObjectIDs correspond to these values. This can also be done. 

More info on list comparisons: https://community.esri.com/docs/DOC-1927 

timdunlevie1
New Contributor II

Awesome! Thanks Xander...much appreciated.

I’m sure I can work things out from here...

Thanks