2

I am trying to calculate the angle of some specific points generated from flow direction in ArcMap 10.2. I converted the DEM to flow direction and then to points. I am trying to convert the grid_code of the generated points into specific angles with a certain condition. I tried using the below Python code in the Field Calculator but it gives an error as shown in the image attached.

Can someone explain how to do this right?

generated error

    ##########################
def Reclass ([grid_code]):
if ([grid_code] == 64):
                        return 0
            elif ([grid_code] == 128) :
                        return 45
            elif ([grid_code] == 1) :
                        return 90
            elif ([grid_code] == 2) :
                        return 135
            elif ([grid_code] == 4) :
                        return 180
            elif ([grid_code] == 8) :
                        return 225
            elif ([grid_code] == 16) :
                        return 270
            elif ([grid_code] == 32) :
                        return 315
  Reclass(![grid_code]!)
###########################

Here is how the attribute table looks like

attribute table

  • 1
    what details did the geoprocessing window give you? – Paul H Mar 22 at 17:34
  • 2
    Please be sure to place the error message in the question as text. This makes the error legible on all devices and searchable by others with the same issue. – Vince Mar 22 at 18:16
5

I know ArcGIS's python interpreter is weird, but I'm pretty sure your syntax is wrong. My recollection is that you don't need to use their weird escape characters within the scope of the function definition.

So your code would become:

def reclass(grid_value):
    if (grid_value == 64):
        return 0
    elif (grid_value == 128):
        return 45
    elif (grid_value == 1):
        return 90
    elif (grid_value == 2):
        return 135
    elif (grid_value == 4):
        return 180
    elif (grid_value == 8):
        return 225
    elif (grid_value == 16):
        return 270
    elif (grid_value == 32) 
        return 315

However, I think you should save yourself some typing and use a dictionary instead of all of those if/elif statements:

def reclass(grid_value):
    lookup = {
        64: 0,
        128: 45,
        1: 90,
        2: 135,
        4: 180,
        8: 225,
        16: 270,
        32: 315,
    }
    return lookup.get(grid_value, None)

Then you'd call this function as you did (maybe -- I don't quite recall which escape characters are needed)

reclass(!grid_code!)

As an aside, if you were going to do this in geopandas, you would do the following:

lookup = {
    64: 0,
    128: 45,
    1: 90,
    2: 135,
    4: 180,
    8: 225,
    16: 270,
    32: 315,
}

(
  geopandas.read_file('path/to/file.shp')
    .assign(angle=lambda df: df['grid_code'].map(lookup))
    .to_file('path/to/file.shp')
)

In my experience, geopandas-based solutions, when available and applicable, tend to be faster than even opening ArcMap on my machine.

  • 1
    reclass(!grid_code!). Also, your indentation is correct and could've been a problem for the OP. – Andy Mar 22 at 18:05
  • @Andy thanks for the tip. I've edited my answer to correct the escape characters – Paul H Mar 22 at 18:32
  • +1 for dictionary. You can place lookup above def. – FelixIP Mar 22 at 20:06
  • Clever method to lookup values without a bunch of if/elif statements! – RyanDalton Mar 22 at 21:09

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.