span8
span4
span8
span4
Adding a python startup script to your workspace is a great way to extend the functionality of workbench. Although we encourage you to make use of FME’s transformers wherever possible, sometimes it is necessary take advantage of the greater control that a custom script provides. If you don’t have a lot of prior programming experience python is a great language to start with as its simple clean syntax makes it is easy to use and understand. Python has also been chosen as the default scripting language for ArcGIS; If you have a valid ArcGIS license on your machine you can make use of the many geoprocessing functions in the arcpy module in your python scripts, you can also use ModelBuilder to create python scripts from your models.
In this example, we will be creating a python startup script to do a little bit of geodatabase cleanup before our workspace runs. The dataset we are using is a file geodatabase that contains an existing polygon feature class linked via a relationship class to an annotation feature class. The issue arises when we try to overwrite the polygon class; the truncate table setting on the feature type parameters will only truncate the table of polygon features, the corresponding annotation features will not be deleted as desired. In order for our workspace to run correctly we must ensure that both tables are properly emptied before our workspace executes so that new features can be written and properly linked. To do this, we will call the Delete Rows method in the arcpy python module from a python startup script to drop all features from both tables before the workspace is run.
First, we need to create a python script.. We add this tool to a blank model and set its single parameter to the name of the table we wish to clear. We do this for both our polygon feature class and our annotation feature class. We can then export our model to a python script by choosing Export>To Python Script from the Model menu and specifying a file location as shown below:
We can then examine the contents of our script in a text editor such as notepad. The contents should look something like this:
# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# samp.py
# Created on: 2012-11-05 10:44:15.00000
# (generated by ArcGIS/ModelBuilder)
# Description:
# ---------------------------------------------------------------------------
# Import arcpy module
import arcpy
# Local variables:
Parcels = "C:\\FMEData\\Resources\\Esri\\CityData.gdb\\PropertyData\\Parcels"
Parcel_Ids = "C:\\FMEData\\Resources\\Esri\\CityData.gdb\\PropertyData\\Parcel_Ids"
# Process: Delete Rows
arcpy.DeleteRows_management(Parcels)
# Process: Delete Rows (2)
arcpy.DeleteRows_management(Parcel_Ids)
This script imports the arcpy module, assigns the feature class locations we specified in ModelBuilder to variables, and calls the Delete Rows geoprocessing tool.
Workbench allows us to add a python script to be run at startup before the main workspace executes, or at shutdown after your workspace has finished running (successfully or not), by adding a script as a workspace parameter. To set this parameter, we locate the workspace parameters entry on the navigator window and then expand the advanced parameters as shown below:
We then double click on 'Startup Python Script'. This opens up a simple text editor, where we can paste in the contents of our python script. Select and copy the contents of the exported python script from notepad or the text editor of your choosing and paste into the Startup Python Script editor. Then click OK. Now your python script should be all set to run before your workspace.
You can also use the PythonCaller transformer to call your python and arcpy scripts. In the examples described here, you could use the PythonCaller in conjunction with the FeatureReader instead of the Startup Python Script. Similarly, a FeatureWriter + PythonCaller could replace the Shutdown script.
Processing FME features in your arcpy script is a little trickier and is discussed in the article Using Arcpy for FME Feature Processing.
If you receive an error message similar to the following when you attempt to run your workspace:
Python Exception <ImportError>: No module named arcpy
It is most likely because the arcpy libraries have not been added to the path environment variable. To fix this, locate the Desktop10.1.pth file found under C:\Python27\ArcGIS10.1\Lib\site-packages, and add the file locations specified in its contents to the 'Path' Environment variable. Depending on your environment, you may also need to add C:\Python27\ArcGIS10.1\Lib\site-packages itself to the PYTHONPATH environment variable.
If you are using ArcPro, then this article may help with your arcpy configuration
If you receive this error in FME 2013 it may be because of the issue identified in this article. The arcpy module is not compatible with FME because FME 2013 is compiled with Visual Studio 2010. Running the workspace in a build of FME 2012 or upgrading to ArcGIS 10.2 are the only workarounds.
Attached are two sample workspaces
ArcpyStartupExample.fmw - used in this example to delete rows from feature classes prior to running a workspace
End_Python_script.fmw - generates a spatial index on a shape file after FME builds it (This can now be done by the SHAPE writer itself as of
Create Spatial Index after loading to ArcSDE - using Load only mode
Startup and Shutdown TCL Script Examples
Published Parameter use within a Startup or Shutdown Script
Transfer Workspace File Output Using SFTP (FME Server 2015 and below)
Example Workflow using FME, Python and Oracle
Logging with Python scripts
Run an FME Workspace from Python using FMEWorkspaceRunner
Extracting a schema subset for dynamic schemas
How to log out after running a translation (Windows OS)
Pass a List of Tables or Layers to an FME Reader using Python Scripted Parameters