Python Script to Snap by Attribute Not Working

1373
10
07-20-2018 04:33 AM
JimW
by
Occasional Contributor II

I have two point feature layers.  One represents cleaning and the other catch basins.  The cleaning layer exists as a hosted feature service in ArcGIS Online.  In ArcGIS Pro, I am trying to run the below code to snap cleaning points to catch basin points so they are coincident.  It only runs through the main loop one time.  It used to run fine but now I can't figure out why.  I am not a programmer so I am hoping someone that is can help me figure out why my code won't loop more than once.  Any ideas?  Thanks!

arcpy.env.overwriteOutput = True
arcpy.env.workspace = "E:\MDC\Catch Basin Cleaning\map_docs\Catch Basin Cleaning 2018\Inspection 2018 Review.gdb"

cleaning_fc = 'Cleaning and Sediment Monitoring'
catchBasin_fc = 'ssCatchbasin'
objId_field = 'OBJECTID'
name_field = 'SAPLINKID_FK'
name_field2 = 'SAPLINKID'

#Create a where clause based on the cleaning date
aCleanDateWhereClause = "CleanDate >= timestamp '2018-06-26 01:10:00'"
aTownWhereClause = "TOWNCODE = '01'"

#Create layer files
cleaning_layer = arcpy.MakeFeatureLayer_management(cleaning_fc, "cleaning_lyr", aCleanDateWhereClause)
catchbasin_layer = arcpy.MakeFeatureLayer_management(catchBasin_fc, "catchbasin_lyr", aTownWhereClause)

#Clear any selections
arcpy.management.SelectLayerByAttribute(cleaning_layer , "CLEAR_SELECTION")
arcpy.management.SelectLayerByAttribute(catchbasin_layer , "CLEAR_SELECTION")

#Create a search cursor using an SQL expression to count
#total number of cleaning records to be snapped
totalRec = 0
with arcpy.da.SearchCursor(cleaning_layer, [objId_field, name_field]) as cursor:
     for row in cursor:
          totalRec += 1
     print ("Total Cleaning Recs:  " + str(totalRec)) #I get 734 records here

#Create a search cursor using an SQL expression to loop through only
#those points that have been created since a specific date.  Snap those
#cleaning points to the ssCatchbasin so they are coincident
curRec = 0 #counter for keep tracking of where we are in the loop

#the same loop below only runs one time
with arcpy.da.SearchCursor(cleaning_layer, [objId_field, name_field]) as cursor:
     for row in cursor:
          curRec += 1
          sapLinkId = row[1]
          if "GIS" in sapLinkId:
               layer1 = arcpy.management.SelectLayerByAttribute(cleaning_layer , "NEW_SELECTION", "SAPLINKID_FK=" +"'"+row[1]+"'")
               layer2 = arcpy.management.SelectLayerByAttribute(catchbasin_layer , "NEW_SELECTION", "SAPLINKID=" +"'"+row[1]+"'")
               arcpy.Snap_edit(layer1, [[layer2, "VERTEX", "120000 feet"]])

               percentDone = round((curRec/totalRec)*100,0)
               print("Processing row " + str(row[0]) + " for SAPLINKID " + row[1] + " ("+ str(percentDone) + "% percent complete)")

print ("Done!")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

					
				
			
			
				
			
			
				
			
			
			
			
			
			
		
Tags (1)
0 Kudos
10 Replies
DanPatterson_Retired
MVP Emeritus

Your code is incorrectly indented.

This could be the problem or it is the result of improper formatting

/blogs/dan_patterson/2016/08/14/script-formatting 

0 Kudos
DanPatterson_Retired
MVP Emeritus

throw a print statement in between lines 39 and 40 to see what it returns

print("row[0] {}   row[1] {}".format(row[0], row[1]))

0 Kudos
JimW
by
Occasional Contributor II

Hi Dan,

Thanks for your help!

Here is the result:

Total Cleaning Recs: 826
row[0]16000 row[1]GIS6089793
Processing row 16000 for SAPLINKID GIS6089793 (0.0% percent complete)
row[0]16001 row[1]GIS6093070

My final print statement never runs either.  

0 Kudos
DanPatterson_Retired
MVP Emeritus

Accessing data using cursors—ArcPy Get Started | ArcGIS Desktop 

I didn't think you needed to do a reset() on the cursor with a 'with' statement.

Also, dedent lines 45, 46 to see if it is printing anything outside of the if statement.

I can't see why it won't print the last line 

JimW
by
Occasional Contributor II

Dedented lines 45 & 46 with the same results:

Total Cleaning Recs: 983
row[0]16000 row[1]GIS6089793
Processing row 16000 for SAPLINKID GIS6089793 (0.0% percent complete)
row[0]16001 row[1]GIS6093070

I put some print statements inside the for loop and the script appears to hang on the first select layer by attribute statement (line 41) when the loop begins its second iteration.  But I can't see anything that is wrong that would cause the script to not run beyond this point without throwing an error message of some kind.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Well nothing I have suggested has helped so far, so I will defer to others at this point

JimW
by
Occasional Contributor II

Thanks for your help!

0 Kudos
NeilAyres
MVP Alum

I am pretty sure that running a search cursor on the layer, then within the read loop apply a selection is going to work as a strategy.

0 Kudos
Azharuddin
New Contributor

Even if a statement or expression is syntactically correct, it may cause an error when an attempt is made to execute it. Errors detected during execution are called exceptions and are not unconditionally fatal. Exceptions help in handling errors in a convenient way so that your program does not crash. Exception handling should be used when you feel that you have a code that is capable of producing an error. It is possible to write programs that handle selected exceptions.

Regards

Azharuddin

Python Developer

0 Kudos