Select to view content in your preferred language

How to Write Value from nested tables to Parent Table

573
2
06-03-2020 11:59 AM
by Anonymous User
Not applicable

I'm having issues with writing some values from 2 tables deep (nested). I'm trying to get the X,Y values of a line that is nested twice from the parent. If that makes sense. 

So I have a point. That point looks in another dataset then once it finds that it looks into another dataset for the values. I'm trying to get those values from the 3 table be written in the first table. 

Here is my code. I almost got it, but I get an undefined variable error   on line 58. 

Thanks 

global lat 
global long 

#Search each record in Queue table 
with arcpy.da.UpdateCursor (tbl, inputtable_fields) as Scur2:
	for row1 in Scur2:
		Sub1 = row1[0]
		Sub2 = row1[1]
		FIPS_var = row1[2]
		state_var = row1[3]
		queid = row1[4]
		arcpy.AddMessage(queid + ",")



		with arcpy.da.SearchCursor(tline_lyr, tline_lyr_fields) as Scur:
			#arcpy.AddMessage(Sub1 + "," + Sub2+ ",")
			for row in Scur:
				if (row[0] == Sub1 and row[1] == Sub2) or (row[0] == Sub2 and row[1] == Sub1):
					Rec_ID_var = str(row[3])
					#arcpy.AddMessage ("the power line is "+ Rec_ID_var) 
					
					#Select Power line
					selectline_var = arcpy.SelectLayerByAttribute_management(tline_lyr, 'NEW_SELECTION', "Rec_ID = " + Rec_ID_var)

					#Create in memory feature to get centroid of slected line. 
					arcpy.CopyFeatures_management(selectline_var, r'in_memory\SelectedLine')

					#Select County by Attribute  
					#arcpy.SelectLayerByLocation_management(county_lyr, 'INTERSECT', r'in_memory\SelectedLine', '#', 'NEW_SELECTION', 'NOT_INVERT')

					#Select County to use as a clip 
					arcpy.SelectLayerByAttribute_management(county_lyr, 'NEW_SELECTION', "FIPS = '"+FIPS_var+"'" )

					#Clip line with the County
					arcpy.Clip_analysis(r'in_memory\SelectedLine', county_lyr, r'in_memory\SelectedLineClipped', '#')

					#Create the mid point
					arcpy.FeatureToPoint_management(r'in_memory\SelectedLineClipped', r'in_memory\LineMidPT', 'INSIDE')

					#Calculates Lat + Long
					arcpy.AddGeometryAttributes_management(r'in_memory\LineMidPT', 'POINT_X_Y_Z_M', '#', '#', WGS_1984_CS)
					
					#Print out lat long 

					with arcpy.da.SearchCursor(r'in_memory\LineMidPT', ["POINT_X","POINT_Y"]) as Scur:

						for row in Scur:
							arcpy.AddMessage(str(row[0]))
							arcpy.AddMessage(str(row[1]))
							long = row[0]
							lat = row[1]
							arcpy.management.SelectLayerByAttribute(tline_lyr, 'CLEAR_SELECTION')
							arcpy.management.SelectLayerByAttribute(county_lyr, 'CLEAR_SELECTION')
		
							

			row1[5] = long
			row1[6] = lat
			cursor.updateRow(row)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I've also tried this with a try statement on line 58. But then it doesn't write anything. 

Thanks! 

0 Kudos
2 Replies
JoshuaBixby
MVP Esteemed Contributor

Try changing lines 58 and 59 to "row1" instead of "row".  Even if that resolves your error on line 58, you will get one on line 60 because there is not cursor named "cursor".

There are other issues I see with your code, but I have to jump on a call shortly.  I will try to follow-up later.

0 Kudos
by Anonymous User
Not applicable

Thanks for the reply. Yeah my bad that was suppose to be row1 I had that before. But yeah still same error says it's undefined. 

Thanks again. No rush. 

0 Kudos