TclCaller
Runs a Tool Command Language (Tcl) command and assigns its return value to an attribute.
The Tcl command can operate on the feature’s geometry and/or attributes, using any of the built-in Tcl functions provided by the Tcl language, as well as any of the FME-provided Tcl facilities.
See the Tcl language reference manual (www.tcl.tk) and the FME Tcl Variables and Functions section below for details of the capabilities.
Common Tcl examples usage include:
- Trim spaces from an attribute:
FME_SetAttribute trimmedAttribute [string trim [FME_GetAttribute originalAttribute]]
- Replace all non-numeric characters with spaces in an attribute:
FME_SetAttribute anAttribute [regsub -all {[^0-9]} [FME_GetAttribute anAttribute] {}]
Note that in this case, the return value is the number of substitutions actually made.
- Match a regular expression against an attribute:
regexp {^[A-Za-z]$} [FME_GetAttribute anAttribute]
This regular expression tests if the entire value of the attribute consists only of alphabetic characters.
Note that when matching regular expressions, the return value will be 1 if the expression matched, and 0 otherwise.
- Log a custom message to the log window:
FME_LogMessage fme_inform {This is my message}
The recommended way to manipulate feature attributes is through the functions that are provided for this purpose:
FME_GetAttribute attrName
FME_SetAttribute attrName newVal
FME_CopyAttribute destAttrName srcAttrName
FME_RenameAttribute destAttrName srcAttrName
FME_UnsetAttributes attrName1 [attrName2 attrName3 ...]
Parameters
Output Feature Parameters
The Tcl expression which will be evaluated for each feature. The value of the expression will be stored in the Destination Attribute.
Note: You can access published parameters via the global $FME_MacroValues array. If FME returns the error Can't read "FME_MacroValues(text_param)": no such variable, replace $FME_MacroValues with $::FME_MacroValues.
Additional code may also be provided in the Source Code editor. This code will be compiled once at the beginning of the translation and so offers a more efficient option than providing extensive code in the Tcl Expression parameter.
A Tcl source file can optionally be provided. The file will be read before the Tcl command is executed. This can be used to reference Tcl functions kept in a common external file.
The attribute which will store the result of the Tcl Expression parameter after it is evaluated. If the Tcl Expression does not return a value, the destination attribute can be left blank.
Copy Control
It is possible to create multiple copies of a feature using the Number of Copies Tcl Expression parameter. This expression is evaluated in the context of the input feature. It returns a number to indicate how many instances of the feature are to be processed using the transformer's Tcl Expression.
If specified, the Copy Number Attribute will hold the copy number of each output feature. For example, if 3 copies of an input feature are made, the output copies will have 0, 1, and 2, respectively, assigned to their copy number attribute. The Tcl expressions can examine this attribute to determine which output feature number it is working on.
Advanced
Exposes any attributes that are created by the Tcl Expression being executed so they can be used by other transformers.
Hides any attributes that may be removed by the Tcl Expression being executed. Other transformers will not be able to use these attributes.
Hides any lists that may be removed by the Tcl Expression being executed. Other transformers will not be able to use these lists.
Note that if you select to hide a list, your selection will include any list attributes or nested lists. For example, if you select to hide a list called list{} then list{}.attr or list{}.sublist{} will also be hidden.
FME Tcl Variables and Functions
Four Tcl global variables provide a gateway between an FME feature and the Tcl script: FME_FeatureType, FME_CoordSys, FME_AttrEncoding, and FME_AttrNameEncoding variables. In order to use these in a Tcl procedure, they must be declared as global; otherwise, the variables will be considered to be local and will not affect or interact with the FME feature upon which the function is run. To use them in a function, use a pattern like:
proc doSomething {
global FME_FeatureType
global FME_CoordSys
# rest of function goes here...
}
This global variable defines the name of the character encoding in which Tcl assumes a feature's attributes are specified when working with FME_GetAttribute and FME_SetAttribute functions. As Tcl works internally with UTF-8, this means that it will convert attribute values from the given encoding to UTF-8 when getting the value of attribute with FME_GetAttribute function, and will convert values from UTF-8 to the given encoding when setting the attribute value using FME_SetAttribute function.
The value of this variable does not impact the setting or retrieval of attributes when character encoding is known, either on the retrieved attribute or as directed by the arguments to FME_GetAttribute or FME_SetAttribute.
The default attribute encoding is the same as the default character encoding of the system on which FME is executing, and might be overridden by Tcl's built-in "encoding system" command. A complete list of supported encodings may be obtained by executing the command "encoding names".
The variable may be set, read, or unset:
set FME_AttrEncoding iso8859-2
puts $FME_AttrEncoding
unset FME_AttrEncoding
If FME_AttrEncoding is unset, or is set to be an empty string, it reverts to the default system encoding.
This function is used to determine if an attribute is present on a feature.
The syntax for FME_AttributeExists is:
FME_AttributeExists <attributeName>
<attributeName> is the name of the attribute whose value is to be retrieved.
If the attribute was present on the feature, the value 1 is returned; otherwise, 0 is returned.
This function is used to determine if the value of an attribute is null.
The syntax for FME_IsAttributeNull is:
FME_IsAttributeNull <attributeName>
<attributeName> is the name of the attribute whose value is to be checked.
If the attribute has a null value, the value 1 is returned; otherwise, 0 is returned.
This function is used to retrieve the names of all feature attributes and return them as a Tcl list.
The syntax for FME_AttributeNames is:
FME_AttributeNames
This global variable defines the name of the character encoding in which Tcl assumes a feature's attributes' names are specified when working with FME_GetAttribute and FME_SetAttribute functions. As Tcl works internally with UTF-8, this means that it will convert attribute names from UTF-8 to the given encoding system, and use the converted name when referring to the actual feature's attributes.
The default attribute encoding is the same as the default character encoding of the system on which FME is executing, and might be overridden by Tcl's built-in "encoding system" command. A complete list of supported encodings may be obtained by executing the command "encoding names".
The variable may be set, read, or unset:
set FME_AttrNameEncoding iso8859-2
puts $FME_AttrNameEncoding
unset FME_AttrNameEncoding
If FME_AttrNameEncoding is unset, or is set to be an empty string, it reverts to the default system encoding.
This global variable is set up to mirror the coordinate system of the feature. As with the FME_FeatureType variable, it can be set, read, and unset:
set FME_CoordSys UTM10-83
puts $FME_CoordSys
unset FME_CoordSys
In any Tcl procedure invoked by TclCaller, the FME_Coordinates function can be used to read and write the coordinates of the feature. It provides several options, as listed below:
dimension (2|3)
Sets the dimension of the feature - either 2 or 3.
numCoords
Returns the number of coordinates in the feature.
resetCoords
Removes all the current coordinates from the feature.
Note: Calling resetCoords has no effect on the fme_geometry or fme_type attributes. These must be reset or readjusted explicitly by the script so the feature is not left in an inconsistent state.
getCoord (x|y|z) <index>
Returns the coordinate value for the specified axis at the specified <index>.
<index> ranges from zero to one less than the number of coordinates.
addCoord <xvalue> <yvalue> [<zvalue>]
Adds the coordinate specified to the end of the feature's geometry.
geomType
Returns the geometry type of the feature - it will be one of the values allowed for the FME Geometry Model attributes:
fme_point
fme_line
fme_polygon
fme_donut
fme_aggregate
fme_undefined
geomType <type>
Sets the geometry type of the feature to the passed-in value. No sanity checking is done. It is important that this be done with care since setting an incorrect geometry type can later hinder FME.
This function is used to copy a feature attribute. The attribute's original internal data storage type is preserved by using this function. This can be significant when working with high precision floating point values.
The syntax for FME_CopyAttribute is:
FME_CopyAttribute <newName> <oldName>
If the feature did not contain an attribute with the name oldName, then the function does nothing. It will not create a new blank valued attribute.
This function is used to decode character strings which have been encoded as described in the section Substituting Strings in Mapping Files. The result of the function is a copy of the input parameter, with substituted string sequences converted back to the characters they represent.
The syntax for FME_DecodeText is:
FME_DecodeText <encodedText>
<encodedText> specifies a string which possibly contains substituted character string sequences.
This function is similar to FME_DecodeText, except that its argument may be interpreted as a reference to an attribute value rather than as a string to decode.
The syntax for FME_DecodeTextOrAttr is:
FME_DecodeTextOrAttr [&]<encodedText>
<encodedText> specifies a string which possibly contains substituted character strings.
If the argument begins with an ampersand character ("&"), the function returns the value in the attribute named by decoding the remainder of the argument. Otherwise, the function returns the decoded equivalent of <encodedText>.
This function is used to generate character strings which have are encoded as described in the section Substituting Strings in Mapping Files. The result of the function is a copy of the input parameter, with special characters converted to substituted string sequences which pass cleanly through FME's file parser, and can be represented in any ANSI character set.
The syntax for FME_EncodeText is:
FME_EncodeText <unencodedText>
<unencodedText> specifies the string which is to be encoded. Any special characters in this string will be replaced with the substitution sequences listed in the referenced section Substituting Strings in Mapping Files.
This function is used to call any FME function from within a Tcl procedure. This allows standard FME functions to be called iteratively on the same feature.
The syntax for FME_Execute is:
FME_Execute <functionName> [<arg1> ... <argN>]+
<functionName> is the name of any FME function, without the leading @ sign.
Any remaining arguments are passed to the function. For example, the @Generalize function is invoked like this:
FME_Execute Generalize Douglas 10
and this would accomplish the same effect as if @Generalize(Douglas,10) were invoked elsewhere in the mapping file.
If the function that is invoked causes the feature to be deleted, the translation will be aborted. The @Generalize function may do this if the feature's total length is less than the tolerance value passed in.
This global variable is set up to mirror the feature type of the feature. As with the other variables, it can be set, read, and unset:
set FME_FeatureType "Roadster"
puts $FME_FeatureType
This function can be used to get a value of attribute on the feature from within a Tcl procedure.
The syntax for FME_GetAttribute is:
FME_GetAttribute <attributeName> [<attributeEncoding>]
<attributeName> is the name of the attribute whose value is to be retrieved.
<attributeEncoding> is an optional character encoding used when interpreting the bytes of an existing attribute's value. If this is not specified, the bytes of an encoded attribute are interpreted according to their original encoding, and the bytes of a plain string attribute are interpreted according to the value of the FME_AttributeEncoding global variable.
If the attribute was not present on the feature, an empty string is returned as the value.
The FME_LogMessage function is used to write messages to the FME log file. It may be invoked in one of two ways:
FME_LogMessage <severity> <messageNumber> [<arg1> ... <argN>]+
or
FME_LogMessage <severity> <message>
<severity> can have one of these values: fme_inform, fme_warn, fme_error, fme_fatal, fme_statistic, and fme_statusreport
When the first form is used, the message number must be present in a file in the messages subfolder under the FME installation folder. The remaining parameters are used to fill in any %0, %1, ... %n parameter holders in the message. For example, if the message was:
3011, Opening file %0 for mode %1
then FME_LogMessage could be called like this:
FME_LogMessage fme_inform 3011 /tmp/cacher.txt read
In the second form, the message is output directly to the log file.
This function is used to rename a feature attribute. The attribute's original internal data storage type is preserved by using this function. This can be significant when working with high precision floating point values.
The syntax for FME_RenameAttribute is:
FME_RenameAttribute <newName> <oldName>
If the feature did not contain an attribute with the name oldName, then the function does nothing. It will not create a new blank valued attribute.
This function is used to set the value of an attribute on a feature.
The syntax for FME_SetAttribute is:
FME_SetAttribute [-notranscode] <attributeName> <attrValue> [<attributeEncoding>]
<attributeName> is the name of the attribute whose value would be set to <attrValue>.
<attributeEncoding> is an optional character encoding to be applied to the resulting attribute. It overrides the effect of any value currently stored in the FME_AttributeEncoding global variable.
If the -notranscode option is specified, the bytes of the Tcl variable are copied verbatim to the resulting attribute, in place of a character encoding conversion from Tcl's internal UTF-8 encoding to the target character encoding. This is useful when the Tcl variable is known to contain an array of bytes in a particular character encoding instead of the usual sequence of UTF-8 characters.
The resulting attribute will normally have attached character encoding information. If the <attributeEncoding> argument is not specified, the attribute will default to a UTF-8 encoding unless the FME_AttributeEncoding global variable is defined. If this variable is defined, the resulting attribute will be an unencoded (plain string) attribute, with bytes representing the value's characters in the named encoding.
This function is used to set the value of an attribute on a feature to null. If the attribute does not exist, it will be created.
The syntax for FME_SetAttributeNull is:
FME_SetAttributeNull <attributeName>
<attributeName> is the name of the attribute whose value would be set to null.
If the attribute does not exist, it will be created and its type will be string.
This function generates a temporary filename in the FME temporary folder.
The filename is guaranteed to be a unique, new file.
Note: FME will create an empty file with the given name; you must delete it when you are done.
The syntax for FME_TempFilename is:
FME_TempFilename [<prefix>] [<suffix>]
If a prefix and suffix are not provided, the filename will be returned as an arbitrarily uniquely named file in the FME temporary folder. (For information on where this folder is located, see Temporary Folder Determination.)
If a prefix is provided, then that is used as the beginning portion of the filename within the temporary folder, and the suffix is used as the ending portion. Typically the suffix is used to append an extension, and in this case, it would have to additionally include a period "."
For example, this call:
FME_TempFilename raster .png
would return something like:
c:/Documents and Settings/username/Local Settings/Temp/rastera05921.png
This function is used to remove one or more attributes from a feature.
The syntax for FME_UnsetAttributes is:
FME_UnsetAttributes <attr1> [<attr2> <attr3 ...]
No error will be generated if the feature did not contain any of the attributes listed. FME_UnsetAttributes simply ignores any argument that does not specify an attribute on the feature.
Usage Notes
% Character
Due to FME parser limitations, a Tcl expression cannot contain a percent (%) character. If a % character is needed, the expression should be coded as a Tcl procedure and put into an external file to be 'source'd in. Note that you can avoid the % character issue by using a StringFormatter transformer, which provides a convenient way to access the Tcl 'format' command.
Editing Transformer Parameters
Using a set of menu options, transformer parameters can be assigned by referencing other elements in the workspace. More advanced functions, such as an advanced editor and an arithmetic editor, are also available in some transformers. To access a menu of these options, click beside the applicable parameter. For more information, see Transformer Parameter Menu Options.
Transformer Categories
Technical History
Associated FME function or factory: @Tcl2
Search FME Community
Search for samples and information about this transformer on the FME Community.