Timeline for Writing conditional (if/then) statements into ArcGIS Field Calculator using Python Parser?
Current License: CC BY-SA 3.0
14 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jun 2, 2014 at 22:29 | comment | added | Michael Stimson | The link resources.arcgis.com/en/help/main/10.1/index.html#//… provided in the answer is a basic tutorial, about half way down is a nice picture on how to use the field calculator. Unlike the VB field calculator expressions the python function list is just python, though I don't know about importing specific modules like sys, os, math, if you go here: docs.python.org/2/tutorial to get a tutorial of the basics of python (not arcpy). | |
Jun 2, 2014 at 22:18 | comment | added | sirgeo | @>= is the operator for greater or equal. – Michael Miles-Stimson Thanks a bunch! Do you know of list for commonly needed python field calculator expressions?... Something not like the Esri help web pages would be ideal; just concise easy to follow list using examples like the code you provided above. | |
Jun 2, 2014 at 22:05 | comment | added | Michael Stimson | @radouxju I used to put the parenthesis around the conditions, sort of 'C' style but got pinged because they were unnecessary. I like them, and use them in my own code, they help to isolate the if statement and contribute to the readability. | |
Jun 2, 2014 at 21:52 | comment | added | Michael Stimson | >= is the operator for greater or equal. | |
Jun 2, 2014 at 14:43 | vote | accept | sirgeo | ||
Jun 2, 2014 at 14:05 | comment | added | sirgeo | S O L U T I O N i.imgur.com/I7ijD9F.png I forgot to replace FromCode with GRIDCODE. It is working! Just one last thing how do you write greater OR EQUAL ? | |
Jun 2, 2014 at 13:56 | comment | added | sirgeo | screen shot here: i.imgur.com/8UQD8l9.png | |
Jun 2, 2014 at 13:51 | comment | added | sirgeo | okay, it's running but only populating bedrockdep field with "N/A". Not sure how to post code in threads <cntl + k> { } ?? Here's the paste of the Pre-Logic: def GRIDCODE(FromCode): if (FromCode > 0) and (FromCode < 50): return "0 - 50 cm from the surface" elif (FromCode < 150): return "101 - 150 cm" elif (FromCode < 200): return "151 - 200 cm" elif (FromCode < 250): return "201 - 250 cm" elif (FromCode < 1000): return "251 - 1000 cm" else: return "N/A" | |
Jun 2, 2014 at 8:54 | comment | added | radouxju | good answer, but I recommend using the parentheses in the condition: if (FromCode > 0) and (FromCode < 50): | |
Jun 2, 2014 at 5:05 | comment | added | sirgeo | GRIDCODE is the only value from the attribute table being used. The toc has the 6 classes that need to be added to the bedrockdep field based on the GRIDCODE field. I will give your code a shot in the morning and post the results here. THANKS | |
Jun 2, 2014 at 4:26 | history | edited | Michael Stimson | CC BY-SA 3.0 |
added 539 characters in body
|
Jun 2, 2014 at 4:15 | comment | added | Michael Stimson | Fair enough, the problem is that you're using !gridcode! as the function name. The field bedrockdepmin is the one that gets the exclamation marks. It has to do with the scope of the variable.. only the lower box allows substitution, the pre-logic code deals only with variables and the return value is what is populated into the selected field. I can't see the field that you are using to calculate from brockdepmin in the screenshot, is it there? or is it the GRIDCODE? | |
Jun 2, 2014 at 3:56 | comment | added | sirgeo | i.sstatic.net/JEmEH.png is link to the screenshot | |
Jun 2, 2014 at 3:46 | history | answered | Michael Stimson | CC BY-SA 3.0 |