I'm using model builder to iterate through feature classes in datasets. With each sucessive process step (each model) I'm using the %Name% variable to name new feature classes in a new dataset.
I found that I can't just use %Name% without adding a little to the name of the new feature class; the model didn't seem to like working with two feature classes with the same name. To write the new feature classes to disk, I'm using something like %Name%_AppendedBit. The problem is that with sucessive steps I keep adding more and more bits to the name of the feature class.
I tried using Calculate Value with something like Replace(%Name%,_AppendedBit,_JustIdentifyThisStep) to make a variable with which to name the new feature class, but %output_value% doesn't appear to have any value (just "1"). I tried this as a variant and as a string. I was using vb Replace in the "expression" box, not messing with the "code block" box.
I could use %n% to name the feature classes, but then I lose useful detail from the original feature class name. This could be worked around by doing a spatial join later.
So, the long and the short of it is I'd like to use some kind of string truncation with inline variable substitution.
-
1Are you trying to use the original name of a feature class to name the new feature class?kkaszas– kkaszas2013年09月18日 16:20:25 +00:00Commented Sep 18, 2013 at 16:20
-
Yes. I attempted to use just the name of the feature class as the name of a new feature class in a different data set. Maybe this should have worked, but I got an error saying that the feature class was locked. That's when I appended a tag (some characters) to the end of the name, and it worked. Now I'd like to be able to remove those characters in order to use a different tag.Scott– Scott2013年09月18日 18:22:18 +00:00Commented Sep 18, 2013 at 18:22
-
I have a similar question: how to truncate a full feature class path? I am using Calculate Value in Model Builder in ArcGIS Pro 2.3 Expression: fn(%fullPath%) Code Block: def fn(featureClass): index = featureClass.rfind("\\") featureClass = featureClass[index:] return featureClass Data Type: String where "fullPath" is the in-line variable I am passing in and the backslash \ is to truncate the feature class proper name to the rest of the path So far, this piece of code do not work for no reason!Vincenty– Vincenty2019年02月26日 16:02:13 +00:00Commented Feb 26, 2019 at 16:02
2 Answers 2
Slicing should do the trick. To slice a string do this "%var%"[x:y], where x and y are indices representing the start and end positions of the bits of the string you want to keep. The first character of a string has index 0.
say you got:
var = "myfc_clip"
and you want
myfc_buffer
not
myfc_clip_buffer
you would do something like this
"%var%"[0:5] + "buffer"
this will give you
myfc_buffer
-
Ok. Would I use the slicing and concatenating in Calculate Value?Scott– Scott2013年09月18日 19:26:10 +00:00Commented Sep 18, 2013 at 19:26
-
Yes, building up the full name of your new output feature class in
Calculate Value
using Python is usually the best choice. This way you can ensure the name you get is actually what you want, and you can bypass some of ModelBuilder's "quirks" when it comes to inline variable substitution.Cindy Jayakumar– Cindy Jayakumar2013年09月19日 05:17:21 +00:00Commented Sep 19, 2013 at 5:17 -
1Ok, I got it worked out. ModelBuilder can be quirky. I used "%Name%".replace("AppendedChain","NewSuffix") to get rid of the chain of appended suffixes that accreted through several processes. I found that I couldn't just wipe out the appended chain, apparently because feature classes with the original names were already sitting in another dataset in the same file geodatabase. This prevented
Copy Features
from writing to the new dataset. I also found that theoutput_value
inCalculate Value
didn't follow the iterator very well, so it was necessary to build the model anew for it to work.Scott– Scott2013年09月25日 22:50:00 +00:00Commented Sep 25, 2013 at 22:50 -
-
Slicing did the trick, though, I did not get it to work when I originally asked.Scott– Scott2014年03月13日 20:14:40 +00:00Commented Mar 13, 2014 at 20:14
To truncate feature class name I am using Calculate Value in Model Builder
Expression: changeName("%Name%")
Code Block: def changeName(name): return str(name[:3])
Data Type: String
where "Name" is the in-line variable I am passing in.