Error while reclassifying slope value using calculate field

852
8
Jump to solution
04-27-2022 06:06 AM
KedaravindanBhaskar
New Contributor III

Hello, I am having trouble reclassifying slope value into a new field.

I have attached an image with the dialogue box for the calculate field tool, including the code I am using.

Calculate field error while reclassifying slope.PNG
Something(s) to consider - I am using the tool in the workflow for a model and have used an iterator before the tool. Hence the "%Value%_Slope_Type"(Inline variable substitution). I also tried executing the tool outside the model but I get the same result.

The error message I receive is as follows -  ERROR 000013: N_Slope_Type already in N_Sample_Lines
Failed to execute (Calculate Field).

I am using ArcGIS Pro version 2.9.0.

Any help would be greatly appreciated. 

Regards, Kedar

 

0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor

Hi Dan. 

Here's my fix

Field Name: %Value%_Slope_Type 

Expression: reclass(!%Value%_Slope!) (I am assuming this is a field name (of a numeric field in your table, since you are doing a numeric comparison in your code block)

Code Block: (unlike label expressions you must use a local variable here. Make sure you handle the 0 or None [null]  case)

 

def reclass(vs):
    if vs < 0: 
        return 1
    elif vs > 0:
        return 2
    else:
        return 0

 

 

The error message (ERROR 000013: N_Slope_Type already in N_Sample_Lines) may reflect that first time @KedaravindanBhaskar  ran the model the field N_Slope_Type didn't exist yet, so the field existed already from then on but ModelBuilder didn't know it so tried to add it hence the error.  The Calculate Field tool has the capability of creating a field as it runs, but because of ModelBuilder validation issues I do not use this feature as it may work the first time but not in future iterations because of validation confusion (the validation tells GP the field does not exist but in iteration two or another run, it exists!) (I think this was a bad design decision, but I'm sure they had their reasons.)  When I am creating a new field I run the Add Field tool first (in your case adding the field %Value%_Slope_Type) because if the field exists, Add Field just passes a warning and moves on.

Hope this helps.

View solution in original post

8 Replies
RhettZufelt
MVP Frequent Contributor

Not sure with inline substitution but normally the expression box you call the def in the Code Block and pass the field variable.

Instead of %Value%_Slope, try this:    reclass(!%Value%_Slope!)

R_

KedaravindanBhaskar
New Contributor III

Hi Rhett, thanks for your response,

I changed "%Value%_Slope" to "reclass(!%Value%_Slope!)" but the calculate field tool greyed out. I also changed the expression type to Arcade and ran the new code. That didn't work either.

Any thoughts?

Regards, Kedar




0 Kudos
DanPatterson
MVP Esteemed Contributor

Also, you have a space in the new or existing field name, remove it or replace it with an _ .


... sort of retired...
KedaravindanBhaskar
New Contributor III

Hi Dan, thanks for your input,

Silly error there, but this doesn't seem to be the problem I am facing. Even after removing the space, the same error message shows up.

Regards, Kedar 

0 Kudos
DanPatterson
MVP Esteemed Contributor

see the calculate field example here

Inline variable substitution—ArcGIS Pro | Documentation

it seems the field and value are separated

I don't use modelbuilder so you may have to experiment with the correct syntax

@curtvprice do you have any ideas?


... sort of retired...
0 Kudos
KedaravindanBhaskar
New Contributor III

Thank you once again, Dan,

Will test it and let you know the outcome

Warm regards, Kedar

0 Kudos
curtvprice
MVP Esteemed Contributor

Hi Dan. 

Here's my fix

Field Name: %Value%_Slope_Type 

Expression: reclass(!%Value%_Slope!) (I am assuming this is a field name (of a numeric field in your table, since you are doing a numeric comparison in your code block)

Code Block: (unlike label expressions you must use a local variable here. Make sure you handle the 0 or None [null]  case)

 

def reclass(vs):
    if vs < 0: 
        return 1
    elif vs > 0:
        return 2
    else:
        return 0

 

 

The error message (ERROR 000013: N_Slope_Type already in N_Sample_Lines) may reflect that first time @KedaravindanBhaskar  ran the model the field N_Slope_Type didn't exist yet, so the field existed already from then on but ModelBuilder didn't know it so tried to add it hence the error.  The Calculate Field tool has the capability of creating a field as it runs, but because of ModelBuilder validation issues I do not use this feature as it may work the first time but not in future iterations because of validation confusion (the validation tells GP the field does not exist but in iteration two or another run, it exists!) (I think this was a bad design decision, but I'm sure they had their reasons.)  When I am creating a new field I run the Add Field tool first (in your case adding the field %Value%_Slope_Type) because if the field exists, Add Field just passes a warning and moves on.

Hope this helps.

KedaravindanBhaskar
New Contributor III

Hi Curt. It worked!

Here's what I did - 

- I inserted an Add field before the calculate field as you said.

- The details of the calculate field are in the image of the dialogue window -Calculate field error while reclassifying slope FIXED.PNG

Thank you so much, Curt!

Regards, Kedar




0 Kudos