Take the 2-minute tour ×
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 some Python script tools in ArcGIS for Desktop.

I need to reproduce what all geoprocessing tools do when the first input parameter is introduced: autogenerate a workspace and basename for the outputs.

How can I do that?

share|improve this question

closed as unclear what you're asking by PolyGeo, Get Spatial, Matthias Kuhn, neogeomat, Erica Aug 27 '14 at 11:19

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

1  
Would you please elaborate on how a workspace is being auto-generated with a GP tool? –  Alex Tereshenkov Aug 21 '14 at 10:59
2  
there isn't enough detail in your Q for us to help. Perhaps include a sample of your script, reduced to it's simplest possible form (sometimes pseudo code can be clearer than the real thing). Also What GP tool do you see creating a workspace automatically? (most ask for a pre-existing workspace). ...and finally a tip: add the GP tool to a model, then export as python script. That will show you how the tool does it. (And if that works, come back and answer your own question so others can benefit from your research) –  matt wilkie Aug 21 '14 at 17:19
    
Sorry, What I need is the output workspace and basename to appear in the tool dialog box, when the user select a first input dataset. It's a behavior of all geoprocessing tools in arcgis. –  Margaral Aug 25 '14 at 8:02
    
I think I have to adapt de validation process for that. Or something about the schema, but I dont control pretty well these terms, nor how to work with them. –  Margaral Aug 25 '14 at 8:13
    
Well, it should be something with the validation process, because it happens before the tool runs, when the user is interacting with the tooldialog box... –  Margaral Aug 25 '14 at 8:22

3 Answers 3

up vote 1 down vote accepted

You should use arcpy.Describe() and/or the os.path Python module. For example:

import arcpy, os
input = arcpy.GetParameterAsText(0)
output_workspace = arcpy.Describe(input).path # the file path, or geodatabase for geodatabase datasets
suffix = 1
output_name = arcpy.Describe(input).baseName + "_" + suffix # the file base name + a suffix
arcpy.CopyFeatures_management(input, os.path.join(output_workspace, output_name))

You can also check for the existence of the output if you want to make sure it doesn't already exist, increment the suffix if it does, etc.

EDIT: Based on the example above, if you want to have this output_name value returned in the tool's interface as asked in your comment, you should modify the validation code:

def updateParameters(self):
  if self.params[0].value:
      output_workspace = arcpy.Describe(self.params[0]).path 
      suffix = 1
      output_name = arcpy.Describe(self.params[0]).baseName + "_" + suffix
      self.params[1].value = os.path.join(output_workspace, output_name)

  return

And your script should look like this:

 input = arcpy.GetParameterAsText(0)
 output = arcpy.GetParameterAsText(1)
 arcpy.CopyFeatures_management(input, output)

Don't forget to import all necessary modules in both codes.

Refer to Customizing script tool behavior, this is where the code comes from and there are other examples for other use cases.

share|improve this answer
    
Hi, but how to show the "output_name" variable in the dialog tool box??? –  Margaral Aug 25 '14 at 8:17
    
You can do that by modifying the validation code of your script tool. See Understanding validation in script tools and Customizing script tool behavior –  GISGe Aug 25 '14 at 8:28
    
I had already checked these pages, but I don't find how to modify the UpdateParameter method in validation. I'm looking for some codes, without success. –  Margaral Aug 25 '14 at 8:57
    
See updated answer. –  GISGe Aug 25 '14 at 9:38
    
Thanks for your help GISGe. As my input data is a Raster Layer, the Describe gives an error ("Describe input value is not a valid type"). I have tried to introduce de raster dataset by browsing, but got the same error. The function recommended by help would be: workspace = os.path.dirname(self.params[0].value.value) . But also get an error. –  Margaral Aug 26 '14 at 8:28

Not sure what you mean. The only options are to "derive" a parameter, ie. when you select a featureclass as a first parameter, one of the derived parameters could be a list of fields for the second input parameter. Alternately, you can provide defaults for any parameter so that when a tool is opened, it will appear as the default option.

share|improve this answer
    
Hi Dan, this could be the key. If my output parameter is derived, how can the basename be shown in the tooldialog box?? –  Margaral Aug 25 '14 at 8:19

The simplest solution I have found thanks to GISGe's hint is modifying only the updateParameter() method in validationtool class, adding this code.

if self.params[0].value:
    self.params[4].value = "output"

This will generate a string with the default workspace with basename, "output". I dont have to modify the python script at all.

Thanks to all!!!

share|improve this answer

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