If Statement Satisfied but not Entering?

494
10
Jump to solution
08-08-2017 12:10 PM
TylerSearls2
New Contributor III

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. Any help would be appreciated. The 'Warning' Messages a just to trigger a different font effect in the display box as the tool runs. The if statement at line 21 in the attached file is the issue, though as I've said it triggers no errors.

0 Kudos
1 Solution

Accepted Solutions
TylerSearls2
New Contributor III

I have come up with an approach to automate pour point creation with slight setbacks from a given stream's delineated outlet (this was the intent):

1. Create a point layer with points for each vertice in the stream network layer
2. Buffer each vertice a (small) desired amount (i arbitrarily chose 10 metres)
3. Run intersect and create points as the output, this will create a series of points where the buffer intersects the stream layer at the setback amount.
4. Identify which points of intersection are of the desired order or greater. Remove those that are not.
5. Identify the smallest remaining order within the buffer. Any points greater than the smallest order (i.e the stream being flowed into) will be removed.
6. Left with the smallest order points which are greater than the specified minimum order of concern.
7. These are the pour points (automated). I snapped the pour points back half of the original setback (5m). So the pour point should be 5 or so metres from the actual outlet (dependent on the shape of the stream), which makes for clean delineation.


Hoping the brief description is clear.

View solution in original post

10 Replies
JoshuaBixby
MVP Esteemed Contributor

How are you determining that the if conditions are being met if the code afterwards is never executing?

0 Kudos
TylerSearls2
New Contributor III

Joshua Bixby‌ I added the 'AddMessage' function to verify the values in the if statement were being passed in correctly. They are.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Since Line 21 is working, i.e., being met, are you not seeing either the if or else working afterwards?

0 Kudos
TylerSearls2
New Contributor III

I'm not, the script is certainly not entering the statement at line 22 or 25. I'm thinking this is the issue, but I can't identify any issues with the syntax? Further, if I am entering the statement at 21, should I not be entering the else if the 'if' on 22 is repeatedly false? I would expect some error if I were appending to a file which doesn't exist...

0 Kudos
RandyBurton
MVP Regular Contributor

Are you seeing the message generated by arcpy.AddMessage in lines 19-20?  And likewise, are you seeing the arcpy.AddWarning messages from lines 23, 26 and 31?  You may need to use GetMessages.

And what are some of those messages?

0 Kudos
TylerSearls2
New Contributor III

All messages are coming through less 23, 26 and 31. Is my syntax for the if loop on line 22 correct? I'm looking to confirm a dataset is there and if not create one. Most of the messages are to give users in the office an idea of whats going on without having to interact with python.. The message on line 19 was added to confirm the different elements of the if statement on line 21 are being passed in correctly - they seem to pass in fine. I apologize for the difficulty in reading the code, this has expanded to a larger script than anticipated and my experience in python is not extensive.

0 Kudos
RandyBurton
MVP Regular Contributor

Could you post a message that was printed at line 19-20?

Point[0] == orders[0] and Point[0] >= Minimum_Stream_Order_To_Delineate
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I see where Randy is going, you say the values are being passing just fine, but it seems you are doing that by looking at the variables and not by having code between lines 21 and 22 actually add a message. 

RandyBurton
MVP Regular Contributor

Add an else at the end of your script (match the indentation of your "if Point[0] ==...."

    for Point in Points:
        arcpy.AddMessage( .... )
        if Point[0] == orders[0] and Point[0] >= Minimum_Stream_Order_To_Delineate:
            if arcpy.Exists(Final_PP_Layer + "_O" + Point[0]):
                stuff
            else:
                more stuff
        # add an else here to check if point[0], orders[0] and Min_Stream... meets criteria
        else:
            AddMessage ... "Failed"‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍