# Get parameter as text

2494
6
09-14-2016 03:36 AM
New Contributor II

I have 5 parameters in my arcpy script, imported into a toolbox.

inputFC = arcpy.GetParameterAsText(0)

outputFC = arcpy.GetParameterAsText(1)

heightfield = arcpy.GetParameterAsText(2) #Must be in the same units as the coordinate system!

azimuth = math.radians(float(arcpy.GetParameterAsText(3))) #Must be in degrees

altitude = math.radians(float(arcpy.GetParameterAsText(4))) #Must be in degrees

I have building footprints, including height in the attribute, and I want to compute the shadow of the building in 2D.

Could anyone tell me which 'Data type' and parameter properties I should define before running the script? Thanks

Tags (2)
1 Solution

Accepted Solutions
Regular Contributor II

Is there a reason why you're specifying the Azimuth and Altitude values as Any Value and not Double? The Any Value type would allow you to insert alphanumeric text and I'd assume you'd need to add logic into your code to account for this. For example, Any Value would allow you to provide values like 90° or EAST or 90.0, whereas Double would restrict you to 90.0. You could also use validation logic to verify that your azimuth or altitude values are within the expected ranges. I've included a quick example of the logic below.

import arcpyclass ToolValidator(object):  """Class for validating a tool's parameter values and controlling  the behavior of the tool's dialog."""  def __init__(self):    """Setup arcpy and the list of tool parameters."""    self.params = arcpy.GetParameterInfo()  def initializeParameters(self):    """Refine the properties of a tool's parameters.  This method is    called when the tool is opened."""    return  def updateParameters(self):    """Modify the values and properties of parameters before internal    validation is performed.  This method is called whenever a parameter    has been changed."""    return  def updateMessages(self):    """Modify the messages created by internal validation for each tool    parameter.  This method is called after internal validation."""    if self.params[3].value < 0.0 or self.params[3].value > 360.0:        self.params[3].setErrorMessage("Value must be 0 to 360")    else:        self.params[3].clearMessage()    return‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

6 Replies
MVP Frequent Contributor

The numbers (Height) etc could be added as any value

Also see here :

defining-parameter-data-types-in-a-python-toolbox

New Contributor II

The height value is not a constant value through the whole building shapefile. It varies from one polygon to another. Its the same data you have commented on the other question of mine: HERE is there a 'data type' where it can import the value of a column of attribute (building height). And I have tried it with the following parameters:

I've typed 'Building_H' at the height field, but then ended up with:

Occasional Contributor III

I think the code may be failing on this section:

    # Create in-memory feature class for holding the shadow polygons    tempshadows = r"in_memory\tempshadows"    arcpy.CreateFeatureclass_management(        "in_memory",        "tempshadows",        "POLYGON", "", "", "",        outputSR)    arcpy.AddField_management(tempshadows, origfidfield, "LONG")

The syntax for the Create Feature Class tool is:

CreateFeatureclass_management (out_path, out_name, {geometry_type}, {template}, {has_m}, {has_z}, {spatial_reference}, {config_keyword}, {spatial_grid_1}, {spatial_grid_2}, {spatial_grid_3})It looks like you are creating a feature class called in_memory\in_memory\tempshadows - and I think one of the in_memory's needs to be removed.
Occasional Contributor III

I agree with Neil.  The "Any Value" option would work well.

In addition to this, if your heightfield parameter should have a field as the input, you can use "Field" for the data type.  You can then loop through the values in the field within the script.

If you want to validate that the input values for heightfield, azumuth, and altitude are in the correct data type (ex; degrees) you can add some code to validate the values.  Ex; loop through all azimuth values to ensure they are of the float data type and all between 0 and 360 - this would be done within the script after the parameters have been set, but before the rest of the script executes.  Then just spit out a message to the user if the data is not in the correct format.

Regular Contributor II

Is there a reason why you're specifying the Azimuth and Altitude values as Any Value and not Double? The Any Value type would allow you to insert alphanumeric text and I'd assume you'd need to add logic into your code to account for this. For example, Any Value would allow you to provide values like 90° or EAST or 90.0, whereas Double would restrict you to 90.0. You could also use validation logic to verify that your azimuth or altitude values are within the expected ranges. I've included a quick example of the logic below.

import arcpyclass ToolValidator(object):  """Class for validating a tool's parameter values and controlling  the behavior of the tool's dialog."""  def __init__(self):    """Setup arcpy and the list of tool parameters."""    self.params = arcpy.GetParameterInfo()  def initializeParameters(self):    """Refine the properties of a tool's parameters.  This method is    called when the tool is opened."""    return  def updateParameters(self):    """Modify the values and properties of parameters before internal    validation is performed.  This method is called whenever a parameter    has been changed."""    return  def updateMessages(self):    """Modify the messages created by internal validation for each tool    parameter.  This method is called after internal validation."""    if self.params[3].value < 0.0 or self.params[3].value > 360.0:        self.params[3].setErrorMessage("Value must be 0 to 360")    else:        self.params[3].clearMessage()    return‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

New Contributor II

Thanks, It worked finally perfect!