Tuesday, 16 June 2015

arcpy - Guidelines for organizing Python Toolboxes (.pyt) in ArcGIS


For one of my tasks I need to create a Toolbox with three tools (python scripts).


Instead of ordinary tbx I've decided to write it as Python Toolbox (pyt).


Everything is clear except the "good" way to organize the code.


As I have three tools it is not good way to store them in one file (pyt). So, I've decided to keep each tool in a separate .py file.



Here is a problem: what is the good way to organize several files with PYT for distribution or deployment on ArcGIS for Server? Should I keep them on the same level with PYT file or should I place them in some subdirectory (i.e. "Tools")?


Can you recommend any Esri guidelines or reference "big PYT toolbox" sample?


I have not find anything on this topic. In version 10.0 there was so called ToolShare folder structure which I've used.


This is more a question of style of coding. Because the idea to create one PYT file with 500-1000 or more lines of code does not look good to me and I believe it is not "pythonic" way.



Answer



Have a look at this thread on the ArcGIS forum. Basically just use standard python modules or a package structure and import your tools into the python toolbox.


Something like:


#  \--SomeDir
# | toolbox.pyt
# \--toolpackage

# | __init__.py
# | script_a.py
# | script_b.py


#----------------------------
#The .pyt file
#----------------------------

import arcpy

import toolpackage.script_a.Tool1 as Tool1
import toolpackage.script_a.Tool2 as Tool2
import toolpackage.script_b.Tool3 as Tool3

class Toolbox(object):
def __init__(self):
"""Define the toolbox (the name of the toolbox is the name of the
.pyt file)."""
self.label = "MultiTool Toolbox"
self.alias = "mtt"


# List of tool classes associated with this toolbox
self.tools = [Tool1, Tool2, Tool3]

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...