1

I have some code that seems to behave as expected and occasionally satisfies an if loop I've created but the code never seems to enter the loop. Even where the if statement is satisfied.

The 'Warning' Messages a just to trigger a different font effect in the display box as the tool runs. The if statement on line 21 is the issue, though as I've said it triggers no errors.

BufferLayer = arcpy.da.UpdateCursor(Temp_Buff, ["OBJECTID"])
for Poly in BufferLayer:
    counta = Poly[0]
    arcpy.AddMessage("Assessing Intersection or Outlet {} of {}...".format(counta, total))
    arcpy.AddMessage("Isolating Intersection or Outlet...")
    # Selecting the Current Outlet from the master Layer
    arcpy.Select_analysis(Temp_Buff, Temp_Poly, "OBJECTID = {}".format(counta))
    # Clipping All Vertices
    arcpy.Clip_analysis(All_Points, Temp_Poly, Current_Points)
    # Identify and Sort Stream / River
    Points = arcpy.da.UpdateCursor(Current_Points, ["grid_code"])
    orders = []
    for Point in Points:
        orders.append(Point[0])
    orders.sort()
    countb = 1
    arcpy.AddMessage("Approximating Pour Point(s)...")
    # Append to Master List if a Pour Point, Delete Point if Not
    Points = arcpy.da.UpdateCursor(Current_Points, ["grid_code"])
    for Point in Points:
        arcpy.AddMessage("if {} == {} and {} >= {}".format(Point[0], orders[0], Point[0], Minimum_Stream_Order_To_Delineate))
        if Point[0] == orders[0] and Point[0] >= Minimum_Stream_Order_To_Delineate:
            if arcpy.Exists(Final_PP_Layer + "_O" + Point[0]):
                arcpy.AddWarning("Appending Pour Point to Respective Master Layer...")
                arcpy.Append_management(Point, Final_PP_Layer + "_O" + Point[0], "NO_TEST")
            else:
                arcpy.AddWarning("Creating New {} Order Master Pour Point Layer...")
                arcpy.CreateFeatureclass_management(Workspace, Final_PP_Layer + "_O{}".format(val), "MULTIPOINT", Current_Points, "DISABLED", "DISABLED", "PROJCS['NAD_1983_CSRS_New_Brunswick_Stereographic',GEOGCS['GCS_North_American_1983_CSRS',DATUM['D_North_American_1983_CSRS',SPHEROID['GRS_1980',6378137.0,298.257222101]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Double_Stereographic'],PARAMETER['False_Easting',2500000.0],PARAMETER['False_Northing',7500000.0],PARAMETER['Central_Meridian',-66.5],PARAMETER['Scale_Factor',0.999912],PARAMETER['Latitude_Of_Origin',46.5],UNIT['Meter',1.0]];-28216700 -23260100 10000;-100000 10000;-100000 10000;0.001;0.001;0.001;IsHighPrecision", "", "0", "0", "0")
                arcpy.AddWarning("Appending Pour Point to Respective Master Layer...")
                arcpy.Append_management(Point, Final_PP_Layer + "_O" + Point[0], "NO_TEST")
7
  • Do you use an IDE? Try break at the line with the if statement and see what the condition evaluates to and then step through each line watching what happens. Aug 8 '17 at 21:44
  • 1
    May be the buffer never clips out the points, are they different coordinate systems? Also your code is difficult to read as Points is a cursor and Point is a row. If you want your code to be readable and instantly understandable you should adopt the naming conventions used throughout the user community and used within all the code samples found in the ESRI help file.
    – Hornbydd
    Aug 8 '17 at 21:45
  • Could you explain in more detail what the if statement is supposed to be checking, and examples of thearcpy.AddMessage("if {} == {} and {} >= {}".format(Point[0], orders[0], Point[0], Minimum_Stream_Order_To_Delineate)) output? Aug 8 '17 at 23:35
  • @EmilBrundage The statement checks two items: 1. that the value contained in the 'grid_code' field is the smallest of those present within the buffer distance. And 2. that the value in the same field is greater than a minimum specified by the user and taken in by a parameter much earlier in the script. The AddMessage was a troubleshooting effort to validate that the different elements of the if statement are being passed in correctly. I can confirm they are. The if/else statement nested (intended to check for the existence of a dataset) is never triggering, nor are any of the commands within. Aug 9 '17 at 17:03
  • @Hornbydd I can confirm the coordinate systems are common, and the buffer is being produced. I'm beginning to believe that the syntax for the if/else statement within might be flawed? This is the first time I've used this approach to check if a file exists. if arcpy.Exists(Final_PP_Layer + "_O" + Point[0]): Aug 9 '17 at 17:38
2

@Hornbydd "Yeah it could be a duff path, your code does not show what Final_PP_Layer is so your concatenation of text may be invalid"

This was correct, python enters the loop with this syntax:

if arcpy.Exists(Final_PP_Layer + "_O{}".format(Point[0])):

Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.