Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm trying to string together a field calculator script that will populate all the records of a field at once based upon values in a number of fields. I can't even get the first part of the command to work and can't figure out how to get all the other conditions to fit in.

Here's a simplified version:

NewFIELD: the field I would like to update

Fields = COVER1 and COVER2 (values are three letter codes) and LANDUSE (Values are WET, FOR, COM, RES...). There are other fields I'll be selecting on but for an initial example this should do.
One complication is that there is some overlap between results depending upon how one selects the records. So one record (polygon) could fall into two Forest/Landcover groups (selected first by species composition and secondly by LANDUSE) but I may want to update this new field with only the first Forest class. So I need to do the selections sequentially and explicitly exlude records that have already been defined.

IF [COVER1] = "WWW" OR [COVER1] = "OWW" OR [COVER1] = "SFW" OR [COVER1] = "WAT" THEN
  F2 = "Wetland"
ELSE IF ([COVER2] = "WS" OR [COVER2] = "BF" OR [COVER2] = "CE" OR [COVER2] = "LA") AND
[DRAINTYPE] ="IMPERFECT" OR [DRAINTYPE] ="POOR") AND [NewFIELD] <> "Wetland"
  F2 = "Forest1"

The first statement updates NewFIELD with "Wetland" for all those records to which it applies. The second statement will apply to some of those that were selected in the first statement but I want them to remain as "Wetland"

As mentioned, I can't even get a simple statement to work so I suspect my initial syntax is horribly wrong.

My first try looks like this and didn't work:

dim f2
if [COVER1] = "SDW" OR [COVER1] ="BSB" then
  f2 = "Beach"
end if

So, any thoughts on how I would approach this? I can do it by selecting all applicable records using Select by Attributes and then updating NewFIELD (doing this 9 or ten times) but it'd be nice to do this all at once. And I'm not familiar with Python so don't know where to start with that. But I'd be interested in trying it.

Looking at this thread ("If then" VB Script code (or Python) equivalent to SQL's IN ('x','y',z') expression in ArcGIS Field Calculator question?) it looks like I may be able to use the SQL IN function but I'm still unclear about it.

Thanks in advance. Phil

share|improve this question
1  
just to check, you are calculating on the f2 field, you have the 'Advanced' check box checked, your main statement is in the Pre logic VBA Script Code textbox, and your f2 variable is listed in the f2 = textbox? –  artwork21 Jun 22 '11 at 17:27
    
@artwork21, yup, the main statement was in the Pre-Logic Script code section (ArcMap 10) and I have now got that first part to execute properly. Now I just need to try stringing a series of these together. P. Greyson –  phyllo Jun 23 '11 at 12:50
add comment

1 Answer

i don't know if this helps since i just rewrote the logic in python, but it's a lot clearer to read. Run it, let me know what you see/get

make a backup copy of your data set the Expression Type to PYTHON

put this in your expression

conditionalupdate(!COVER1!, !COVER2!, !DRAINTYPE!, !NewField!)

set this in your code block

def conditionalupdate(cov1, cov2, draintype, newfield):
  if cov1 in ['WWW', 'OWW', 'SFW', 'WAT']:
    return 'Wetland'
  elif (cov2 in ['WS', 'BF', 'CE', 'LA']) and (draintype in ['IMPERFECT','POOR']) and (newfield != 'Wetland'):
    return 'Forest1'
  else:
    return '??'

edit : fixed syntax error in code block

share|improve this answer
    
Hmm, got a Python syntax error. When you say "put this in your expression" I assume that means it goes in the bottom part of the window (e.g. where it says "Newfield ="). Python statement was arcpy.CalculateField_management("Landcover","NewField","conditionalupdate(!COVER‌​1!, !COVER2!, !DRAINTYPE!, !NewField! )","PYTHON_9.3","def conditionalupdate(cov1, cov2, draintype, testkey): if cov1 in ['WWW', 'OWW', 'SFW', 'WAT']: return 'Wetland'/n elif (cov2 in ['WS', 'BF', 'CE', 'LA']) and (draintype in ['IMPERFECT','POOR']) and (testkey != 'Wetland'): return 'Forest1' else: return '??' –  phyllo Jun 24 '11 at 17:18
    
Sorry gotchula for the poorly parsed reply but I haven't worked out formatting in the comment field. The Python statement I grabbed from the Results window. –  phyllo Jun 24 '11 at 17:20
    
i had a syntax error, trying to be too fancy with the code. copy the above code block again and try –  gotchula Jun 24 '11 at 23:17
    
I saw your edit, which was more readable .. if you want to break the elif logic over several lines (rather than in just one long line), just use an additional set of parenthesis to group the ( start and end ): of the logic statements. See PEP 8 for more recommended Python code style details. –  Mike T Jun 25 '11 at 6:15
    
Thanks all (@gotchula, @mike-toews). After much playing with indentation and such I got my 16 or 17 elif statements to evaluate, based on the model of gotchula's code above. –  phyllo Jul 18 '11 at 12:26
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.