I am trying to automate the production of a feature class with daily updates. I would like to use a Python Script do this. The basic concept is there is a SQL table that is updated with addresses, the script needs to pull that table into a intermediate file geodatabase, geocoded it, compare the geocoded results to the existing SDE feature class, and remove any old entries from the SDE feature class, append any new features, and leave alone any that are the same. I get the first chunk of my script to do what I want it to do. When I get to the compare and update part, my initial line of thinking will not work in all cases (especially when there are only removals). I know there has to be a more elegant way of doing the compare and update portion. Any suggestions?
This is what the portion that does not work all the time:
# Put in error trapping in case an error occurs when running tool
CoMGIS_SDE_Warrants = #Path to SDE Feature Class
try:
# Make a layer from the feature class
arcpy.MakeFeatureLayer_management(MAW_GeoCode,"newCompare_lyr")
# Selecting all existing features
arcpy.SelectLayerByLocation_management("newCompare_lyr", "INTERSECT", CoMGIS_SDE_Warrants, "", "NEW_SELECTION")
# Selecting only new features
arcpy.SelectLayerByLocation_management("newCompare_lyr", "INTERSECT", CoMGIS_SDE_Warrants, "", "SWITCH_SELECTION")
# Write the selected features to a new featureclass
arcpy.Append_management("newCompare_lyr", CoMGIS_SDE_Warrants,"TEST","","")
arcpy.Delete_management("newCompare_lyr")
# Make a layer from the Existing Features
arcpy.MakeFeatureLayer_management(CoMGIS_SDE_Warrants,"existCompare_lyr")
# Selecting all existing features
arcpy.SelectLayerByLocation_management("existCompare_lyr", "INTERSECT", MAW_GeoCode, "", "NEW_SELECTION")
# Selecting only Non-Active Warrants
arcpy.SelectLayerByLocation_management("existCompare_lyr", "INTERSECT", MAW_GeoCode, "", "SWITCH_SELECTION")
# Delete Non-Active Warrants
arcpy.DeleteFeatures_management("existCompare_lyr")
arcpy.Delete_management("existCompare_lyr")
except:
print arcpy.GetMessages()
print "Success"
Solved! Go to Solution.
Xander,
Thank you very much. I ran it, tested, and it worked like a charm. I will spend some time to truly understanding what is all going on in the script you posted. Thank you again for helping out a newbie at python scripting.
Remember that Riyas Deen did most of the work.
To help you a little in the process of understanding the code, here goes...
There are a number of functions that can be reused and help to make the code more readable
The rest of the code does the job:
Kind regards, Xander
Hi Xander,
The python script you shared here was very useful, thanks!
The add function insert new records and delete function remove old records, is it possible to update attribute value if the record exist in both tables/features?
Thanks,
Amy
I suppose that is possible, but what would you like to register in the destination featureclass is a feature ID still exists, but attributes have changed? Should the record be marked as changed or do you want to overwrite the field values? Be aware that only the listed fields (those that both featureclasses have) will be checked to see if attributes changed.
Xander,
Thanks for your quick reply!
I have a feature layer, 10 attribute fields were captured in GIS and 20 fields were from City Works and rest were our inputs. In GIS, the feature layer is identical to the 10 fields in the destination features. So I'd like to send GIS updates to the layer automatically (overwrite the 10 field values) and keep updating/editing the rest attributes. Is it possible to do that?
Thanks,
Amy
Aimei,
Did you figure this out?
David,
my solution is exporting the feature layer into File geo database, split it into three layers and update them accordingly. then merge them into one.
Thanks,
Aimei