Geospatial Modelling Environment

addcodedfield (Add Coded Field To Table)

Adds a new field to a table and automatically codes it with a specified pattern of values


This command adds a new field to a table and automatically codes it with a specified pattern of values. The options for value generation are: i) constant: a constant value or text string is applied to all records, ii) sequence: the tool populates records starting with a specified initial value, incrementing it by the specified increment value after each record, iii) repeat: the tool draws from a sequence of values, in order and looping back to the beginning when the end is reached, until all records have been populated, iv) normal: populates records with random draws from a normal distribution of a specified mean and standard deviation, v) uniform: populates records with random draws from a uniform distribution of a specified minimum and maximum.

The code will attempt to coerce the values it generates into the field type specified in the command. An appropriate field type must therefore be specified. Use the SHORT or LONG field types when generating integers (use LONG if the values exceed about +/- 32000), and use the DOUBLE field type for real numbers. Although the STRING field type can be used when generating any numeric values, it is inefficient to store numbers as strings and truncation of the values may occur. It is recommended that the STRING option only be used with the ‘constant’ option, where the constant provided is a text string.

The parameters can be combined in various ways to achieve a greater variety of functions. For instance, if you specify the field type as LONG, and use the sequence option with a start value of 1 and an increment value of 0.2, then the records are coded: 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, etc. Thus, the truncation that results from attempting to write a decimal point value to an integer field can be used to your advantage.

Note that if the specified field already exists then an error will be returned unless you have specified the update=TRUE option.

See also: field.delete


addcodedfield(in, field, fieldtype, [constant], [sequence], [repeat], [normal], [uniform], [update], [where]);

inthe input feature/table data source
fieldthe name of the field to add or replace
fieldtypethe field data type – short integer, long integer, double precision real number, or string (options: SHORT, LONG, DOUBLE, STRING)
[constant]codes the new field with the specified constant value (a single value expected), e.g.: 3.14 or “RIVER”
[sequence]codes the new field based on a start value and increment value (a start value and increment value expected), e.g.: c(1000, 1)
[repeat]codes the new field by cycling through the specified list of values (a list of values expected), e.g. c(1,2,3,4,5)
[normal]codes the new field by drawing random values from a normal distribution with a specified mean and standard deviation (a mean and standard deviation expected), e.g. c(100,5)
[uniform]codes the new field by drawing random values from a uniform distribution with a specified minimum and maximum (a minimum and maximum value expected), e.g. c(0,100)
[update](TRUE/FALSE) if TRUE and you specify an existing field, the existing field will be updated rather than generating an error message (default=FALSE); warning: this option will result in overwriting of existing data and is therefore potentially dangerous.
[where]the selection statement that will be applied to the feature data source to identify a subset of features to process (see full Help documentation for further details)


addcodedfield(in=”c:datapnts.shp”, field=”NEWCONST”, fieldtype=”SHORT”, constant=100);

addcodedfield(in=”c:datapnts.shp”, field=”MARKER”, fieldtype=”STRING”, constant=”SIGNS”);

addcodedfield(in=”c:datapnts.shp”, field=”MyUID”, fieldtype=”LONG”, sequence=c(1000, 1));

addcodedfield(in=”c:datapnts.shp”, field=”MyGROUP”, fieldtype=”LONG”, repeat=c(1, 2, 3, 4, 5));

addcodedfield(in=”c:datapnts.shp”, field=”RAMP”, fieldtype=”DOUBLE”, sequence=c(0, 0.01))

addcodedfield(in=”c:datapnts.shp”, field=”NORMVALS”, fieldtype=”DOUBLE”, normal=c(100, 3.6));

addcodedfield(in=”c:datapnts.shp”, field=”UNIFVALS”, fieldtype=”DOUBLE”, uniform=c(0, 100), where=”COUNTY=’WOOD’ AND MONTH=7″, update=TRUE);


Please consider making a purchase to support the continued development of these tools  Read more...

Tips on how to use this interface efficiently 


Open Source GIS

Copyright © 2001-2014 Hawthorne L. Beyer, Ph.D., Spatial Ecology LLC    Connect on LinkedIn