Using a Python Startup/Shutdown Script or PythonCaller to Perform Geoprocessing with Arcpy

Crystal Fitzpatrick
Crystal Fitzpatrick
  • Updated

FME Version

Introduction

Adding a Python startup script to your workspace is a great way to extend the functionality of FME Workbench. Although we encourage you to make use of FME’s transformers wherever possible, sometimes it is necessary to 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 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. For more information on using Python with FME, please see Tutorial: Python and FME Basics.

 

Requirements

  • FME Form
  • FME Background Geoprocessing (if using 64-bit FME)
  • Licensed Esri ArcGIS

 

Demos

Demo 1: Using a Python Startup Script

Background

In this example, we will be creating a Python startup script to clean up a geodatabase 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, and 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.

 

Creating a Python script

First, we need to create a Python script. If you are unfamiliar with writing Python scripts using Arcpy, an easy way to get started is to use ArcGIS ModelBuilder to export a model to a Python script. In this example, we’ll create a simple model that runs the Delete Rows tool from the Data Management Toolbox on two tables in our geodatabase. We’ll 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:

 

modelbuilder.jpg

 

We can then examine the contents of our script in a text editor such as notepad. The contents should look something like this:

# Import arcpy module
import arcpy

# Local variables
parcels = r"C:\Users\crystal.fitzpatrick_\Desktop\Article Updates\Python3\startup_shutdown\startup\CityData.gdb\PropertyData\Parcels"
parcel_ids = r"C:\Users\crystal.fitzpatrick_\Desktop\Article Updates\Python3\startup_shutdown\startup\CityData.gdb\PropertyData\Parcel_Ids"

# Process: Delete Rows
arcpy.management.DeleteRows(parcels)

# Process: Delete Rows (2)
arcpy.management.DeleteRows(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.

You will need to change the path to where you have the CityData.gdb saved.

Adding a Startup Script in FME Workbench

FME Workbench allows us to add a Python script to 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. Open arcpyStartupExample.fmwt in FME Workbench. Then, to set this parameter, expand Workspace Parameters in the Navigator pane and then expand Scripting. 

 

Double-click on Startup Python Script. This opens up a simple text editor, where we can paste 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 them into the Startup Python Script editor. Then click OK. Now your Python script should be all set to run before your workspace.

 

Demo 2: Using a Python Shutdown Script

In the attached Python_Shutdown_Script.fmw workspace, a Python shutdown script has been set up. This script creates spatial indexes on all Shapefiles written in the workspace. This is an older script to demonstrate this topic. This functionality can not be accomplished by using various transformers.

 

PythonCaller

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. For an example, please see the article Using Arcpy for FME Feature Processing.

 

Additional Resources

Tutorial: Python and FME Basics

Using Arcpy for FME Feature Processing

No Module Named ArcPy: Importing Esri's ArcPy for Use with FME

Was this article helpful?

Comments

0 comments

Please sign in to leave a comment.