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've built a model that takes the name of the Excel worksheet containing xy data, and uses that name as the name of the output shapefile. ( Worksheet "massachusetts_sightings" in an xlsx becomes "massachusetts_sightings$.shp"). I do this using Parse Path. Notice that a "$" is added.

If I then feed this shapefile, massachusetts_sightings$.shp into a clip tool (say, to clip out points in the data that are not in my study area), I get an error for an invalid character in the clip tool. My suspicion is the $. I know I could remove this with Calculate Value...but I can't figure out the code block.

My parse path output is called Value:

Codeblock

Can someone help troubleshoot? I haven't used Python in years-

# 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")
share|improve this question
    
One mistake is reassigning the name of the function (GetName) as a variable. Your function simply needs one statement, saying "return os.path.dirname(input)" and that's all. –  Michalis Avraam Oct 5 '12 at 20:51
    
Michalis, thank you for taking the time; I was't able to get your suggestion to work. However...it appears my script is working (i admit, i could've been better written), but files are still being created, but they are named "1_Clip" (the clip should be there, but the 1 replaces the worksheet name). It looks like I'm missing something still. –  MBarretts20 Oct 5 '12 at 21:13
    
without more of the code it is hard to figure out what is wrong. Perhaps edit your question to get more attention too. –  Michalis Avraam Oct 5 '12 at 22:12
add comment

2 Answers

Happily in Python (arcpy) getting rid of this character is a simple one line expression, and so you don't need to use the entire code block; instead in the Expression you should just be able to use str.strip.

"%Value%".strip("$")

Note the code block doesn't need to set output, just needs to return, and you only need to import os if you want the basename. So your expression could be:

strip_chars("%Value%")

And the code block:

import os.path
def strip_chars(s):
    return os.path.basename(s).strip("$")

Note that the value insertion only works in Model Builder, not within Python itself. Instead you can use String Formatting Operations to get the result you're after, for example (note the %s):

fixed = "some_filename"
Projected_and_Clipped_Data = "X:\\Tools\\XY\\point_upload\\Data\\%s_Clip.shp" % fixed
share|improve this answer
    
I continue to get the same answer, no matter what code block / expression I use, the number 1. I've used the provided expressions/code blocks, all giving 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. –  MBarretts20 Oct 9 '12 at 15:10
    
@MBarretts20 Edited my answer above to say that the value insertion only works from within model builder. If you're just running it from a script you should assign the variable in the script and use String Formatting. –  om_henners Oct 9 '12 at 22:16
add comment

Solved- I needed to move the parse path / calculate value to the first step in the model- not the last, as I had originally. I believe the expressions/codes above will work, but were not the final ones used. But a huge thank you to those who took time to weigh in. Most appreciated. enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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