ENVI ® Py for ArcGIS ® Library¶
ENVI Py for ArcGIS Library provides tools for generating ArcGIS Python toolboxes.
Usage¶
Create GPToolbox¶
To generate an empty toolbox from Python, use the GPToolbox class and initialize with an empty list of task objects:
>>> from envipyarclib import GPToolbox
>>> toolbox = GPToolbox([], 'DEMO')
>>> toolbox.create_toolbox('C:\\TEMP\\demo.pyt')
To create a toolbox with empty GPTools, you must first create a Task class defining the properties the GPToolbox supports:
class Task(object):
def __init__(self, name, display_name, description):
self.name = name
self.display_name = display_name
self.description = description
self.uri = name
@property
def parameters(self):
return dict()
Once the Task class is defined, you can pass in task objects to the GPToolbox and generate empty GPtools:
>>> toolbox = GPToolbox([Task('firstTask', 'First Task', 'Does nothing'),
Task('secondTask', 'Second Task', 'Still Does nothing')],
'DEMO')
>>> toolbox.create_toolbox('C:\\TEMP\\demo.pyt')
To import Python modules or any global functionality to the toolbox, you can use the imports_template keyword when initializing the GPToolbox. The template string must be formatted with zero indentation. For example, to include arcpy and os:
from string import Template
from envipyarclib import GPToolbox
imports_template = Template('''
import os
import arcpy
''')
toolbox = GPToolbox([Task('firstTask', 'First Task', 'Does nothing')], 'DEMO',
imports_template=imports_template)
toolbox.create_toolbox('C:\\TEMP\\demo.pyt')
To add code to the execute method of all GPTools, you can use the execute_template keyword when initializing the GPToolbox. The template string must be formatted with two indentations. From the previous example, to add the system path to the messages when running the GPTool:
from string import Template
from envipyarclib import GPToolbox
imports_template = Template('''
import os
import arcpy
''')
execute_template = Template('''
messages.AddMessage('System Path: ' + str(os.sys.path))
''')
toolbox = GPToolbox([Task('firstTask', 'First Task', 'Does nothing')], 'DEMO',
imports_template=imports_template,
execute_template=execute_template)
toolbox.create_toolbox('C:\\TEMP\\demo.pyt')
Test GPToolbox Data Types¶
This library provides test cases and test tasks for testing ENVI and IDL data types available in the envipyarclib.test package. To test a data type, you must implement the test config abstract base class for generating a toolbox and importing it into arcpy before running a test:
import arcpy
from envipyarclib.test.config import Config
class MyConfig(Config):
def __init__
def setup_toolbox(self, engine_name, task_name, toolbox_name):
# Create Python toolbox here
arcpy.ImportToolbox(toolbox_file)
Once the Config class is created, you can import a data type test in your module for testing. Once imported, the unittest module will be able to find and run the test cases:
from envipyarclib.test.datatype.bool import TestDataTypeBool
config = MyConfig()
# Attach the config to the test case
TestDataTypeBool.config = config
API Documentation¶
GPToolbox¶
GPToolbox is used as a base class to create GPTool wrappers for ENVI, IDL, and GSF Analytics.
-
class
envipyarclib.gptoolbox.
GPToolbox
(tasks=None, alias=None, imports_template=<string.Template object>, execute_template=<string.Template object>, parameter_templates=None)[source]¶ GPToolbox is used as a base class to create GPTool wrappers for ENVI, IDL, and GSF Analytics.
Parameters: - tasks – a list of tasks to map to GPTools where each task name is a GPTool in the toolbox.
- alias – The alias of the generated toolbox
- imports_template – The template string code for defining imports
- execute_template – The template string code for GPTool execution
- parameter_templates – The python package containing parameter templates. Templates must implement the envipyarclib.gptool.parameter.Template class.
-
create_toolbox
(filename)[source]¶ Creates a new Python toolbox where each task name is a GPTool in the toolbox.
Parameters: - filename – the filename of the generated toolbox
- service_name – The name of the ESE service containing the tasks. Only tasks from one service may be used.
- tasks – The list of tasks from the service to build as GPTools.
GPTool Parameter Builder¶
The GPTool Parameter Builder is responsible for creating code blocks to be placed into the main GPTool template. Each create method corresponds to a GPTool method that must be defined in order for it to be a valid tool. These methods implement the GetParameterInfo, UpdateParameter, and Execute methods of the GPTool Python class.
For example, to define a GPTool in a Python toolbox, start out with a class definition and implement the methods arcpy expects:
class myTool(Object):
def __init__(self):
self.label = "My Tool"
self.description = "Tool description"
self.canRunInBackground = True
def getParameterInfo(self):
# builder.create_param_info() goes here
def is Licensed(self):
return True
def updateParameters(self, parameters):
# builder.create_update_parameter goes here
def updateMessages(self, parameters):
return
def execute(self, parameters, messages):
# builder.create_pre_execute goes here
# submit job
# builder.create_post_execute goes here
-
class
envipyarclib.gptool.parameter.builder.
ParameterMap
[source]¶ Convenience class for holding a template map
-
exception
envipyarclib.gptool.parameter.builder.
UnknownDataTypeError
[source]¶ Error class for raising unknown datatypes
-
envipyarclib.gptool.parameter.builder.
convert_list
(in_list)[source]¶ Converts a list of strings to a printable list of object names
-
envipyarclib.gptool.parameter.builder.
create_param_info
(task_params, parameter_map)[source]¶ Builds the code block for the GPTool GetParameterInfo method based on the input task_params.
Parameters: task_params – A list of task parameters to map to GPTool parameters. Returns: A string representing the code block to the GPTool GetParameterInfo method.
-
envipyarclib.gptool.parameter.builder.
create_post_execute
(task_params, parameter_map)[source]¶ Builds the code block for the GPTool Execute method after the job is submitted based on the input task_params.
Parameters: task_params – A list of task parameters from the task info structure. Returns: A string representing the code block to the GPTool Execute method.
-
envipyarclib.gptool.parameter.builder.
create_pre_execute
(task_params, parameter_map)[source]¶ Builds the code block for the GPTool Execute method before the job is submitted based on the input task_params.
Parameters: task_params – A list of task parameters from the task info structure. Returns: A string representing the code block to the GPTool Execute method.
-
envipyarclib.gptool.parameter.builder.
create_update_parameter
(task_params, parameter_map)[source]¶ Builds the code block for the GPTool UpdateParameter method based on the input task_params.
Parameters: task_params – A list of task parameters from the task info structure. Returns: A string representing the code block to the GPTool UpdateParameter method.
GPTool Parameter Template¶
-
class
envipyarclib.gptool.parameter.template.
Template
(data_type)[source]¶ Interface class for mapping Task parameters to ArcGIS GPTool parameters.
-
default_value
()[source]¶ Defines the code block for this parameter data type in the GPTool GetParameterInfo if a default value exists.
Returns: Returns the string.Template object.
-
get_parameter
(task_param)[source]¶ Defines the code block for this parameter data type in the GPTool GetParameterInfo method. All code returned must begin with 2 indents. The template is substituted against the GP parameter dictionary.
Parameters: task_param – The task parameter information. Returns: Returns the string.Template object.
-
parameter_names
(task_param)[source]¶ Defines the code block for the parameter variable names in the GPTool GetParameterInfo method.
Parameters: task_param – The task parameter Returns: A list of string.Template objects representing the parameter variable names defined in get_parameter.
-
post_execute
()[source]¶ Defines the code block for this parameter data type in the GPTool Execute method after the task is executed.
Returns: Returns the the string.Template object
-