2

I created a Python toolbox to run in ArcGIS Pro 3.0. I wish to save toolbox results in another geodatabase than the one by default, which contains input data.

This toolbox will be run by several users so I can't write the full path, and depending on the project the user is working on, I wish she/he could choose to save results in a geodatabase created by her/him, meaning with a customized name.

I wish the path and names of the results (defined as parameters), are pre-filled, so the user doesn't need to choose her/himself.

Below is a part of the script

import arcpy
import os
class Toolbox(object):
 def __init__(self):
 """Define the toolbox (the name of the toolbox is the name of the
 .pyt file)."""
 self.label = "StatisticsToolbox"
 self.alias = "StatisticsToolbox"
 # List of tool classes associated with this toolbox
 self.tools = [Tool]
class Tool(object):
 def __init__(self):
 """Define the tool (tool name is the name of the class)."""
 self.label = "Statistics of Subsistence Activities by Type in a Defined Area"
 self.description = "This tool calculates the total surface of each type of subsistence activities \
 in an area defined by the user using a polygon shapefile as parameter"
 self.canRunInBackground = False
 def getParameterInfo(self):
 """Define parameter definitions"""
 ''' Input parameters '''
 # Parameter 0: Subsistence layer
 param0 = arcpy.Parameter(
 displayName="Input layer of subsistence activities",
 name="in_features",
 datatype="GPFeatureLayer",
 parameterType="Required",
 direction="Input"
 )
 param0.value = "Interne\\Activite_subsistance"
 # Parameter 1: Clip feature
 param1 = arcpy.Parameter(
 displayName="Study area used for clipping",
 name="clip_features",
 datatype="GPFeatureLayer",
 parameterType="Required",
 direction="Input"
 )
 param1.value = "studyArea_poly"
 # Parameter 2: Output gdb
 param2 = arcpy.Parameter(
 displayName="Output Geodatabase",
 name="out_gdb",
 datatype="DEWorkspace",
 parameterType="Required",
 direction="Input"
 )
 param2.value = "ProjetXYZ.gdb"
 ''' Output parameters '''
 # Parameter 3: Output clip
 param3 = arcpy.Parameter(
 displayName="Clipped layer name",
 name="out_clip_name",
 datatype="GPFeatureLayer",
 parameterType="Required",
 direction="Output"
 )
 param3.value = "clip"
 params = [param0, param1, param2, param3]
 return params
 def isLicensed(self):
 """Set whether tool is licensed to execute."""
 return True
 def updateParameters(self, params):
 """Modify the values and properties of parameters before internal
 validation is performed. This method is called whenever a parameter
 has been changed."""
 return
 def updateMessages(self, params):
 """Modify the messages created by internal validation for each tool
 parameter. This method is called after internal validation."""
 return
 def execute(self, params, messages):
 """The source code of the tool."""
 # List parameters
 inFeatures = params[0].valueAsText
 clipFeatures = params[1].valueAsText
 outGDB = params[2].valueAsText
 outCLIP = params[3].valueAsText
 # Process: Feature Class To Feature Class (conversion)
 Activite_subsistance_gdb = arcpy.conversion.FeatureClassToFeatureClass(
 in_features=inFeatures,
 out_path=outGDB,
 out_name="Activite_subsistance_gdb"
 )[0]
 # Process: CLIP SUBSI (Clip) (analysis)
 arcpy.analysis.Clip(
 in_features=Activite_subsistance_gdb,
 clip_features=clipFeatures,
 out_feature_class=outCLIP)
 return
 def postExecute(self, params):
 """This method takes place after outputs are processed and
 added to the display."""
 return

As the default gdb is the geodatabase containing input data, parameters 0 and 1 are correctly found by the tool.

The user will have created a gdb for the project (parameter 2) in the same directory as the default geodatabase.

But how can I specify the path for parameter 3, based on the value of parameter 2 once defined by the user?

I tried to update param3 value in the function updateParameters, using param2 value.

param3.value = os.path.join(os.path.dirname(__file__), params[2].valueAsText,'clip.shp')

but I get the error The name contains invalid characters.

Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Aug 10, 2022 at 16:16
1
  • 4
    "clip.shp" is an invalid geodatabase table name Commented Aug 10, 2022 at 16:38

1 Answer 1

4

@Vince’s comment helped me find 2 problems which were the extension, and os.path.dirname(__file__), which is the path of the toolbox instead of the workspace as I first thought

I made the correction in updateParameters, and it works great :)

 def updateParameters(self, params):
 """Modify the values and properties of parameters before internal
 validation is performed. This method is called whenever a parameter
 has been changed."""
 params[3].value = os.path.join(params[2].valueAsText, 'clip')
 return
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
answered Aug 10, 2022 at 16:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.