Use Modulus to Identify Layers Divisible by a Number

Contours StartRecently I had a need to filter features in a layer by a specific numeric value in ArcGIS Desktop. Specifically, I created contour lines at one foot intervals, and needed to display the 10 foot contour lines with a thicker line and label them. This is accomplished by creating a new layer and use a definition query to only show the 10 foot lines. Definition queries in ArcGIS filter features using SQL, displaying only features meeting the criteria specified.

Identifying the 10 foot contour lines is accomplished using the modulus operator. Modulus calculates the remainder of one number divided by another. If the remainder is zero, the first number is divisible by the second.

SQL supports modulus, but definition queries in ArcGIS do not support full SQL. A quick test discovered modulus falls into the unsupported category. Therefore, the solution is to create a new field and calculate the field using the modulus operator to identify the layers to display.

Add Field in Options menuNew field creation can be accomplished right in ArcMap. Make sure you are not in an editing session. ArcGIS will not allow you to make schema changes while you are editing. Adding a field is a schema change. Add the field by clicking on the options button with the attribute table open and select Add Field from the menu. In the add field dialog, pick a name for the field and set the field type to short integer. Yes, I said short integer. It will make sense in a minute.

With the new field created, now it is time to calculate the field. This field will be used to note whether the feature is a contour divisible by 10 (60, 80, 90, etc) or not. What we are creating is a true or false field to indicate this. Why did we make it an integer you ask? It is because of the way the computer views boolean (true/false) values. To the computer, zero is false and one (or any other value of that matter) is true. We are going to take advantage of this when we use the field calculator.

Open up the field calculator by right clicking on the new field header and selecting Field Calculator from the context menu. In this instance, the contour field is storing the elevation I am interested in, so I need to determine if contour is divisible by 10. Since I use Python most of the time, this is the language I will be using. Hence, the first step is to select the Python radio button at the top of the dialog to select the parser.

Calculate Field Dialog - Select Python Parser

The next step is entering the code to calculate the field. The modulus operator is the percent sign (%). Using the modulus operator, it will look like this.

Code for Calculating Field

This is literally telling ArcGIS to use Python to evaluate the following statement. Is the contour value modulus 10 equal to zero. In slightly more intelligible English, this translates to is the remainder zero when dividing the contour value by 10?

If the value is divisible by zero, it will return true. Since it is a computer, the computer returns a one to indicate true. If the value is not divisible by 10, it returns false. Again, since it is a computer, it will return a zero. To change this statement for another value, simply replace 10 with another number.

Finished ContoursThe new field will be filled with zero or one. The last step to get the map set up with the contours is creating two layers with definition queries, one selecting all the zeros (not 10' contours) and the other selecting all the ones (10' contours). After selecting a slightly thicker line and labeling the 10' contour lines, it was done.