5

I am very new to GIS, so I hope to get some help. I am using ArcGIS 10.2 for Desktop.

I want to create a new numeric column on my attribute table to store "seasons" in numbers.

For example, I want the "seasons" field to hold a numeric code (i.e. "summer" will be "1", "autumn" to be "2", "winter" to be "3" and "spring" to be "4").

How can I do this using the field calculator?

0

3 Answers 3

7

I am guessing you are using ArcGIS from your terminology.

1) Add a new field called 'codes'. Type = Short Integer

2) Select by attributes --on the top menu-- "seasons = Summer".

3) Open the attribute table and in the field calculator click on the field you added in step 1 and type 1.

It will only do the selected records so all summers will get 1.

Repeat for Spring and so on.

You can use "if" in the field calculator but as your a newbie and you only have four classes just do selections for now.

Do not even bother turning on edit mode unless you need to "undo."

0
4

If you want to use QGIS you can use a CASE statement. In this example seasons is the name of the field that holds the text information

CASE WHEN seasons = 'summer' THEN 1
  WHEN seasons = 'autumn' THEN 2
  WHEN seasons = 'winter' THEN 3
  WHEN seasons = 'spring' THEN 4
END
0
2

Add your new short integer field to your attribute table, right click the field name, and click "Field Calculator".

At the top of the Field Calculator window, in the 'Parser' box, check the Python box/bubble. Below the 'Fields' list, check the "Show Codeblock" and enter the following code in the codeblock:

def Classify(a):
    if a == "summer": return 1
    elif a == "autumn": return 2
    elif a == "winter": return 3
    elif a == "spring": return 4

In the box below the codeblock (where you would otherwise assign a value to the field), type in:

Classify(!seasons!)

Then click OK. Your field calculator window should look like this:

enter image description here

Let me know if you have any trouble with it.

2
  • 2
    You can do it without defining a function. Just use a dictionary in the expression: {"summer": 1, "autumn": 2, "winter": 3, "spring": 4}[!seasons!]
    – nmpeterson
    Commented Apr 15, 2016 at 16:24
  • Great Idea! That would have the added benefit of dealing with potential indentation issues with the function (for those unfamiliar with python)
    – crld
    Commented Apr 15, 2016 at 16:26

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.