TransWikia.com

Use command line to call an FME workbench from a python tool in ArcGIS

Geographic Information Systems Asked by whatahitson on July 28, 2021

I have a Python script that I run in ArcGIS Desktop 10.2. I also have an FME workbench I use in part of the same process. I currently have to run half of the script, switch to FME then run the other half. I would like my script to call the FMW.

At present I’m trying:

cmd = '"C:Program FilesFME201864bitfme.exe" "W:EntityABZOperations & GeosciencesGSRGIS Data_Common-Data_ResourcesToolsMigMapCountourFiller.fmw" --SourceDataset_ESRISHAPE "\main.glb.corp.localDataEPEMEAGBGroupDataGISExploNewVenturesOpportunitiesWinterton2018Migration2018-02-14_CarbMigFilescontours_l_1m.shp" --DestDataset_ESRISHAPE "\main.glb.corp.localDataEPEMEAGBGroupDataGISExploNewVenturesOpportunitiesWinterton2018Migration2018-02-14_CarbMigFiles"'
os.system(cmd)

this is returning:

'C:Program' is not recognised as an internal or external command, operable grogram or batch file. The system cannot find the path specialised.

I can see that the problem is the space between program and files but I dont understand why its ignoring my quotes.

I see there are multiple ways of accessing command line from python and that there is also FMEobjects but I haven’t managed to get them to work. Am I trying the right thing but getting it wrong or should I be using a completely different method.

3 Answers

Managing all arguments in a single string is a debugging hell.

You would be better off using subprocess and having your arguments in a tidy list structure.

import os
import subprocess

args = [r"C:FMEfme.exe", "version"]
subprocess.Popen(args)

Output:

FME 2018.0.0.0 (20180308 - Build 18284 - WIN64)

Your arguments would obviously be different. In my case, I just want to make sure the fme.exe is called with a single argument.

As Mark pointed out, your other option is to use fmeobjects which would give you ability to call your workbenches. The minimal working code:

import sys
sys.path.append(r'C:FMEfmeobjectspython27')

#----------------------------------------------------------------------
def buffer_single_shapefile_diff_param_values():  
    try:
        wkspc_path = r"C:FMEWorkbenchesBufferForSameShapefile.fmw"

        wkspc = fmeobjects.FMEWorkspaceRunner()
        for dist in xrange(100,200,25):
            wkspc.runWithParameters(wkspc_path,
                                    {"Output_buffered_name":"Buffered_{0}m".format(dist),
                                     "OFFSET":"{0}".format(dist)})

    except fmeobjects.FMEException, err:
        print "FMEException: %s" % err
        sys.exit(1)

buffer_single_shapefile_diff_param_values()

Correct answer by Alex Tereshenkov on July 28, 2021

Although what you are trying should work (I don't have enough Python knowledge to debug quotes) I think the better solution is to use FME Objects.

Check out the documentation for the FMEObjects API. In particular you're looking for a method called fmeobjects.FMEWorkspaceRunner and FMEWorkspaceRunner.run(workspace)

You say you couldn't get FME Objects to work. Was there a particular issue or error? Were you using this method or something else?

Answered by Mark Ireland on July 28, 2021

If you scroll up to the top of the log there is an example batch command for the script:

cmd = [r"C:Program FilesFMEfme.exe", r"D:teararoanzscriptET_Node.fmw", r"--SourceDataset_GEODATABASE_FILE d:teararoanzcurrent.gdb", r"--DestDataset_GEODATABASE_FILE d:teararoanzcurrent.gdb", r"--DestDataset_GEODATABASE_FILE_4 d:teararoanzcurrent.gdb"]

Which I have converted to a python list and escaped the backslashes. Then use the python module subprocess as above - easy

import subprocess, os
result = subprocess.Popen(cmd)
if not result.returncode:
    print("Failed!", result)
    os.startfile(r"D:teararoanzscriptET_Node.log")
else:
    print"hooray!")

The trouble is fme.exe always returns a non-zero result when you use subprocess.call() Even if the log says it was successful with no warnings. How can I get a proper success/fail flag? Answering my own question: use result = subprocess.Popen(cmd) and then test NOT result.returncode for success The reason I do this at all is because I am mixing ArcMap (32 bit) and FME 64 bit in one workflow. I don't even install fme32 now.

Answered by KimO on July 28, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP