Select to view content in your preferred language

in-line variable substitution and raster name max length

1602
5
09-26-2011 10:23 AM
JamieKass
Regular Contributor
Wondering if there's a fix for this. Can you add in-line substituted string values into a raster name without it erroring out and protesting that either 1) the 10-character max has been exceeded, or 2) the name has spaces (if the string has spaces)?
0 Kudos
5 Replies
ShitijMehta
Esri Regular Contributor
You have to follow the length rule.

For spaces etc remove them before using them as inline variable substitution by using Calculate Value tool. Read help here

Check the 5th image in the help doc and the para explaining how to replace decimal. Use the same to replace a space.

To get the name of the feature class use Parse Path tool and pass the output of the Parse Path to Calculate Value..
0 Kudos
JamieKass
Regular Contributor
Thanks Shitij! It's kind of a round-about way, but it should work.
0 Kudos
curtvprice
MVP Esteemed Contributor
Instead of doing multiple replaces of any character you may get, I suggest using the ValidateTableName arcpy method. This requires that you write a little python script in the code block, but it is likely to be more effective. Note only grids have the 10-character limitation. Here I'm using the python os module instead of the Parse Path tool in modelbuilder to take the path apart and put it together.

I am convinced a good understanding of Calculate Value and a little python is necessary to leverage the full power of ModelBuilder.

Calculate Value tool

Expression:
ValidatePath(r"%Output Raster%")

(The "r" and quotes are required -- so the path is read correctly.)

Code block:
def ValidatePath(ds):
  import arcpy
  import os
  # extract pathname
  dspath = os.path.dirname(ds)
  dsname = os.path.basename(ds)
  # fix weird characters in dataset name
  dsname = arcpy.ValidateTableName(dsname,dspath)
  # for grids, truncate to 10 characters
  dsname = dsname[:10]
  return os.path.join(dspath,dsname)
0 Kudos
PeterWilson
Frequent Contributor
Hi Curtis

I came accross your post regarding ValidateTableName and wanted to ask if you could assist me constructing a python expression using calculate value to solve a similar problem. I've attached two print screens of my current model. I'm selecting CAD Features based on their layer name and then using Feature Class to Feature Class to write out the CAD Features to a File Geodatabase based on the Layer Name. The problem that I have is that I need to remove invalid characters from the Layers Name. I've also attached my model itself for you to look at. The Output Geodatabase is where I'm writing out the CAD Features, so I need to use some sort of inline-variable substitution to validate the name of the Feature Class.

Regards
0 Kudos
curtvprice
MVP Esteemed Contributor
could [you] assist me constructing a python expression using calculate value to solve a similar problem. I've attached two print screens of my current model. I'm selecting CAD Features based on their layer name and then using Feature Class to Feature Class to write out the CAD Features to a File Geodatabase based on the Layer Name. The problem that I have is that I need to remove invalid characters from the Layers Name.


Here's some code for using Calculate Value to do this:

Expression:
ValidateName(r"%Layer name%",r"%Output geodatabase%")

Code:
def ValidateName(fc,wks):
  import arcpy
  vName = arcpy.ValidateTableName(fc,wks)
  return vName


The way to connect this up is to make the inputs preconditions (to ensure the input values are ready to go when you run Calculate Value [CV]) and then you can connect the output directly to the Feature Class to Feature Class tool.  Depending on the required parameter type for the next tool you may have to set the output Data Type in Calculate Value. In this case, Feature Class to Feature class was happy to accept the default type (Any Value).

In some situations (say, the name is part of a pathname or the tool validation doesn't accept a parameter type that CV supports) you cannot easiliy connect the CV output to the tool. In those cases, you connect the CV output with a precondition and use the CV output variable in the tool dialog parameter box (e.g. "MyOutput_%Validated_name%").  Calculate Value, as you have seen by now, is key to make the most of ModelBuilder.

[ATTACH=CONFIG]21319[/ATTACH]

Another approach to your problem is the Feature Class to Geodatabase (Multiple) tool, which does all this iteration and validation for you. (BTW, its source code really helped me get the hang of validation and iteration in Python when I was first learning.)
0 Kudos