Select to view content in your preferred language

Field Calculator

1254
6
Jump to solution
05-21-2014 10:43 AM
BrettAuger1
Deactivated User
Hi folks,

I am new to this python game and hopefully I can get pointed in the right direction. I am trying to run a field calculator python script using an IF THEN statment.

My data exists like this:
Species Code 1
Species Code 1 Percentage
Species Code 2
Species Code 2 Percentage
Species Code 3
Species Code 3 Percentage
And so on to 7.

I am trying to fill in an area field using a logic like this:

PL_vol
If Species code 1 = PL
Then Species Code 1 Percentage x Area

If Species Code 1/= PL
Then move to species code 2 to validate.

Essentially what I am trying to do is search the species code fields for PL then multiply the corresponding value by an area.
If somebody could point me in the right direction or has suggestions I would be very greatful.

Thanks,
Brett

I am using ARCGIS 10.0INFO
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RichardFairhurst
MVP Alum
Hi folks,

I am new to this python game and hopefully I can get pointed in the right direction. I am trying to run a field calculator python script using an IF THEN statment.

My data exists like this:
Species Code 1
Species Code 1 Percentage
Species Code 2
Species Code 2 Percentage
Species Code 3
Species Code 3 Percentage
And so on to 7.

I am trying to fill in an area field using a logic like this:

PL_vol
If Species code 1 = PL
Then Species Code 1 Percentage x Area

If Species Code 1/= PL
Then move to species code 2 to validate.

Essentially what I am trying to do is search the species code fields for PL then multiply the corresponding value by an area.
If somebody could point me in the right direction or has suggestions I would be very greatful.

Thanks,
Brett

I am using ARCGIS 10.0INFO


I am a VB Script guy with the Field Calculator.  This should work:

Parser:  VB Script

Use Codeblock: Checked

Pre-Logic Script Codeblock:
Percent = 0 If Not IsNull([Species Code 1]) Then   If [Species Code 2] = "PL" Then Percent = [Species Code 1 Percentage] End If If Not IsNull([Species Code 2]) Then   If [Species Code 2] = "PL" Then Percent = [Species Code 2 Percentage] End If If Not IsNull([Species Code 3]) Then   If [Species Code 3] = "PL" Then Percent = [Species Code 3 Percentage] End If If Not IsNull([Species Code 4]) Then   If [Species Code 4] = "PL" Then Percent = [Species Code 4 Percentage] End If If Not IsNull([Species Code 5]) Then   If [Species Code 5] = "PL" Then Percent = [Species Code 5 Percentage] End If If Not IsNull([Species Code 6]) Then   If [Species Code 6] = "PL" Then Percent = [Species Code 6 Percentage] End If If Not IsNull([Species Code 7]) Then   If [Species Code 7] = "PL" Then Percent = [Species Code 7 Percentage] End If Output = Percent * [Area]


Expression (your PL percent of area field):  Output

If no field contains "PL" then no percent of area is associated with the species and the output will be 0. It is assumed that only one Species Code field can contain "PL" for any given record.  Edited to handle NULL Species codes.

View solution in original post

0 Kudos
6 Replies
IanMurray
Honored Contributor
You'll probably need to use cursors to iterate over each row check the values of your fields and update the value of the field you are calculating.  Depending on how many species code fields, it could be rather lengthy code.  Hopefully this gets you pointed in the right direction.

http://resources.arcgis.com/en/help/main/10.1/index.html#//018z0000009r000000

http://resources.arcgis.com/en/help/main/10.1/index.html#//002z0000001q000000

These are the 10.1 Help, it should be relatively easy to find it for 10.0 on your computer using the arcgis help.
0 Kudos
RichardFairhurst
MVP Alum
Hi folks,

I am new to this python game and hopefully I can get pointed in the right direction. I am trying to run a field calculator python script using an IF THEN statment.

My data exists like this:
Species Code 1
Species Code 1 Percentage
Species Code 2
Species Code 2 Percentage
Species Code 3
Species Code 3 Percentage
And so on to 7.

I am trying to fill in an area field using a logic like this:

PL_vol
If Species code 1 = PL
Then Species Code 1 Percentage x Area

If Species Code 1/= PL
Then move to species code 2 to validate.

Essentially what I am trying to do is search the species code fields for PL then multiply the corresponding value by an area.
If somebody could point me in the right direction or has suggestions I would be very greatful.

Thanks,
Brett

I am using ARCGIS 10.0INFO


I am a VB Script guy with the Field Calculator.  This should work:

Parser:  VB Script

Use Codeblock: Checked

Pre-Logic Script Codeblock:
Percent = 0 If Not IsNull([Species Code 1]) Then   If [Species Code 2] = "PL" Then Percent = [Species Code 1 Percentage] End If If Not IsNull([Species Code 2]) Then   If [Species Code 2] = "PL" Then Percent = [Species Code 2 Percentage] End If If Not IsNull([Species Code 3]) Then   If [Species Code 3] = "PL" Then Percent = [Species Code 3 Percentage] End If If Not IsNull([Species Code 4]) Then   If [Species Code 4] = "PL" Then Percent = [Species Code 4 Percentage] End If If Not IsNull([Species Code 5]) Then   If [Species Code 5] = "PL" Then Percent = [Species Code 5 Percentage] End If If Not IsNull([Species Code 6]) Then   If [Species Code 6] = "PL" Then Percent = [Species Code 6 Percentage] End If If Not IsNull([Species Code 7]) Then   If [Species Code 7] = "PL" Then Percent = [Species Code 7 Percentage] End If Output = Percent * [Area]


Expression (your PL percent of area field):  Output

If no field contains "PL" then no percent of area is associated with the species and the output will be 0. It is assumed that only one Species Code field can contain "PL" for any given record.  Edited to handle NULL Species codes.
0 Kudos
BrettAuger1
Deactivated User
This solution I think is on the right track to what I need. I updated field caluclator with the following expression but I am getting an error now that reads " Type Mismatch: "Output'

EDIT: Made a mistake in the second part of code. This solution seems to have worked. Thank you for the help!
Percent = 0
If Not IsNull([SPECIES_CD_1]) Then
  If [SPECIES_CD_1] = "PL" Then Percent = [SPECIES_PCT_1]
End If
If Not IsNull([SPECIES_CD_2]) Then
  If [SPECIES_CD_2] = "PL" Then Percent = [SPECIES_PCT_2]
End If
If Not IsNull([SPECIES_CD_3]) Then
  If [SPECIES_CD_3] = "PL" Then Percent = [SPECIES_PCT_3]
End If
If Not IsNull([SPECIES_CD_4]) Then
  If [SPECIES_CD_4] = "PL" Then Percent = [SPECIES_PCT_4]
End If
If Not IsNull([SPECIES_CD_5]) Then
  If [SPECIES_CD_5] = "PL" Then Percent = [SPECIES_PCT_5]
End If
If Not IsNull([SPECIES_CD_6]) Then
  If [SPECIES_CD_6] = "PL" Then Percent = [SPECIES_PCT_6]
End If
Output = Percent * [Area_ha_1]




I am a VB Script guy with the Field Calculator.  This should work:

Parser:  VB Script

Use Codeblock: Checked

Pre-Logic Script Codeblock:
Percent = 0
If Not IsNull([Species Code 1]) Then
  If [Species Code 2] = "PL" Then Percent = [Species Code 1 Percentage]
End If
If Not IsNull([Species Code 2]) Then
  If [Species Code 2] = "PL" Then Percent = [Species Code 2 Percentage]
End If
If Not IsNull([Species Code 3]) Then
  If [Species Code 3] = "PL" Then Percent = [Species Code 3 Percentage]
End If
If Not IsNull([Species Code 4]) Then
  If [Species Code 4] = "PL" Then Percent = [Species Code 4 Percentage]
End If
If Not IsNull([Species Code 5]) Then
  If [Species Code 5] = "PL" Then Percent = [Species Code 5 Percentage]
End If
If Not IsNull([Species Code 6]) Then
  If [Species Code 6] = "PL" Then Percent = [Species Code 6 Percentage]
End If
If Not IsNull([Species Code 7]) Then
  If [Species Code 7] = "PL" Then Percent = [Species Code 7 Percentage]
End If
Output = Percent * [Area]


Expression (your PL percent of area field):  Output

If no field contains "PL" then no percent of area is associated with the species and the output will be 0. It is assumed that only one Species Code field can contain "PL" for any given record.  Edited to handle NULL Species codes.
0 Kudos
BrettAuger1
Deactivated User
One last question, say I wanted to change another value based on the if statment. Something like this:
Percent = 0
Volume = 0
If Not IsNull([Species Code 1]) Then
  If [Species Code 2] = "PL" Then Percent = [Species Code 1 Percentage] And Volume = [Species 1 Live Volume ]
End If
If Not IsNull([Species Code 2]) Then
  If [Species Code 2] = "PL" Then Percent = [Species Code 2 Percentage] And Volume=  [Species 2 Live Volume ]

End If
If Not IsNull([Species Code 3]) Then
  If [Species Code 3] = "PL" Then Percent = [Species Code 3 Percentage] And Volume= [Species 3 Live Volume ]

End If
If Not IsNull([Species Code 4]) Then
  If [Species Code 4] = "PL" Then Percent = [Species Code 4 Percentage] And Volume= [Species 4 Live Volume ]

End If
If Not IsNull([Species Code 5]) Then
  If [Species Code 5] = "PL" Then Percent = [Species Code 5 Percentage] And Volume= [Species 5 Live Volume ]

End If
If Not IsNull([Species Code 6]) Then
  If [Species Code 6] = "PL" Then Percent = [Species Code 6 Percentage] And Volume= [Species 6 Live Volume ]

End If
If Not IsNull([Species Code 7]) Then
  If [Species Code 7] = "PL" Then Percent = [Species Code 7 Percentage] And Volume= [Species 7 Live Volume ]

End If
Output = Percent * Volume



Thanks again,

Brett

I am a VB Script guy with the Field Calculator.  This should work:

Parser:  VB Script

Use Codeblock: Checked

Pre-Logic Script Codeblock:
Percent = 0
If Not IsNull([Species Code 1]) Then
  If [Species Code 2] = "PL" Then Percent = [Species Code 1 Percentage]
End If
If Not IsNull([Species Code 2]) Then
  If [Species Code 2] = "PL" Then Percent = [Species Code 2 Percentage]
End If
If Not IsNull([Species Code 3]) Then
  If [Species Code 3] = "PL" Then Percent = [Species Code 3 Percentage]
End If
If Not IsNull([Species Code 4]) Then
  If [Species Code 4] = "PL" Then Percent = [Species Code 4 Percentage]
End If
If Not IsNull([Species Code 5]) Then
  If [Species Code 5] = "PL" Then Percent = [Species Code 5 Percentage]
End If
If Not IsNull([Species Code 6]) Then
  If [Species Code 6] = "PL" Then Percent = [Species Code 6 Percentage]
End If
If Not IsNull([Species Code 7]) Then
  If [Species Code 7] = "PL" Then Percent = [Species Code 7 Percentage]
End If
Output = Percent * [Area]


Expression (your PL percent of area field):  Output

If no field contains "PL" then no percent of area is associated with the species and the output will be 0. It is assumed that only one Species Code field can contain "PL" for any given record.  Edited to handle NULL Species codes.
0 Kudos
RichardFairhurst
MVP Alum
One last question, say I wanted to change another value based on the if statment. Something like this:
Percent = 0
Volume = 0
If Not IsNull([Species Code 1]) Then
  If [Species Code 2] = "PL" Then Percent = [Species Code 1 Percentage] And [Species 1 Live Volume ]
End If
If Not IsNull([Species Code 2]) Then
  If [Species Code 2] = "PL" Then Percent = [Species Code 2 Percentage] And [Species 2 Live Volume ]

End If
If Not IsNull([Species Code 3]) Then
  If [Species Code 3] = "PL" Then Percent = [Species Code 3 Percentage] And [Species 3 Live Volume ]

End If
If Not IsNull([Species Code 4]) Then
  If [Species Code 4] = "PL" Then Percent = [Species Code 4 Percentage] And [Species 4 Live Volume ]

End If
If Not IsNull([Species Code 5]) Then
  If [Species Code 5] = "PL" Then Percent = [Species Code 5 Percentage] And [Species 5 Live Volume ]

End If
If Not IsNull([Species Code 6]) Then
  If [Species Code 6] = "PL" Then Percent = [Species Code 6 Percentage] And [Species 6 Live Volume ]

End If
If Not IsNull([Species Code 7]) Then
  If [Species Code 7] = "PL" Then Percent = [Species Code 7 Percentage] And [Species 7 Live Volume ]

End If
Output = Percent * Volume



Thanks again,

Brett


I have no idea what you mean to do with this calculation.  What you have written clearly will not work unless you want all Percents to be equal to -1 or 0 (True of False).  The AND operator makes the evaluation result in only True or False outcomes or an error (not sure which, but you don't want either).

One calculation can only calculate one field, so you cannot simultaneously calculate an area and a volume table field.  Just custom write the calculation for each separate field and save them separately if that is what you mean to do.

I don't see any reason to actually combine volume and area in a single calculation, unless you were testing to see if one or the other was not null and use the one that is not null.  But you had better explain why you want to combine volume and area in one calculation in the first place before I write any alternative code.

What is the relationship between the area field and the volume field and why would one field affect the other or the calculation result and not just be parts of separate calculations?
0 Kudos
BrettAuger1
Deactivated User
After re-reading my ask I realize that I type a bunch of nonsence and I actually dont have a second question.

Thank you for your support. This has been a pain in my side a a while.

Brett
0 Kudos