Sign up ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have created a python toolbox for work that has two tools inside. These tools use the toolbox template and so have a init, getParameterInfo, etc.

I want to be able to run one tool both stand alone and call it inside other tools within that toolbox. I cant seem to get the parameters correct though.

class foo(object)
    def __init__(self)
        #stuff
    def getParameterInfo(self):
        # list parameters for UI tool
    def execute(self, parameters, messages)
        print parameter[0]
        return

class bar(object)
    def __init__(self)
        #stuff
    def getParameterInfo(self):
        # list parameters for UI tool
    def execute(self, parameters, messages)
        foo("hello, world)
        return

I have tried adding a parameter to the init(self, parameter) or the foo class but I cant get it to work.

I am new to Object-Oriented Programming (OOP) and ArcGIS in general.

share|improve this question
    
You have mentioned ArcPy and ArcGIS but your code seems to be pure Python. Can you edit your question to make clear whether you are trying to write a Python toolbox or Python script tools in a standard toolbox, please? –  PolyGeo Mar 13 at 20:20
    
I corrected the question. Hopefully its more clear. Though it is mostly a python question, I can't figure out how to make the class take both parameters given from a UI and passed programmatically when used in a different script. Thanks! –  mECH Mar 14 at 18:53
1  
This link is for Python script tools in standard toolboxes and does not mention Python toolboxes but it may give you some ideas: blogs.esri.com/esri/arcgis/2011/08/04/pythontemplate I do not know if what you are trying to do is possible. –  PolyGeo Mar 15 at 1:34
1  
I have a feeling what you're trying to do won't work, but can't confirm right now. One way that should work was if you import the tool into itself using arcpy.ImportToolbox() with a path to the PYT itself, then call arcpy.foo() –  KHibma Mar 15 at 1:58

1 Answer 1

up vote 1 down vote accepted

The simplest option is to have your execute method call a function instead of doing the actual processing. This makes it easily callable by any tool.

class Foo(object)
    def __init__(self)
        #stuff
    def getParameterInfo(self):
        # list parameters for UI tool
    def execute(self, parameters, messages)
        somefunc(parameters[0].value, parameters[1].value)

class Bar(object)
    def __init__(self)
        #stuff
    def getParameterInfo(self):
        # list parameters for UI tool
    def execute(self, parameters, messages):
        somefunc(parameters[0].value, parameters[1].value)
        anotherfunc(parameters[2].value, parameters[3].value)
        return

def somefunc(arg1, arg2):
    #do something
    return

def anotherfunc(arg1, arg2):
    #do something else
    return

If you want those functions contained in the tool classes:

class Foo(object)
    def __init__(self)
        #stuff
    def getParameterInfo(self):
        # list parameters for UI tool
    def execute(self, parameters, messages)
        self.somefunc(parameters[0].value, parameters[1].value)
    def somefunc(self, arg1, arg2):
        #do something
        return

class Bar(object)
    def __init__(self)
        #stuff
    def getParameterInfo(self):
        # list parameters for UI tool
    def execute(self, parameters, messages):
        foo = Foo()
        foo.somefunc(parameters[0].value, parameters[1].value)
        self.anotherfunc(parameters[2].value, parameters[3].value)
        return
    def anotherfunc(self, arg1, arg2):
        #do something else
        return
share|improve this answer
    
Thanks! This is what I was looking for. I thought I may have to create a module and import it but this supports my needs better. –  mECH Mar 18 at 11:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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