3

I am trying to run a buffer tool as part of script that I will both use as a standalone script and import into ArcToolbox. I'm just wondering, if I use GetParameterAsText for the buffer distance, how do I set the linear units? E.g. I want it preset so they only have to type 300 and the script takes that to be meters? Here is what I have so far:

# Buffer the Thames feature class to create the Flood Risk Area
in_features = "Thames"
out_feature_class = "Flood_R_A"
# Run the Buffer Tool
arcpy.Buffer_analysis (in_features, out_feature_class, arcpy.GetParameterAsText(5),    "FULL", "ROUND", "ALL", "")
  • +1 I have wondered this as well. I have only splitted the string from GetParameterAsText with space as delimiter and converted the first part to a number (and thereby ignored units completely). But there has to be a clever way to handle the units! – Martin Jul 24 '13 at 13:01
3

If you want the user to specify both a distance and units, as can be found in the Buffer tool (and most other tools that specify a distance), you can use the Linear Unit parameter. You can pass this whole parameter value to the tool

The distance passed to buffer can be numeric (in which case it's in the units of the feature class) or as numeric with units (meters, feet, degrees, etc).

  • 2
    Agreed. Using the "Linear Unit" parameter is your best best, as it makes your script more flexible than hard-coding units into your script. – RyanDalton Jul 24 '13 at 14:39
  • Is there something on the script side that can handle different units, or do I need to hard code conversions to get everything consistent? Constants and math operations usually varies a bit depending on units, so would be nice to have different units as input, but only one for the rest of the script. – Martin Jul 25 '13 at 6:23
  • 1
    It would not be a fun task to hardcode for every possible combination of units. I've shied away from converting units in my scripts for this very reason. I just force the user to use the units of the feature class in this instance. – Paul Jul 25 '13 at 7:03
  • How do you do force them to match units to feature class? – Adam B May 3 '18 at 5:44
  • 1
    @AdamB it defaults to the units of the FC if no unit is specified (ie, you only specify a number) – Paul May 8 '18 at 21:20
1

This is untested but should work:

arcpy.Buffer_analysis (in_features, out_feature_class, arcpy.GetParameterAsText(5) + " Meters",    "FULL", "ROUND", "ALL", "")

To avoid confusion between whether you are asking for a number or a "number with units", I also advise structuring your code more like this:

bufDistAsStringWithNoUnits = arcpy.GetParameterAsText(5)

arcpy.Buffer_analysis (in_features,out_feature_class,bufDistAsStringWithNoUnits + " Meters","FULL","ROUND","ALL","")

However, I think @Pauls answer which recommends using the Linear Unit parameter instead is sage advice.

  • I may be wrong, but does the 5 need to be converted to an integer. Is it stands, I think it would be interpred as text. – dchaboya Jul 24 '13 at 15:12
  • @dchaboya, I'm pretty sure you are wrong on your above statement. A) the "5" in arcpy.GetParameterAsText(5) is referring to the 6th parameter passed into the script (starting with a base 0) and B) that parameter needs to come across a string in order to be able to concatenate the numeric value with the distance units (meters) for the buffer_distance_or_field parameter. – RyanDalton Jul 24 '13 at 15:46
  • @dchaboya, no that wouldn't work. Integers can't be concatenated with strings unless they are cast to str() first. Edit: As RyanDalton mentions, parameters are accessed as strings. – Paul Jul 24 '13 at 15:47
  • 2
    Well it looks like you are trying to pass a linear unit then appending the unit to that string. You either get a numeric input and hardcode the units or get a linear unit with user specified distance and units. Can't mix and match. – Paul Jul 24 '13 at 16:34
  • 1
    @Dunuts, You are combining both solutions by PolyGeo & Paul. To use PolyGeo's code above, drop the + " meters". you are essentially combining the "Meters" from the Linear Unit parameter with a hard-coded "meters". – RyanDalton Jul 24 '13 at 16:36

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.