3

I am trying to do a simple (or so I thought) if/then statement in the ArcGIS field calculator.

I am trying to add the HydroCode field - hydologic soil codes - to an existing data set. I know what the code should be based on the "MUSYM" number. For example, "MUSYM" 1667 has the hydrologic soil code A. The "MUSYM" field is a number and the "HydroCode" field is text.

I vaguely remember doing if/then statements years ago when I more actively used GIS. I have tried simple If "MUSYM" = 1667 Then "HydroCode" = A Endif along with some other variations but none are working.

What am I missing?? All of the help I have found online deals with very complex coding, I can't find anything for such a simple statement. I also can't find anything with just basic operational instructions as far as where the operators should go/punctuation/etc.

enter image description here

2
  • 1
    Can you post the code you're trying in your Pre-Logic Script Code? I am assuming you're setting up a function or so and passing in values there?
    – Branco
    Commented May 13, 2015 at 17:14
  • Two questions here that provide simple VB if/then statement examples: gis.stackexchange.com/questions/81817 and gis.stackexchange.com/questions/16218 However, along the same lines as jlcrawf0's answer I'd suggest you don't really need an if statement here. You just need the table that relates hydrocode to musym, which should be 1:1, and then join that to your shapes using musym as the join field.
    – Chris W
    Commented May 14, 2015 at 0:04

7 Answers 7

4

Normally, I try to direct anyone using the Field Calculator to use Python. But to answer your question directly, you'll need something similar to this below. It's a basic pattern of if/elseif/end if.

(substitute your own values of course, I use 9999/Z just to show an elseif)

enter image description here

Note: You mention that MUSYM is a number, but the way the values in your table are aligned to the left makes me think it is stored as a text field. If so, try the below instead:

enter image description here

2

Pre-Logic script code

Dim val

If [MUSYM] = 1667 Then
     val = "no"

elseif [MUSYM] = 9999 Then
     val = "yes"

end if

Hydrocode = val

1

Please let me know if I am misunderstanding your work. It looks like you want to add HydroCodes to NRCS SSURGO data. What is the source of the HydroCodes? Are HydroCodes something in the SSURGO database, like the Hydrologic Soil Group?

Most of us don't work with NRCS data in the US, so here is a description. NRCS SSURGO data consists of a shapefile of Map Unit polygons, and an MS Access database with about 60 soil-related attributes for each map unit. Many of the attributes exist in a one-to-many relationship with the Map Units, so the data is typically aggregated for mapping. For example, each map unit could have several soil types, but the map will only show the dominant soil type. The data is aggregated using built-in queries in the database.

If you are using attributes that are in the SSURGO database, then you will not need to use Field Calculator to add them. Instead, use this awesome tool for extracting soil data by automatically joining aggregated attributes in the database with the Map Unit polygon shapefile in ArcGIS. I have ArcGIS 10.2, and it work for me. NRCS Soil Data Viewer

http://www.nrcs.usda.gov/wps/portal/nrcs/detail/soils/survey/geo/?cid=nrcs142p2_053620

If you want to see how the NRCS SSURGO data works without ArcGIS, try the web viewer at

http://websoilsurvey.nrcs.usda.gov/app/

1
  • This answer looks interesting regardoing NRCS soil data but it does not answer the initial question regarding if/else statements. I suggest that you move this answer to a new question with user answer with the heading SSURGO database, NRCS soil so that users who come upon this question can immediately find the solution to their problem in the other answers and users interested in NRCS can find an answer easily.
    – kl-higgins
    Commented Sep 29, 2017 at 14:17
0
if [value] = 1 Then
n = "lowest"
elseif [value] = 2 Then
n = "low"
elseif [value] = 3 Then
n = "moderate"
elseif [value] = 4 Then
n = "high"
elseif [value] = 5 Then
n = "highest"

end if

I used this code similar to above and found it useful in my case. I tried to assign the name of a separate column using another column on big data.

1
  • 2
    Welcome to GIS.SE. Please edit your answer in order to enhance the formatting, also point out, how your answer is different and/or superior to other provided solutions. Thank you.
    – Erik
    Commented Mar 11, 2020 at 7:56
-1

My answer to a similar question works for this one also. The Hydrologic Soil group code is in the component table and is easily queried and joining to the spatial. the query can be completed in the MS Access database provided with a ssurgo download or queried from the Soil Data Access site.

MUKEY is your join column. Use a dominant component sql on the Soil Data Access site

select m.mukey, hydgrp from legend l 
    inner join mapunit m on l.lkey=m.lkey and areasymbol like 'PA%' 
         inner join component c 
              on m.mukey=c.mukey and c.cokey = (
                   SELECT TOP 1 component.cokey 
                   FROM component 
                   WHERE component.mukey=m.mukey 
                   ORDER BY component.comppct_r DESC
              )
              ORDER by mukey

Import the results into your GIS session, then join to the spatial.

-1

This won't answer your question directly, but will meet your needs in a fairly efficient manner as well.

First, sort "MUSYM"in ascending/descending order so that equal values are grouped together. Second, have the editor toolbar open and click start editing (focusing on the layer which needs editing of course). Third, open attribute table that needs editing and also click the attribute icon on the editor toolbar. The latter should bring up a window headlined "Attributes."

From here you can do mass edits/copy/paste. Simply highlight the rows that need changing in the attribute table (in your case, all MUSYM values that are 1667 which are now grouped together). You can click the top most entry, hold the shift button, and click the bottom most 1667 entry. Once highlighted, you'll see these values in the attribute window as well. Select all of the values in this window and below, you will be given the to option add information for each field. Anything you type in the fields will automatically apply to all selected rows in your attribute table.

Save edits and you're done! i find this can be just as fast as coding if you have just a handful of categories but large counts for each.

For more information, look at : http://desktop.arcgis.com/en/arcmap/10.3/manage-data/editing-attributes/applying-the-same-attribute-values-to-multiple-features-in-a-layer.htm

also attaching image in case a visual helps.

enter image description here

-1

I share a tutorial on the subject, I hope it is useful. https://www.youtube.com/watch?v=SBGNxaei6mg

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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