I have tried to put the new feature Object id in a variable (I think i am doing it right) but I am uncertain because the script takes a long time like it's selecting every point feature.my script creates the point, populates certain fields, then it's suppose to take that new point and runt a spatial join against the parcels feature class and output the result to the in-memory workspace, then Insert the result from above into your original points feature class.Here is what i have.
import arcpy
import pythonaddins
arcpy.env.overwriteOutput = True
fcTarget = "TT"
workspace = r"Connection to sqlexpress.sde"
#arcpy.ChangeVersion_management('TT','TRANSACTIONAL','dbo.DEFAULT', "")
# Start an edit session. Must provide the worksapce.
edit = arcpy.da.Editor(workspace)
# Edit session is started without an undo/redo stack for versioned data
# (for second argument, use False for unversioned data)
edit.startEditing(True)
# Start an edit operation
edit.startOperation()
input = arcpy.GetParameterAsText(0)
CC_list = []
with arcpy.da.SearchCursor(fcTarget, ["AddressID"]) as cursor:
for row in cursor:
try:
if "CC" in row[0]:
CC_list.append(int(row[0].strip("CC")))
except TypeError:
pass
del cursor
CC_list.sort()
AddressID = CC_list[-1] + 1
AddressID = 'CC' + str(AddressID)
rows = arcpy.SearchCursor(input)
for row in rows:
geom = row.shape
x = geom.lastPoint.X
y = geom.lastPoint.Y
del row, rows
row_values = [(x, y, (x, y), AddressID)]
cursor = arcpy.da.InsertCursor(fcTarget, ["X_Coord", "Y_Coord", "SHAPE@XY", "ADDRESSID"])
for row in row_values:
cursor.insertRow(row)
#del cursor
Parcellyr = "testParcelsAdmit"
#maxValue = arcpy.SearchCursor(fcTarget, "", "", "", 'AddressID D').next().getValue("AddressID") #Get 1st row in descending cursor sort
#arcpy.SelectLayerByAttribute_management(fcTarget, "NEW_SELECTION", "\"AddressID\"= " + maxValue)
Par_lyr = arcpy.MakeFeatureLayer_management(Parcellyr, "Parcel layer")
entries = int(arcpy.GetCount_management(fcTarget).getOutput(0))
for i in xrange(entries):
arcpy.MakeFeatureLayer_management(fcTarget, "point layer", "\"OBJECTID\"={}".format(str(i)))
arcpy.SelectLayerByLocation_management(Par_lyr, "INTERSECT", fcTarget, "", "NEW_SELECTION")
#if arcpy.Exists(pt_lyr): arcpy.Delete_management(pt_lyr)
#### populates fields
add_fields = ["ACCOUNT","SiteNum","OwnerName","SiteAddres","SiteNumSfx","SiteStreet","predir","StreetType","SubName"]
# fix args
if not isinstance(add_fields, list):
# from script tool
add_fields = add_fields.split(';')
# do not need this scratch file
fcOutput = r'in_memory\temp_join'
arcpy.SpatialJoin_analysis(Par_lyr, fcTarget, fcOutput, 'JOIN_ONE_TO_MANY', 'KEEP_COMMON')
# grab oid field from points
oid_t = arcpy.Describe(fcTarget).OIDFieldName
# init rowW and rowR
curR = arcpy.SearchCursor(fcOutput, query)
join_dict = dict([(r.JOIN_FID,[r.getValue(f) for f in add_fields]) for r in curR])
del curR
# Now update the new target
curW = arcpy.UpdateCursor(fcTarget)
for row in curW:
t_oid = row.getValue(oid_t)
if t_oid in join_dict:
for f in add_fields:
row.setValue(f, join_dict[t_oid][add_fields.index(f)])
curW.updateRow(row)
del row, curW
arcpy.Delete_management(r"in_memory\temp_join")
arcpy.AddMessage('Updated all records sucussefully')
arcpy.RefreshActiveView()
# Stop the edit operation.
edit.stopOperation()
# Stop the edit session and save the changes
edit.stopEditing(True)