Syntax of subtype values in codeblock

536
5
08-23-2017 08:00 AM
AaronKoelker
Occasional Contributor III

I have a pair of data sets with identical schemes; one is a sort of reference layer (A) and the other one is an editable working layer (B). These data sets eventually make their way into a web map for Collector. There is a field using subtypes that help restrict domains for other fields, which looks like:

CodeDescription
1Access
2Anthropocentric
3Float
4Generic Karst
5Other
6Outcrop

My workflow includes appending the data in layer (B) to layer (A). The problem is that the subtype field displays its numeric code instead of its description in pop-ups for the web map. For ease of use I need the description to display, so I wanted to calculate the description into a second text field. Eventually this will be a python script, but for testing purposes I've been using the field calculator in ArcMap to check that the code is right before I incorporate it into my larger script. The below example successfully runs without error, but instead of returning the description in my "if" statement, I'm always getting the "else: nope". 

Pre-logic script code

def myFunc(ftype_int):
    if (ftype_int == 4):
       return "Generic Karst"
    else:
       return "nope"‍‍‍‍‍

Expression

myFunc(!FTYPE!)

In place of the (ftype_int == 4) I've tried (ftype_int == "4"), (ftype_int == "Generic Karst"), (ftype_int == "4 - Generic Karst"), but# have had no luck. 

How do I get the field calculator to properly read the values in the subtype field? Hope this makes sense. 

Thanks

-Aaron
0 Kudos
5 Replies
DanPatterson_Retired
MVP Esteemed Contributor

instead of printing 'nope' print the value it is returning to make sure it is getting the right field

0 Kudos
AaronKoelker
Occasional Contributor III
def myFunc(ftype_int):
    if (ftype_int == 4):
       return "Generic Karst"
    else:
       ftype_int‍‍‍‍‍‍‍‍‍‍
myFunc(!FTYPE!)‍

Hm, modifying my "else" statement to the above gives me a <NULL> value in my FTYPE field. 

-Aaron
0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

then !FTYPE! isn't the field or it is populated with <null> I don't suppose Description would be the field?

0 Kudos
AaronKoelker
Occasional Contributor III

There are only the two fields: "ftype_int" which is the 'short' field that has the subtype values, and "ftype" which is the 'text' field I'm trying to calculate to. 

Plugging in some of the other unrelated fields from the table into my "else" statement, I've noticed that everything gives me a <Null> value for my ftype field. The only thing that hasn't given me a <Null> return is telling it to print something specific like "nope". 

Thanks for taking the time

-Aaron
0 Kudos
AaronKoelker
Occasional Contributor III

I ended up using another method because I just couldn't figure out what the heck was wrong here. Didn't seem to be able to read any field I tried, so must not be an issue specific to subtype. Instead I broke it into two steps: calculated the numeric codes into my string field, then used the .replace() function to swap out the codes and descriptions. Would still be curious to know why I couldn't get the if/else statements to work, though. 

-Aaron
0 Kudos