-1

I am trying to populate a field [Hazard] based on the values in two other fields [Depth2D] and [Speed2D].

I have managed to write a code block in VB script that works but I really want to figure out how to write it in Python. I cannot seem to get it right.

Underneath is the current iteration of my attempt at python, from what I have seen I need to define the variables to use, but can't seem to define things right.

The VB Script code block is as follows -

Dim hazard

If [DEPTH2D]<0.1 Then
hazard = 1

elseif [DEPTH2D]>=0.1 AND [DEPTH2D]<0.2 AND [SPEED2D]<2 Then
hazard = 2

else
hazard = 3

end if

The Python code block I have tried is as follows

def ifBlock(depth2d,speed2d)

  if: depth2d < 0.1    
         return 1

  elif: depth2d >=0.1 AND depth2d <0.2 AND speed2d<2    
         return 2

  else:
         return 3

I then have

ifBlock([DEPTH2D],[SPEED2D])

in the formula box

The error I am getting in the results window is ERROR999999; Syntax Error

  • What does your attempt to write this in Python look like? We are not keen to be used as a code translation service but happy to help you where you get stuck. This should give you some ideas: gis.stackexchange.com/questions/99615 and may well be a duplicate. – PolyGeo Feb 7 '17 at 2:26
  • Hi there, many apologies, and thanks for the heads up. I have edited my query and included the current iteration of my python attempt. cheers – ALiOAWA Feb 8 '17 at 3:15
  • Your Python syntax is astray. What does your expression on the field calculator look like? What happens when you run it? Python uses elif not elseif and needs colons after if, elif and else statements. – PolyGeo Feb 8 '17 at 3:26
  • elseif is not valid in python. use elif. – Adam Feb 8 '17 at 3:52
  • Hi, thanks, I have edited my code block to replace elseif with elif and to include the colons. Unfortunately I am still getting an error - I have edited my original post with the updated code block. Thanks again. – ALiOAWA Feb 8 '17 at 4:47
1

You could define a function with your logic, as in:

def hazLevel(speed, depth):
   if depth < 0.1:
       return 1
   elif (depth < 0.2 and speed < 2):
       return 2
   else:
       return 3

...and in your field calculator form, toward the bottom should be a single line to enter your 'call' the above function (which should be copied to the code logic box. The single line call in this case feeds in the 2 fields for depth and speed:

hazLevel(!Speed2D!, !Depth2D!)

For the Python parser, the '!' delimits the fields...don't forget those, and to tick the Python parser in the field calculator window, as well as maintain proper indentation as shown.

  • hi there, thanks heaps for the help, your suggestion works. I ran it as you have written it and then also by adjusting mine to fit the same format - putting the colons at the end of each if/elif/else line, putting the multi-condition elif in brackets, and changing the delimiters to '!'. So thank you very much for that. – ALiOAWA Feb 24 '17 at 4:22
1

This logic is hard to program, this is why I am using these polygons, where Y is depth and Y is velocity.

enter image description here

Convert relevant fields data into points, accordingly and do a spatial join to above polygons to calculate hazard

  • Think you mean X is speed, not Y for both; +1 for the nice visual although the yellow stripe should be the same thickness as the blue.... Also, in the OP's logic the 'else if' line can leave out the >= 0.1 because the < 0.1 condition is already taken care of in the if clause – T. Wayne Whitley Feb 7 '17 at 3:05
  • Yes of course, X is speed. In regards of actual polygons shape I simply took ones from actual project. Hazard definitions vary from client to client. This is to show concept and often shapes are way more complicated – FelixIP Feb 7 '17 at 3:21

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.