4

I have Python 2.7 (installed together with ArcGIS 10.1). Via OSGeo4W I have installed QGIS and Orfeo ToolBox (OTB) applications. I was able to connect QGIS and OTB and it's working fine. Through OSGeo installer I also installed otb-python and otb-wrapping, but I have no idea, how to import otbAplication into Python while writing script.

Could you please help?

6
  • Probably not the appropriate place... But the site won't let me comment. @ Roman, did you manage to figure out this issue? I am trying to use otb in a python script in Pycharm. I added a path to the folder with otbApplication, but I still get an error that the package cannot be found. Any help would be very welcome,
    – Olivier
    Commented Mar 26, 2014 at 17:22
  • if you are having the same problem but can't comment, upvoting the question will increase its visibility.
    – Dan C
    Commented Mar 26, 2014 at 18:03
  • Thanks, and sorry (I am new on this site and I can't upvote if I don't have reputatio).
    – Olivier
    Commented Mar 26, 2014 at 18:14
  • sorry, I forgot about the rep required for voting.
    – Dan C
    Commented Mar 26, 2014 at 18:17
  • Hi, I havn't yet figure it out. In a meantime I import grass and PYQGIS to PyScripter but no luck with OTB :/
    – Roman
    Commented Mar 27, 2014 at 19:34

1 Answer 1

2

Before actually using OTB from a Python instance on your computer you have to make sure you have access to the relevant libraries. ArcGIS installs Python in a separate folder from that of OSGeo4W. So, if you are planning on using both ArcPy and OTB you must look closely at the paths and locations of your libraries.

For using OTB from within Python when library paths are in order, please refer to this example from the OTB cookbook. You can also launch OTBApplication commandlines directly, using subprocess.call in Python.

#  Example on the use of the Smoothing application 
# 

# We will use sys.argv to retrieve arguments from the command line. 
# Here, the script will accept an image file as first argument, 
# and the basename of the output files, without extension. 
from sys import argv 

# The python module providing access to OTB applications is otbApplication 
import otbApplication 

# otbApplication.Registry can tell you what application are available 
print "Available applications : " 
print str( otbApplication.Registry.GetAvailableApplications() ) 

# Let's create the application with codename "Smoothing" 
app = otbApplication.Registry.CreateApplication("Smoothing") 

# We print the keys of all its parameter 
print app.GetParametersKeys() 

# First, we set the input image filename 
app.SetParameterString("in", argv[1]) 

# The smoothing algorithm can be set with the "type" parameter key 
# and can take 3 values : 'mean', 'gaussian', 'anidif' 
for type in ['mean', 'gaussian', 'anidif']: 

  print 'Running with ' + type + ' smoothing type' 

  # Here we configure the smoothing algorithm 
  app.SetParameterString("type", type) 

  # Set the output filename, using the algorithm to differenciate the outputs 
  app.SetParameterString("out", argv[2] + type + ".tif") 

  # This will execute the application and save the output file 
  app.ExecuteAndWriteOutput()
4
  • Thanks, but my python, located in c:\Python27 can't see otb module located probably in c:\OSGeo4W\...
    – Roman
    Commented Mar 21, 2014 at 9:38
  • when I'm putting "import otbApplication" that error appears: Traceback (most recent call last): File "<interactive input>", line 1, in <module> File "C:\OSGeo4W\apps\orfeotoolbox\python\otbApplication.py", line 25, in <module> _otbApplication = swig_import_helper() File "C:\OSGeo4W\apps\orfeotoolbox\python\otbApplication.py", line 24, in swig_import_helper return _mod UnboundLocalError: local variable '_mod' referenced before assignment
    – Roman
    Commented Mar 21, 2014 at 10:16
  • A manage to do that: when I run OSGeo4W Shell and then type: python' -> import otbApplication everything work. But I cannot import arcpy (because it's in other python installation I think). And when I start python from c:\python27 than system cannot see otbApplication but see arcpy....
    – Roman
    Commented Mar 21, 2014 at 11:32
  • this need to be fixed in the registry. Possible but not straightforward. I don't remember how I did it, sorry.
    – radouxju
    Commented Mar 21, 2014 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.