update field records

492
2
01-31-2012 10:38 AM
RameshGautam
New Contributor
I have two shape files. They are: (i) WashingtonA and (ii) WashingtonB. WashingtonB is a subset of WashingtonA. During processing, additional records were added in WashingtonB. These records were generated by dividing the existing polygons of WashingtonB. Now, I want to update WashingtonA based on updated polygons of WashingtonB. Can you please help me how to do this using Python code? Thanks
Tags (2)
0 Kudos
2 Replies
JakeSkinner
Esri Esteemed Contributor
Below is an example on how I was able to accomplish this.  The OBJECTIDs for the two feature classes were the same, so I first appended all the new features created for a polygon split.  Then I compare the original geometry to each geometry in the subset feature class (excluding new features).  If the geometry is different, I delete the original feature and update with the new feature (the other half of the geometry that was created from the polygon split).  Since I used the OBJECTID I can only run this script once as the OBJECTIDs in the original feature class will change after the appends.

import arcpy
from arcpy import env
env.workspace = r"C:\temp\python\test.gdb"
env.overwriteOutput = True

fcA = "AirportsA"
fcB = "AirportsB"

OID_list = []
listB = []

# find max OBJECTID
rows = arcpy.SearchCursor(fcA)
for row in rows:
    OID_list.append(row.OBJECTID)

del row, rows

maxOID = sorted(OID_list)[-1]

# append all geometries for AirportsB to list
rows = arcpy.SearchCursor(fcB)
for row in rows:
    geom = row.Shape
    listB.append(geom.area)

del row, rows

# append all new features to AirportsA feature class
arcpy.MakeFeatureLayer_management(fcB, "AirportsB_lyr", "OBJECTID > " + str(maxOID))
arcpy.CopyFeatures_management("AirportsB_lyr", "AirportsB_2")
arcpy.Append_management("AirportsB_2", fcA, "NO_TEST")

x = 0

# compare geometries from AirportsA to AirportsB
rows = arcpy.SearchCursor(fcA, "OBJECTID <= " + str(maxOID))
for row in rows:
    geom = row.Shape
    if geom.area != listB:
        print "OBJECTID " + str(row.OBJECTID) + " has changed"
        arcpy.MakeFeatureLayer_management(fcA, "AirportsA_lyr")
        arcpy.SelectLayerByAttribute_management("AirportsA_lyr", "NEW_SELECTION", "OBJECTID = " + str(row.OBJECTID))
        # delete original features that changed
        arcpy.DeleteFeatures_management("AirportsA_lyr")
        arcpy.MakeFeatureLayer_management(fcB, "AirportsB_lyr", "OBJECTID = " + str(row.OBJECTID))
        # append new split feature
        arcpy.Append_management("AirportsB_lyr", fcA, "NO_TEST")
        x += 1

del row, rows
0 Kudos
RameshGautam
New Contributor
Thanks a lot for your help. I apprecaite it. Let me try and see how it goes.















Below is an example on how I was able to accomplish this.  The OBJECTIDs for the two feature classes were the same, so I first appended all the new features created for a polygon split.  Then I compare the original geometry to each geometry in the subset feature class (excluding new features).  If the geometry is different, I delete the original feature and update with the new feature (the other half of the geometry that was created from the polygon split).  Since I used the OBJECTID I can only run this script once as the OBJECTIDs in the original feature class will change after the appends.

import arcpy
from arcpy import env
env.workspace = r"C:\temp\python\test.gdb"
env.overwriteOutput = True

fcA = "AirportsA"
fcB = "AirportsB"

OID_list = []
listB = []

# find max OBJECTID
rows = arcpy.SearchCursor(fcA)
for row in rows:
    OID_list.append(row.OBJECTID)

del row, rows

maxOID = sorted(OID_list)[-1]

# append all geometries for AirportsB to list
rows = arcpy.SearchCursor(fcB)
for row in rows:
    geom = row.Shape
    listB.append(geom.area)

del row, rows

# append all new features to AirportsA feature class
arcpy.MakeFeatureLayer_management(fcB, "AirportsB_lyr", "OBJECTID > " + str(maxOID))
arcpy.CopyFeatures_management("AirportsB_lyr", "AirportsB_2")
arcpy.Append_management("AirportsB_2", fcA, "NO_TEST")

x = 0

# compare geometries from AirportsA to AirportsB
rows = arcpy.SearchCursor(fcA, "OBJECTID <= " + str(maxOID))
for row in rows:
    geom = row.Shape
    if geom.area != listB:
        print "OBJECTID " + str(row.OBJECTID) + " has changed"
        arcpy.MakeFeatureLayer_management(fcA, "AirportsA_lyr")
        arcpy.SelectLayerByAttribute_management("AirportsA_lyr", "NEW_SELECTION", "OBJECTID = " + str(row.OBJECTID))
        # delete original features that changed
        arcpy.DeleteFeatures_management("AirportsA_lyr")
        arcpy.MakeFeatureLayer_management(fcB, "AirportsB_lyr", "OBJECTID = " + str(row.OBJECTID))
        # append new split feature
        arcpy.Append_management("AirportsB_lyr", fcA, "NO_TEST")
        x += 1

del row, rows
0 Kudos