CALC_ATTR

The CALC_ATTR command allows you to calculate a new attribute value (or update the value for an existing attribute) for features in a layer based on a source attribute (including things like the feature label or type) and a second value. The second value can be a specified string or number, or the value from another attribute of the feature.

The following parameters are supported by the command:

  • FILENAME - filename of the layer to update. If an empty value is passed in, all loaded vector layers will be updated. This parameter can be listed more than once to specify multiple input files, like FILENAME="FILENAME_1" FILENAME="FILENAME_2". When running the script in the context of the main map view (including loading a workspace) you can also pass in the value 'USER CREATED FEATURES' to have the 'User Created Features' layer updated or 'SELECTED LAYERS' to have any layers selected in the Control Center If you don't pass anything in all vector layers will be operated on.
  • TYPE - specifies what type of operation to use when assigning the new attribute value.
    • COPY - copies the source attribute value into the new attribute
    • ADD - numerically adds the second value to the source value and saves the result into the new attribute
    • SUBTRACT - numerically subtracts the second value from the source value and saves the result into the new attribute
    • MULTIPLY - numerically multiplies the second value by the source value and saves the result into the new attribute
    • DIVIDE - numerically divides the source value by the the second value and saves the result into the new attribute
    • APPEND - appends the second value (as a string) to the source value and saves the result into the new attribute. The SEP_STR parameter defined below is used to separate the second value from the source.
    • PREPEND - prepends the second value (as a string) to the source value and saves the result into the new attribute The SEP_STR parameter defined below is used to separate the second value from the source.
  • NEW_ATTR - specifies the attribute value to create or update. See special Attribute Name parameter details.
  • SOURCE_ATTR - specifies the attribute value to start with when creating the new attribute. See special Attribute Name parameter details.
  • VALUE_ATTR - specifies the attribute value to use as the 2nd value of the calculation. See special Attribute Name parameter details.
  • VALUE - specifies the value to use as the 2nd value of the calculation. For numeric operations this must be a number.
  • SEP_STR - specifies the string to use to separate the source attribute and 2nd value when appending or prepending text. If not provided the default is no separator at all.

SAMPLE

Here is a sample of creating a new elevation attribute in feet from an elevation attribute (ELEV_M) in meters, including with an appended unit string.

GLOBAL_MAPPER_SCRIPT VERSION=1.00
// Create new ELEV_FT attribute with attribute in feet in any loaded layers
CALC_ATTR TYPE="MULTIPLY" NEW_ATTR="ELEV_FT" SOURCE_ATTR="ELEV_MT" VALUE="3.2808"
// Append the unit name to the new attribute
CALC_ATTR TYPE="APPEND" NEW_ATTR="ELEV_FT" SOURCE_ATTR="ELEV_FT" VALUE=" ft"