Interested in FME & Python? Checkout the recording of the June 2012 Python webinar on safe.com
Python is a programming language that can be used within FME to accomplish tasks either before or after FME runs or to perform tasks within FME which are not possible with standard FME tools and transformers*. For example you can use a python script to extract source data from a zip file before FME runs or to send an email after FME runs. In this article we will look at simple examples of python scripting in the 4 main places python is used in FME:
*Important Note: There are numerous FME transformers for performing almost every task you can think of without resorting to scripting. If you are thinking about writing a python script to do something in FME you should make absolutely sure there is not an existing transformer available already. Please feel free to ask us by contacting support at www.safe.com/support.
For learning Python, there are many Python books available from publishers such as O'Reilly, etc.
Three free resources are:
FME installs its own python interpreter which is used by any scripts running python in FME. If you want FME to use your own python interpreter there is a setting in Workbench. Please see this article for more information:
Much of FME's core functionality is available within python scripts by using FME classes and methods from the FME Objects python API. For example you can get at the FME log file, FME parameters and use any of the countless FME methods such as getArea or reproject etc. (more on this later). To use FME Objects in your python script with FME 2012 and newer you will need to import FME Objects with the statement: import fmeobjects For more information on the new FME Objects python API please see the article here:
Python-related transformers have excellent Help right in FME Workbench, which also includes Help for Startup and Shutdown scripts. For using FME Objects in Python, you can find complete documentation of the Python FME Objects API here: <FME>\fmeobjects\python\apidoc\index.html
A number of FME related variables are available in FME Startup and Shutdown scripts. As of FME 2014 these variables are accessible in the fme module so you will need to include the statement import fmeto access them. For example the fme.status variable can be true or false at the end of a translation depending on the success or failure of the workspace. For a complete list of FME variables available in Startup and Shutdown Python scripts please go to Help > Workspace Basics > User Interface Reference > Workspace Navigator > Workspace Parameters > Advanced > Startup and Shutdown Python Scripts.
Prior to FME 2014 these variables were named differently and it was not necessary to import the fme module. For example the status was returned in the variable FME_Status. For these older versions of FME the FME variables are documented in the FME Quick Translator Help.
import fme SourceDataset = fme.macroValues['SourceDataset_ACAD']Please note: if using an FME version older than FME 2014 please use the syntax below:
SourceDataset = FME_MacroValues['SourceDataset_ACAD']
In the Workbench Navigator pane under Workbench Parameters - > Advanced you will see a parameter called Startup Python Script. Here you can enter some Python code that will execute before the FME translation.
Note: If you want to set or return an FME parameter this is not place - see Scripted Parameters below.
Attached workspace: StartupPython.fmw
By default when FME writes to an existing Microsoft Excel file we insert new records, but what if you want to delete the Excel file each time? (See the article http://fmepedia.safe.com/articles/How_To/Example-scripts-for-deleting-Excel-files-prior-to-writing)
We can delete the destination dataset before running FME using the following Start Up Python script (workspace attached):
OutputFile = str(FME_MacroValues['DestDataset_XLS_ADO'])FME World Tour 2013 - 1Spatial & Star-Apic (London and Leeds)
We are getting the value for the destination dataset parameter from the FME_MacroValues dictionary. Your destination dataset parameter may have another name and which you can find by right clicking on the parameter and selecting Edit User Parameter Definition where you can see the Name:
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 the individual feature types to read within these groups.
featureTypes = '' if FME_MacroValues['layers'].find('Bus Information') != -1: featureTypes += 'BusRoutes BusStops ' if FME_MacroValues['layers'].find('Road and Rail') != -1: featureTypes += 'metrorail Roads ' if FME_MacroValues['layers'].find('All Transit') != -1: featureTypes += 'metrorail Roads BusRoutes BusStops Labels ' #Debug #print featureTypes return featureTypes
If you understand the basic scripted parameter example above you should have no trouble understanding this more complex example. The attached workspace UnZipSource.fmw accepts a published parameter for a zip file and a parameter for a directory to unzip to. A scripted parameter unzips the file and returns the directory to the source dataset parameter of the reader.
The PythonCaller transformer lets you do things to features or groups of features using a Python script. Before using this transformer please be sure there is not already an FME transformer which does the task you want to do. Feel free to ask us in support (www.safe.com/support) if there is a transformer that can help you.
The PythonCaller can call a Function which you can use to process one feature at a time or a Class where you may want to do things to groups of features. In either case the EntryPoint parameter of the transformer is where you name the function or class in the script to run. The PythonCaller can use the full range of FME Objects including numerous methods and classes.
import fmeobjects import time # Template Function interface: def timestampFeature(feature): curTime = time.ctime(time.time()) feature.setAttribute("timestamp", curTime)
The FME Store includes a custom transformer called the FuzzyStringComparer which uses the Python difflib module to compare two string attributes and calculate a similarity ratio. In some ways this is a better example because it is something we cannot do with a regular FME transformer.
1. Open a blank workspace and add the FME Store transformer FuzzyStringComparer by typing it on the canvas or by browsing the FME Store transformers in the Tranformer gallery. Right-click on the transformer and select Embed which will allow you to edit the transformer and see its contents in the same workspace.
2. Once the FuzzyStringComparer is embedded right click on it to Edit and see the contents. Locate the PythonCaller transformer and edit its properties to access the script editor. Notice again we are using a function - this time to compare string attributes on a feature by feature basis. Again we are setting a new attribute which is called FuzzyStringCompare.ratio. An AttributeExposer is used later in the workflow to access the attribute we have created.
string1 = feature.getStringAttribute('FuzzyStringCompare.string1').lower()
string2 = feature.getStringAttribute('FuzzyStringCompare.string2').lower()
ratio = difflib.SequenceMatcher(None,string1,string2).ratio()
In the above example the function used to get an FME attribute value is from the now deprecated pyfme. If you are using fmeobjects the function to use would be feature.getAttribute()
import fmeobjects class MyFeatureProcessor(object):
def __init__(self): self.featureList =  self.totalArea = 0.0 def input(self,feature): self.featureList.append(feature) self.totalArea += feature.getGeometry().getArea() def close(self): for feature in self.featureList: feature.setAttribute("total_area", self.totalArea) self.pyoutput(feature)
A python script can be added to a workspace which will run when the workspace completes. For example you may want to copy files somewhere when a workspace is done, zip data, or send an email. The script runs after all of the transformers reader and writers in a workspace are finished but the script is still run a part of the FME process. You can access the FME parameter values in the same way as in the examples above using the FME_MacroValues dictionary. A shutdown python script can also access a number of global variables which FME sets such as FME_FeaturesRead, FME_FeaturesWritten and many more. For a complete list of the global variables please see the FME Help under the topic FME_END_PYTHON.
#The smtplib gives us the emailing tools import smtplib, fmeobjects #Get the FME parameter values set when workspace run to = FME_MacroValues['EmailTo'] gmail_user = FME_MacroValues['GmailUser'] gmail_pwd = FME_MacroValues['Password'] Subject = FME_MacroValues['SuccessSubject'] FailSubject = FME_MacroValues['FailSubject'] Emailfrom = FME_MacroValues['EmailFrom'] #Get number of features FME wrote FeaturesWritten = str(FME_FeaturesWritten) #If FME Fails then Change Subject #FME_Status tells if FME was successful status = FME_Status if status == 0: Subject = FailSubject Message = 'Workspace Failed' else: Message = 'Workspace Successful the following features were written: ' + FeaturesWritten #Sending the email #Using the gmail smtp server #You can edit this line to use your own email server smtpserver = smtplib.SMTP("smtp.gmail.com",587) smtpserver.ehlo() smtpserver.starttls() smtpserver.ehlo smtpserver.login(gmail_user, gmail_pwd) h eader = 'To:' + to + '\n' + 'From: ' + Emailfrom + '\n' + 'Subject:' + Subject +'\n' print header msg = header + '\n' + Message + '\n\n' smtpserver.sendmail(gmail_user, to, msg) print 'done!' smtpserver.close()
No one has followed this yet.