fcTarget = 'TonyTwoWay.DBO.TT' #Points fcJoin = 'testParcelsAdmint' #Parcels fcOutput = 'Points_joined' arcpy.SpatialJoin_analysis(fcTarget, fcJoin, fcOutput, 'JOIN_ONE_TO_ONE', 'KEEP_COMMON') curR = arcpy.SearchCursor(fcOutput, '', '', '', 'AddressID A') curW = arcpy.UpdateCursor(fcTarget, '', '', '', 'AddressID A') # init rowW and rowR rowW = curW.next() rowR = curR.next() while rowR: currentAddress = rowR.AddressID print 'current add: ' + currentAddress while rowW.AddressID != currentAddress: rowW = curW.next() if rowW.ACCOUNT == rowR.ACCOUNT_1: rowW.OwnerName = rowR.OwnerName_1 rowW.SiteAddres = rowR.SiteAddres_1 # etc., etc., fill in the rest curW.updateRow(rowW) rowR = curR.next() # changed the delete statement, targeting the cursor objs (rather than the row objs) del rowW, rowR
I am trying to populate some fields from the spatial join features but i am not getting it to populate.
The idea is to create a point feature and have that point feature populated with x,y and AddressID
And info from the Parcels. This would be used as tool so i don't have to manually enter the information especially when i am creating a lot of points.
The fcJoin do not have an AddressID, so i have tried to use ACCOUNT to sort because both fcJoin and fcTarget have the ACCOUNT field but nothing gets populated.
Any help would be greatly appreciated.
The code currently works but does not populate the ACCOUNT,OwnerName, SiteAddres from the parcels.
current codefcTarget = 'TonyTwoWay.DBO.TT' #Points fcJoin = 'testParcelsAdmint' #Parcels fcOutput = 'Points_joined' arcpy.SpatialJoin_analysis(fcTarget, fcJoin, fcOutput, 'JOIN_ONE_TO_ONE', 'KEEP_COMMON') curR = arcpy.SearchCursor(fcOutput, '', '', '', 'AddressID A') curW = arcpy.UpdateCursor(fcTarget, '', '', '', 'AddressID A') # init rowW and rowR rowW = curW.next() rowR = curR.next() while rowR: currentAddress = rowR.AddressID print 'current add: ' + currentAddress while rowW.AddressID != currentAddress: rowW = curW.next() if rowW.ACCOUNT == rowR.ACCOUNT_1: rowW.OwnerName = rowR.OwnerName_1 rowW.SiteAddres = rowR.SiteAddres_1 # etc., etc., fill in the rest curW.updateRow(rowW) rowR = curR.next() # changed the delete statement, targeting the cursor objs (rather than the row objs) del rowW, rowR
import arcpy arcpy.env.overwriteOutupt = True def joinAtts(fcTarget, fcJoin, add_fields): # fix args if type(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(fcJoin, fcTarget,fcOutput, 'JOIN_ONE_TO_MANY') # grab oid field from points oid_t = arcpy.Describe(fcTarget).OIDFieldName # init rowW and rowR curR = arcpy.SearchCursor(fcOutput) 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.AddMessage('Updated all records sucussefully') return if __name__ == '__main__': # grab args argv = tuple(str(arcpy.GetParameterAsText(i)) for i in range(arcpy.GetArgumentCount())) # Run it joinAtts(*argv)
Caleb1987 that's pretty neat.
I would prefer this to be in a tool, because the two targets would not change nor would the fields that would be copied.
I would modify it my self but i am not to familiar on what's going on in the code. I would also like to combine it with the creating feature points from my first post.
Could you describe what is going here?
dict([(r.JOIN_FID,[r.getValue(f) for f in add_fields]) for r in curR])
How are you getting the input parameters into the script.
I see argv = tuple(str(arcpy.GetParameterAsText(i)) for i in range(arcpy.GetArgumentCount()))
but i am not sure how it's going into the code.
dict([(r.JOIN_FID,[r.getValue(f) for f in add_fields]) for r in curR])
argv = tuple(str(arcpy.GetParameterAsText(i)) for i in range(arcpy.GetArgumentCount()))