Parse Path or Calculate Value?

4131
7
Jump to solution
10-04-2012 09:24 AM
MikeBarrett
New Contributor
I am building a tool to simplify the uploading xy locations, defining and projecting, as I do this constantly. I???ve gotten that to work; upload the xy table, choose a GCS, then choose a projection. Currently, the projected file comes out as ???projected_data.shp???.  The ideal version of this would automatically name the projected output file with the name from the initial xy table. If the upload table were called ???Massachusetts sightings.xlsx???, the tool would take ???Massachusetts sightings??? and apply it to the output name, producing ???Massachusetts sightings.shp???. [ATTACH=CONFIG]18194[/ATTACH]

I can???t figure out if I need / want the parse path tool or Calculate value. I???ve read postings here, but can???t find out how to reconnect that ???Name??? field from parse path back to the Project output. (Although, I realize that with an xlsx file, we???re asked to provide which sheet data comes from, typically ???Sheet$??? or ???Sheet1???. Will parse path give this, or will it give the file name?)

For Parse Path, I have [ATTACH=CONFIG]18196[/ATTACH]
For Calculate Value, I have [ATTACH=CONFIG]18195[/ATTACH]

I???ve been trying for a few days and can???t figure out how to reconnect the parse path output, or the calculate value output back into the model- notice they are not connected.

Ideas?
thanks so much!
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable
Original User: m.gasior

Try the following approach:
In your model set Value inline variable as precondition to Calculate Value and output_value inline variable as precondition to Make XY Event Layer.
This should enforce creating variables first before creating layers.
The model below works with some of the Python expressions mentioned earlier:
[ATTACH=CONFIG]18311[/ATTACH]

View solution in original post

0 Kudos
7 Replies
by Anonymous User
Not applicable
Original User: j.quinn

Try to set the final output of the model to %Value%.shp, which will take advantage of using inline variables to grab the value that's parsed from the Parse Path tool.
0 Kudos
MikeBarrett
New Contributor
Ah! Simpler than I thought! Thanks!

For those following;
You simply write    %Value%     (no quotes needed) into the output file name of the last tool to run (in this case Project), and Model Builder pulls the Value you've extracted by using the Parse Path Tool. (Value can be either the File, Name, Path, etc). You don't need to use Calculate Value, and you don't need a connecting line.
0 Kudos
MikeBarrett
New Contributor
Ok- so when this model is updated to include a clip at the end, arc adds a $ to the worksheet name, which is an invalid character. Using calculate value to remove the $, i get a 1 instead of the worksheet name, after writing a variety of codes and expressions.

# Import arcpy module
import arcpy

# Load required toolboxes
arcpy.ImportToolbox("Model Functions")

# Set Geoprocessing environments
arcpy.env.scratchWorkspace = "X:\\Tools\\XY\\point_upload\\Scratch"
arcpy.env.workspace = "X:\\Tools\\XY\\point_upload\\Data"

# Script arguments
XY_Table = arcpy.GetParameterAsText(0)

Define_Coordinate_System = arcpy.GetParameterAsText(1)

Projection = arcpy.GetParameterAsText(2)

Geographic_Transformation = arcpy.GetParameterAsText(3)

Study_Area = arcpy.GetParameterAsText(4)

Projected_and_Clipped_Data = arcpy.GetParameterAsText(5)
if Projected_and_Clipped_Data == '#' or not Projected_and_Clipped_Data:
    Projected_and_Clipped_Data = "X:\\Tools\\XY\\point_upload\\Data\\%fixed%_Clip.shp" # provide a default value if unspecified

# Local variables:
xy_event = XY_Table
v_defined = xy_event
defined_data = v_defined
v_projected = defined_data
Value = XY_Table

# Process: Parse Path
arcpy.ParsePath_mb(XY_Table, "NAME")

# Process: Make XY Event Layer
tempEnvironment0 = gp.scratchWorkspace
arcpy.env.scratchWorkspace = "X:\\Tools\\XY\\point_upload\\Scratch"
tempEnvironment1 = gp.workspace
arcpy.env.workspace = "X:\\Tools\\XY\\point_upload\\Data"
arcpy.MakeXYEventLayer_management(XY_Table, "", "", xy_event, "", "")
arcpy.env.scratchWorkspace = tempEnvironment0
arcpy.env.workspace = tempEnvironment1

# Process: Copy Features
tempEnvironment0 = gp.scratchWorkspace
arcpy.env.scratchWorkspace = "X:\\Tools\\XY\\point_upload\\Scratch"
arcpy.CopyFeatures_management(xy_event, v_defined, "", "0", "0", "0")
arcpy.env.scratchWorkspace = tempEnvironment0

# Process: Define Projection
arcpy.DefineProjection_management(v_defined, Define_Coordinate_System)

# Process: Project
tempEnvironment0 = gp.scratchWorkspace
arcpy.env.scratchWorkspace = "X:\\Tools\\XY\\point_upload\\Scratch"
tempEnvironment1 = gp.workspace
arcpy.env.workspace = "X:\\Tools\\XY\\point_upload\\Data"
arcpy.Project_management(defined_data, v_projected, Projection, Geographic_Transformation, "")
arcpy.env.scratchWorkspace = tempEnvironment0
arcpy.env.workspace = tempEnvironment1

# Process: Clip
arcpy.Clip_analysis(v_projected, Study_Area, Projected_and_Clipped_Data, "")

# Process: Calculate Value
arcpy.CalculateValue_management("\"%Value%\".strip(\"$\")", "\\n", "String")
0 Kudos
by Anonymous User
Not applicable
0 Kudos
MikeBarrett
New Contributor
Thank you for the help; however, I continue to get the same answer, no matter what code block / expression I use, the number 1.
I've used the following expression / code blocks, all giving the following[ATTACH=CONFIG]18282[/ATTACH]
Expression:
removeChar("%Value%")
Code:
def removeChar(val):
  if val.endswith("$"):
    return val[:-1]
and/or
def removeChar(val):
  return val.split("$")[0]

Expression: "%Value%".strip("$")
and
Expression:
strip_chars("%Value%")
Code
import os.path def strip_chars(s):    
     return os.path.basename(s).strip("$")


no matter what code I use, all I get is the 1 as my file name. Perhaps I'm using the inline variable wrong. If I use "%Value%".strip("$") as my expression, and then rename the green output circle as fixed, or as outputvalue, and then in the Clip tool, I write either %fixed%_clip, %Value%_clip, or %outputvalue%_clip, I get "1_clip.shp" as the output. I recieve a 1_clip no matter what combination of codes, expressions, etc I enter.

I'm now remembering why I don't love arcpy...until it works, then we're best friends.
0 Kudos
by Anonymous User
Not applicable
Original User: m.gasior

Try the following approach:
In your model set Value inline variable as precondition to Calculate Value and output_value inline variable as precondition to Make XY Event Layer.
This should enforce creating variables first before creating layers.
The model below works with some of the Python expressions mentioned earlier:
[ATTACH=CONFIG]18311[/ATTACH]
0 Kudos
MikeBarrett
New Contributor
Perfect! I had been exploring moving the parse path/calculate value earlier in the model, but setting it at the very beginning works best. Thanks to all who supplied help!
0 Kudos