Python Scripted Parameters in FME

Liz Sanderson
Liz Sanderson
  • Updated

FME Version

Introduction

This article is part 3 of a 5 part series to help you get started with basic Python and FME. This article is about using the scripted parameter functionality within Workbench and includes a walkthrough of two example applications of a scripted parameter.
 

Scripted parameters are extremely useful when we want to set a parameter in FME based on something we derived or calculated from another parameter or parameters. For example, you may want users to select themes or groups of layers and have your script set the individual feature types to read within these groups. For Python scripts, a number of FME variables are available within the fme module, one of which returns a dictionary of FME parameters and their values. For more information, please review the Python and FME Basics introduction article.

Please note scripted parameters are executed before both Python startup scripts and the translation.

 

Examples

Example 1: Scripting Feature Types to Read

Attached workspace: ScriptedParameter1.fmwt

ScriptedParameter1.fmwt is a good example of scripting the feature types to read parameters based on a group selected by the user. The workspace template, ScriptedParameter1.fmwt, is included within the ScriptedParameter-Workspaces-2021.zip file (see Files section above).

This workspace will selectively read feature types with a scripted parameter

This workspace will selectively read feature types with a scripted parameter.


Ensure Prompt for User Parameters option is enabled before running the workspace to see the published parameters. A Translation Parameters window will appear. Click on the ellipsis in the window to bring up the Select ‘Layers’ Items window. Notice that the layers you can choose from are groups:

  • Walking and Biking
  • All Methods
  • Rapid Transit

Feature types to read are selected by the user through a published user parameter

Feature types to read are selected by the user through a published user parameter.


Choose Walking and Biking and run the workspace. Notice the workspace reads both the Bikeways and the PublicStreets feature types.

scripted3.png

 

The Bikeways and PublicStreets feature types are read after selecting 'Walking and Biking' as the parameter.


Let’s look at the scripted parameter used to tell FME that choosing Walking and Biking should return both of these feature types to the Feature Types to Read parameter. In the Navigator pane expand User Parameters > Private Parameters and you will see a Python scripted parameter called feature_types_to_read. Notice that this parameter is linked to the actual Feature Types to Read parameter of the reader in the workspace. Whatever value the script returns will be used by the AUTOCAD reader to determine which feature type to read.

The Feature Types to Read parameter is linked to the private scripted parameter, 'feature_types_to_read'

The Feature Types to Read parameter is linked to the private scripted parameter, 'feature_types_to_read'.


Go back to the scripted parameter, double click on it, and click the ellipsis to open the editor. Here is the script used inside:

import fme
featureTypes = ''
if fme.macroValues['layers'].find('Walking and Biking') != -1:
    featureTypes += 'PublicStreets Bikeways '
if fme.macroValues['layers'].find('Rapid Transit') != -1:
    featureTypes += 'RapidTransitLine RapidTransitStations '
if fme.macroValues['layers'].find('All Methods') != -1:
    featureTypes += 'PublicStreets Bikeways RapidTransitLine RapidTransitStations '
#Debug
#print(featureTypes)
return featureTypes

You can see that we have a series of if statements that finds out which layer the user chose using the fme.macroValues[ ] dictionary and then sets the value of featureTypes which will be returned. The last line is the most important as this is where we "return" the actual value of the scripted parameter with the statement:

return featureTypes

This return statement must always exist in a Python scripted parameter because it is here that the parameter value is given to FME. Also, notice the commented out print function. You can use print functions to help you debug your scripts by returning variable values to the Translation Log pane. For more on logging with Python see this article:

Logging with Python Scripts
 

Example 2: Unique Output Dataset Names with Scripted Parameters

Attached workspace: ScriptedParameter2.fmwt

When writing files, you may want to ensure that your output is uniquely named. One way of doing so is to use a scripted parameter to append the date and time to the output dataset name. The attached workspace ScriptedParameter2.fmwt accepts a published parameter for the output dataset name and a private scripted parameter appends a unique value (date and time) to the published parameter value. The workspace itself finds points (drinking fountains) that fall within park polygons and outputs a CSV file.

This workspace will create a uniquely named output dataset by appending the date and time of the translation of the file name

This workspace will create a uniquely named output datasets by appending the date and time of the translation of the file name.


Open the workspace and ensure that Prompt for User Parameters (found under the Run menu) is enabled. Run the workspace and the Translation Parameters window will appear. Click OK to accept the default output dataset name.

Take a look at the Translation Log pane once the workspace has finished running. Under the Features Written Summary at the end of the log, you will see the name of the output csv file written. It will be in the following format: <fileName>_<YYYYMMDD>_<HHMMSS>, where <fileName> is the value of the published parameter (test in the sample workspace), <YYYYMMDD> is the current date, and <HHMMSS> is the start time of the translation.

Create unique output dataset names by using a scripted parameter to append the date and time

Create unique output dataset names by using a scripted parameter to append the date and time.


Go to the Navigator > User Parameters > Private Parameters > nameWithTime and double-click to open the script editor. Here is the script used inside:

# Import fme module and datetime.datetime class
import fme
from datetime import datetime

# Get the value of the published parameter
OutFileName = fme.macroValues['OutFileName']

# Use the now() method to get the current date and time
# Format date as YYYYMMDD_HHMMSS with strftime method
curTime = datetime.now().strftime("%Y%m%d_%H%M%S")

# Concatenate the published parameter, an underscore, date time
OutFileName += f'_{curTime}'

# Return unique file name which becomes the value of the scripted parameter
# This scripted parameter is linked to the destination dataset name
return OutFileName

The script gets the value of the published parameter using the fme.macroValues[] dictionary and the current date and time by calling datetime.now() method. It concatenates the datetime value to the user-defined filename and returns the unique value (fileName_YYYYMMDD_HHMMSS) to the writer with the return statement. Remember, the return statement must always exist in a scripted parameter.

Note: Adding the date and time to an output file name is also possible without using scripted Python parameters (eg. by using the DateTimeNow() function that is available in the text editor).

For more information on the Date/Time functions in Workbench, please see the help documentation:

Date/Time Functions
 

Documentation

For using FME Objects in Python, you can find complete documentation of the Python FME Objects API here: FME Objects Python API


Data Attribution

The data used here originates from open data made available by the City of Vancouver, British Columbia. It contains information licensed under the Open Government License - Vancouver.

Was this article helpful?

Comments

0 comments

Please sign in to leave a comment.