Vol 1: HOW TO INCLUDE PYTHON IN YOUR CUBE APPLICATION MANAGER WORKFLOW

pythonCube Base Application Manager uses a flow-chart system for designing, coding, documenting and running your models. Input and output data are shown in the context of the model and can be immediately viewed or edited by double-clicking on them. Application Manager makes it easy to present a model in a clear way to others. Features include:

  • A pull-down menu to choose model functions. Each model step has input data on the left, output data on the right.
  • File Linkage (networks, zonal data, etc.) and file creations for intermediate steps, then drag and link data from one model step to another.
  • A complete data editing and visualization system.
  • An easy to use interface to run part of the model or the whole process.

Picture1

Application Manager and clear “flow chart” approach (user defined)

Not only the Cube Modules/Programs can be included in the flow-chart workflow, but also any customized User Program. Indeed, Application Manager can be used as a “shell” to sequence and execute runs involving any type of program.

Common uses of this functionality include integration of third-party or custom-programmed demand modeling routines. Programs like R-software or BIOGEME can be included to undertake model parameters estimation within the Cube environment.

User Programs are located on the Cube Program ribbon tab along with other modules.

cube-ribbon

Adding User Programs in Cube

 

User programs can be EXE, BAT, or DLL files If not one of these types, a BAT wrapper can be used to call files.

Python software can be integrated in the Cube workflow, to exploit its powerful programming language and comprise geoprocessing functionalities by including the ArcPy library. The program can be linked dynamically (inputs/outputs, parameters, etc.) to the other programs in your application.

 

HERE’S HOW:

Python is open source and included with ArcGIS/ArcGIS Engine installation.

Python scripts can be developed from scratch, with a very intuitive scripting language. Moreover, Model Builder in ArcGIS provides a very useful tool to develop geoprocessing scripts, that can be further adapted and easily included in Cube.

In the example below, in converting some GTFS data to Cube format for the Perth City using Cube Voyager capabilities, we used Python geoprocessing to undertake a Spatial Join between the PT nodes included in the Shape.txt file and the PT stops included in the Stop.txt file.

To show how the process could be built in ArcGIS, we show here how to start using Model Builder, exporting the Python script and adapt it to be used in Cube.

With Model Builder in ArcGIS we can build the model very easily, as shown in the example below.

model

 

The model script can be quickly exported to Python script as shown below (Model > Export > To Python Script…).

pythonscript

 

A Python .PY file is then created, containing the script as it is executed through Model Builder.

 

script

 

The script can be refined if needed, but the only necessary edits are the two steps below:

  • Add the import sys statement, to import the sys module
  • Change the definition of inputs and outputs using the argv function: defining the list of command line arguments passed to the Python script

Some further edits have been made in this example:

  • The script to allow overwriting of the outputs has been included as
    from arcpy import env
    env.overwriteOutput = True
  • The try – except – finally commands have been included as below (error handling in Python can be done through the use of exceptions that are caught in try blocks and handled in except blocks)
  • Run messages are written in the text file “printfile

 

script2

 

To add the new user program (Python in the example) in the Cube environment, follow the simple steps below:

  1. First of all, move the python script (py in the example) to the proper location in your catalog folder (e.g. {Catalog_Dir}/Input/Python).
  2. Click on “User Programs > User Programs Settings…” to edit existing programs or add anew program.program_settings
  3. The User Program Settings window contains the full list of your User Programs, Click “Add…” to add the new User Program.
  4. The Python.exe program can be included in Cube directly as an executable (Pythonw.exe if we do not want to pop-up the command prompt when Python is executing). Specify the Program as below, browsing to the Python.exe file location, depending on your installation.add-editAs you can observe in the picture, we are going to pass the information needed to Python on the Command Line.
  5. Then we need to proceed in defining the remaining components of the Program Interface. We define Input and Output Files as below (with the Script File and allowing 10 inputs and 10 outputs data file, as an example).add-edit2
    Note that Input files defined as text (rather than binary) can indicate whether they may contain catalog keys.

    add-edit3

    add-edit4

  6. Define the Command Line Arguments (sys.argv[#] in the Python script), considering the inputs and outputs, as belowadd-new
  7. When clicking “OK” and going back to the Application Manager, we can now include Python by selecting “User Programs > PYTHON”, and this will be an empty box, ready for us to fill in with the necessary script file, inputs and outputs.In our example the inputs are the PT nodes and PT stops DBF file (dynamically linked from the outputs from other conversion steps). The outputs are the prn file containing the run report and the stop nodes feature class contained in the GTFS.gdb, with the result of the spatial join.script
  8. The input Script File can be now edited directly within the Cube Text Editor, by double clicking on it within the Application Manager.

In our example, the stop nodes are joined to the closest PT node, as it can be observed in the picture below.

stopnodes

 

The User Program definition made via the wizard is stored in an Application Manager Resource (.RSC) file. Starting from Cube 6.4 (to avoid issues with missing user rights) the file is written to

 “C:\Users\<username>\AppData\Roaming\Citilabs\Cube\User”

To transfer the program to another machine, the RSC file needs to be copied to the equivalent directory on the target machine. The only settings that you might need to change are the executable name and run path.

It is also possible, through the Pilot program, to copy the necessary RSC file automatically in the right folder, if storing it within the catalog directory.

For more information about this topic:

Please visit our Citilabs Community Forum. Here you will find several other interesting topics to help you improve your Cube models and the opportunity to interact with other Cube users.

Support Community


FILIPPO CONTIERO
SENIOR TRANSPORT MODELLER

Filippo is an experienced and passionate transport modeler based in München, Germany. As Senior Transport Modeller at Citilabs, he assists users in optimizing their use of Cube based on their individual needs and goals. In addition, he oversees Cube user support, training courses and “one-to-one” coaching. Prior to Citilabs, he worked as a consultant and transport modeller over a number of different projects, developing transportation models for highway and multimodal studies. He gained a Master Degree in Civil Engineering from Padova University (Italy) and an MSc in Transport Planning and Engineering from the ITS at Leeds University (UK).