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.

Within a .pyt python toolbox I'd like to have a parameter of data type GPLinearUnit get a default value based on an input Feature Class (FC). The standard Buffer (Analysis) tool works as a great example of what I'm trying to imitate:

Desired User Input

The closest I've been able to come is to set units in updateParameters based on the FC spatialReference.linearUnitName, but linearUnitName and GPLinearUnit units aren't the same and there are 50+ linear units. Anyone know a better way to do this or a reference/code somewhere I could at least pull the linearUnitName to GPLinearUnit from?

def getParameterInfo(self):
    inFC = arcpy.Parameter(
        displayname = "Input FC",
        name = "inputFC",
        datatype = "DEFeatureClass",
        parameterType = "Required",
        direction = "input")
    units = arcpy.Parameter(
        displayName = "Distance & Units",
        name="Units",
        datatype="GPLinearUnit",
        parameterType="Required",
        direction = "Input")
    params = [inFC, units]
    return params

def updateParameters(self, params):
    if not params[1].altered and params[0].altered:
        spRef=arcpy.Describe(params[0].valueAsText).spatialReference
        if spRef.linearUnitName == "Foot_US":
            params[1].values = "0 Feet"
    return

I should also point out that this code defaults the distance to 0, where as the Buffer tool I'm imitating is able to set units without a distance.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

In getParameterInfo, set a dependency between the two parameters.

units.parameterDependencies = [inFC.name]

share|improve this answer
    
I was hoping there was a parameterDependencies based solution, just couldn't find the right property. .name is usually used to return a string for the naming of the input (i.e. I would have expected this to give "L:\Projected_FC.shp") can you offer any explanation why it works? –  jbosq Apr 2 at 14:20
    
Parameter dependencies are used for many of these types of relationships. To go back to your original observation that Buffer showed this behavior, you can observe this by testing Buffer's own parameters. For instance: arcpy.GetParameterInfo('buffer_analysis')[2].parameterDependencies --> which returns [0]. You can see the distance parameter has a relationship with the input (here it shows the parameter by index, while in a python toolbox it is handled by parameter name). –  DWynne Apr 2 at 16:28

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.