3

I'm trying to pass a couple of strings from python as parameters in the extract by mask tool in modelbuilder. Unfortunately it doesn't recognize them. When I type them in the parameters it thinks I'm trying to add files called %shp% and %tif% from and to my default gdb. This is my first ever attempt at passing values from python to modelbuilder so I have no idea how to go about trying to fix it. This is also my first question on StackExchange so hopefully everything is clear. Here's the code:

import arcpy
import os
invalue = arcpy.GetParameterAsText(0)

#Getting the basename of the input file
newbasename = os.path.basename(invalue).strip("'")
newname = os.path.splitext(newbasename)[0]

#Creating the strings for the output parameters
outshp = newname + ".shp"
outtif = newname + ".tif"

#Setting the strings as parameters
arcpy.SetParameterAsText(1, "C:/Base_Data/Topo/boundary/" + outshp)
arcpy.SetParameterAsText(2, "C:/Base_Data/Topo/geotif/" + outtif)

and the model:

model

As you may have guessed I want to batch process a bunch of these rasters where I have a shapefile of the same name as the mask and want the output to be the same name as the input but in a different location.

  • have you set your output parameters as Direction = Output when adding the python to ArcToolbox? – Midavalo Jan 14 '16 at 3:28
  • I also wonder whether the SetParameterAsText should be 0 and 1, not 1 and 2 – Midavalo Jan 14 '16 at 3:31
  • Is there any reason why you're not running the Extract by Mask from python? And are you trying to feed the 'tif' as the output name for Extract by Mask? I'm not sure that will work, as I don't think you'll be able to specify one tool's output to be the output for another – Midavalo Jan 14 '16 at 5:24
  • 1
    Thanks for the responses. Direction is set to output. They are 1 and 2 because I have a GetParameterAsText set to 0. I have scripted this entire thing out already, but there is a known issue with running the extract tool in python that causes the outputs to be either shifted or the tool will fail to remove part of the raster outside of the mask (this happens on some rasters but not others, it's very bizarre), however the tool works perfectly when run from the toolbox. So, I am hoping if I batch run it through model builder my outputs will come out error free. – Luke Jan 14 '16 at 15:12
  • Plus I'm just interested in how to make this work for my own knowledge – Luke Jan 14 '16 at 15:13
1

You don't need a script to do get your input's basename and reuse it in parameters' names. Add the 'Parse Path' tool to your model and connect it to your input raster. Chose 'NAME' for the 'Parse Type' parameter:

enter image description here

Then, in the parameters of the Extract by Mask tool, reuse this name variable (here called by its default name, 'Value') in the path of the mask and output parameters. Surround it with '%' characters:

enter image description here

This is called inline variable substitution, see more details about it here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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