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 FME_EncodeText Function. 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 are encoded as described below in 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 below.
Substituting Strings in Mapping Files
Occasionally, a character can cause a conflict in the mapping file parser. If this occurs, you can substitute safe character strings in many formats, functions and factories, including:
- Geodatabase writer (Attribute Definitions > subtypes, Attribute Definitions > domains)
- @Lookup
- PythonFactory
- @SearchList
- @SupplyAttributes
- @Tcl2
Character Substitutions
The encoding method is inspired by XML character entities.
Note: Substitutions are made only in attribute type definitions.
Symbol |
Replaced with |
---|---|
< | <lt> |
> | <gt> |
" | <quote> |
& | <amp> |
\ | <backslash> |
/ | <solidus> |
' | <apos> |
$ | <dollar> |
@ | <at> |
s | <space> |
, | <comma> |
( | <openparen> |
) | <closeparen> |
[ | <openbracket> |
] | <closebracket> |
{ | <opencurly> |
} | <closecurly> |
; | <semicolon> |
\r | <cr> |
\n | <lf> |
\t | <tab> |
\a | <bell> |
\b | <backspace> |
\v | <verttab> |
\f | <formfeed> |
International Characters
International Character Support |
Replaced with |
---|---|
<specialCharacter> |
<uABCD> (where ABCD is the 4-digit hex value for the equivalent UTF-16 character) |
The encoding described above takes care of characters that may cause problems with mapping file parsing, but it does interfere with some "special" characters from some encodings (where special means characters which are not a part of the 7-bit ASCII set.) The problems appear in systems where one or more bytes of a multibyte character are special to traditional encoding.
To counter this, FME supports Unicode-based character encoding. Encoding of the special characters is now done as a special tag, <uXXXX>, where XXXX is the hex representation for the UTF-16 character. This very simple encoding scheme is based on one observation (assumption): any characters which will cause problems inside of the encoding will involve one or more bytes which has the high bit set. Thus, in order to traditionally encode any string containing a character where a high bit is set, we have to convert the strings to a form where the encoding system is neutralized, where we can somehow represent that character with a string of bytes containing only 7-bit values (i.e. 7-bit clean values).
The encoding process basically applies the following heuristic:
If the string contains any bytes with the high bit set:
- Convert the string to UTF-16
- Convert the string 16-bit UTF-16 characters to a 7-bit traditionally encoded string
The outcome from this is an encoded string which is 7-bit clean (i.e., has no high bits set), and which can be decoded to get exactly the original characters for the unencoded result.
When encountering the character sequence <u anywhere within an encoded string, the decoder will be able to reverse the above process after a "traditional" decoding, to get back to the original UTF-8 or system-default encoded string.
If any 16-bit value is too large for a 7-bit character, replace it with \uXXXX, where XXXX is the hex representation for the 16-bit value.
Example
According to the encoding method, the attribute type
coded_domain(TextDom:char(50):a:"the letter a":b:"the letter b")
would be represented as
coded_domain(TextDom:char<openparen>50<closeparen>:a:
<quote>the<space>letter<space>a<quote>:b:<quote>the<space>
letter<space>b<quote>)
within a mapping file. Notice that the encoding is only applied to the body of the coded_domain type, i.e. coded_domain(<body>). Similarly, it would only be applied to the body of range_domain(<body>), subtype(<body>), and subtype_codes(<body>) which are Geodatabase attribute types and to enum(<body>) and set(<body>) which are both MySQL attribute types.
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.
Defining Values
There are several ways to define a value for use in a Transformer. The simplest is to simply type in a value or string, which can include functions of various types such as attribute references, math and string functions, and workspace parameters. There are a number of tools and shortcuts that can assist in constructing values, generally available from the drop-down context menu adjacent to the value field.
Using the Text Editor
The Text Editor provides a convenient way to construct text strings (including regular expressions) from various data sources, such as attributes, parameters, and constants, where the result is used directly inside a parameter.
Using the Arithmetic Editor
The Arithmetic Editor provides a convenient way to construct math expressions from various data sources, such as attributes, parameters, and feature functions, where the result is used directly inside a parameter.
Conditional Values
Set values depending on one or more test conditions that either pass or fail.
Parameter Condition Definition Dialog
Content
Expressions and strings can include a number of functions, characters, parameters, and more.
When setting values - whether entered directly in a parameter or constructed using one of the editors - strings and expressions containing String, Math, Date/Time or FME Feature Functions will have those functions evaluated. Therefore, the names of these functions (in the form @<function_name>) should not be used as literal string values.
These functions manipulate and format strings. | |
Special Characters |
A set of control characters is available in the Text Editor. |
Math functions are available in both editors. | |
Date/Time Functions | Date and time functions are available in the Text Editor. |
These operators are available in the Arithmetic Editor. | |
These return primarily feature-specific values. | |
FME and workspace-specific parameters may be used. | |
Creating and Modifying User Parameters | Create your own editable parameters. |
Dialog Options - Tables
Transformers with table-style parameters have additional tools for populating and manipulating values.
Row Reordering
|
Enabled once you have clicked on a row item. Choices include:
|
Cut, Copy, and Paste
|
Enabled once you have clicked on a row item. Choices include:
Cut, copy, and paste may be used within a transformer, or between transformers. |
Filter
|
Start typing a string, and the matrix will only display rows matching those characters. Searches all columns. This only affects the display of attributes within the transformer - it does not alter which attributes are output. |
Import
|
Import populates the table with a set of new attributes read from a dataset. Specific application varies between transformers. |
Reset/Refresh
|
Generally resets the table to its initial state, and may provide additional options to remove invalid entries. Behavior varies between transformers. |
Note: Not all tools are available in all transformers.
FME Community
The FME Community is the place for demos, how-tos, articles, FAQs, and more. Get answers to your questions, learn from other users, and suggest, vote, and comment on new features.
Search for samples and information about this transformer on the FME Community.