Parameters in Python script

3448
10
10-29-2015 07:22 AM
TerryGustafson
Occasional Contributor II

I'm trying to figure out parameters in python.  I created a model and it has some parameters in it.  When I run it and create a GP service from it, it asks me to enter those parameters and then runs.  I have a python script that I added to a toolbox hoping it would ask for those parameters as well.  when I add the script to the toolbox and run it, it says this tool has no parameters.  I assumed the in the script arguments from my example that the first_point = arcpy.GetParametersAsText(0) would ask me to enter the first point.  Can someone point me in the right direction?  TIA

# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# new_seg4.py
# Created on: 2015-10-29 07:32:00.00000
#   (generated by ArcGIS/ModelBuilder)
# Usage: new_seg4 <first_point> <end_point> <first_point__2_> <end_point__3_> <beg_point> <end_pt> <first_pt_loc> <end_pt_loc> <points> <segment> <temp__2_> 
# Description: 
# ---------------------------------------------------------------------------
# Import arcpy module
import arcpy
arcpy.env.overwriteOutput = True
toolbox = ("E:/MDTAPPS/interactive/refmseg.tbx")
# Load required toolboxes
arcpy.ImportToolbox("E:/MDTAPPS/interactive/refmseg.tbx")
# Script arguments
first_point = arcpy.GetParameterAsText(0)
if first_point == '#' or not first_point:
    first_point = "E:\\MDTAPPS\\interactive\\interactive.mdb\\first_point" # provide a default value if unspecified
end_point = arcpy.GetParameterAsText(1)
if end_point == '#' or not end_point:
    end_point = "E:\\MDTAPPS\\interactive\\interactive.mdb\\end_point" # provide a default value if unspecified
first_point__2_ = arcpy.GetParameterAsText(2)
if first_point__2_ == '#' or not first_point__2_:
    first_point__2_ = "E:\\MDTAPPS\\interactive\\interactive.mdb\\first_point" # provide a default value if unspecified
end_point__3_ = arcpy.GetParameterAsText(3)
if end_point__3_ == '#' or not end_point__3_:
    end_point__3_ = "E:\\MDTAPPS\\interactive\\interactive.mdb\\end_point" # provide a default value if unspecified
beg_point = arcpy.GetParameterAsText(4)
if beg_point == '#' or not beg_point:
    beg_point = "beg_point" # provide a default value if unspecified
end_pt = arcpy.GetParameterAsText(5)
if end_pt == '#' or not end_pt:
    end_pt = "end_pt" # provide a default value if unspecified
first_pt_loc = arcpy.GetParameterAsText(6)
if first_pt_loc == '#' or not first_pt_loc:
    first_pt_loc = "E:\\MDTAPPS\\interactive\\interactive.mdb\\first_pt_loc" # provide a default value if unspecified
end_pt_loc = arcpy.GetParameterAsText(7)
if end_pt_loc == '#' or not end_pt_loc:
    end_pt_loc = "E:\\MDTAPPS\\interactive\\interactive.mdb\\end_pt_loc" # provide a default value if unspecified
points = arcpy.GetParameterAsText(8)
if points == '#' or not points:
    points = "E:\\MDTAPPS\\interactive\\interactive.mdb\\points" # provide a default value if unspecified
segment = arcpy.GetParameterAsText(9)
if segment == '#' or not segment:
    segment = "segment" # provide a default value if unspecified
temp__2_ = arcpy.GetParameterAsText(10)
if temp__2_ == '#' or not temp__2_:
    temp__2_ = "E:\\MDTAPPS\\interactive\\interactive.mdb\\temp" # provide a default value if unspecified
# Local variables:
Output_Event_Table_Properties = "RID POINT MEAS"
Output_Event_Table_Properties__2_ = "RID POINT MEAS"
MDTGIS_ROUTES_LRM_RM = "MDTGIS.ROUTES_LRM_RM"
MDTGIS_ROUTES_LRM_DC_MI = "MDTGIS.ROUTES_LRM_DC_MI"
MDTGIS_ROUTES_LRM_DC_MI__2_ = "MDTGIS.ROUTES_LRM_DC_MI"
interactive_mdb = "E:\\MDTAPPS\\interactive\\interactive.mdb"
MDTGIS_ROUTES_LRM_DC = "MDTGIS_ROUTES_LRM_DC"
temp = "E:\\MDTAPPS\\interactive\\interactive.mdb\\temp"
# Process: Calculate Field
arcpy.CalculateField_management(first_point, "Corridor", "str(!Corridor!) + \"_\" + str(!RM!)", "PYTHON_9.3", "")
# Process: Calculate Field (2)
arcpy.CalculateField_management(end_point, "Corridor", "str(!Corridor!) + \"_\" + str(!RM!)", "PYTHON_9.3", "")
# Process: Make Route Event Layer (2)
arcpy.MakeRouteEventLayer_lr(MDTGIS_ROUTES_LRM_RM, "ROUTE_ID", end_point__3_, "Corridor POINT end_offset", end_pt, "", "ERROR_FIELD", "NO_ANGLE_FIELD", "NORMAL", "ANGLE", "LEFT", "POINT")
# Process: Make Route Event Layer
arcpy.MakeRouteEventLayer_lr(MDTGIS_ROUTES_LRM_RM, "ROUTE_ID", first_point__2_, "Corridor POINT beg_offset", beg_point, "", "ERROR_FIELD", "NO_ANGLE_FIELD", "NORMAL", "ANGLE", "LEFT", "POINT")
# Process: Locate Features Along Routes
arcpy.LocateFeaturesAlongRoutes_lr(beg_point, MDTGIS_ROUTES_LRM_DC_MI, "CORRIDOR", "0 Meters", first_pt_loc, Output_Event_Table_Properties, "ALL", "NO_DISTANCE", "ZERO", "NO_FIELDS", "NO_M_DIRECTION")
# Process: Locate Features Along Routes (2)
arcpy.LocateFeaturesAlongRoutes_lr(end_pt, MDTGIS_ROUTES_LRM_DC_MI__2_, "CORRIDOR", "0 Meters", end_pt_loc, Output_Event_Table_Properties__2_, "FIRST", "NO_DISTANCE", "ZERO", "NO_FIELDS", "NO_M_DIRECTION")
# Process: Append
arcpy.Append_management("E:\\MDTAPPS\\interactive\\interactive.mdb\\first_pt_loc;E:\\MDTAPPS\\interactive\\interactive.mdb\\end_pt_loc", temp, "", "RID \"CORRIDOR\" true true false 10 Text 0 0 ,First,#,E:\\MDTAPPS\\interactive\\interactive.mdb\\first_pt_loc,RID,-1,-1,E:\\MDTAPPS\\interactive\\interactive.mdb\\end_pt_loc,RID,-1,-1;beg_offset \"beg_offset\" true true false 8 Double 0 0 ,First,#;end_offset \"end_offset\" true true false 8 Double 0 0 ,First,#", "")
# Process: Table to Table
arcpy.TableToTable_conversion(temp__2_, interactive_mdb, "points", "", "RID \"CORRIDOR\" true true false 10 Text 0 0 ,First,#,E:\\MDTAPPS\\interactive\\interactive.mdb\\temp,RID,-1,-1;beg_offset \"beg_offset\" true true false 8 Double 0 0 ,First,#,E:\\MDTAPPS\\interactive\\interactive.mdb\\temp,beg_offset,-1,-1;end_offset \"end_offset\" true true false 8 Double 0 0 ,First,#,E:\\MDTAPPS\\interactive\\interactive.mdb\\temp,end_offset,-1,-1", "")
# Process: Make Route Event Layer (3)
arcpy.MakeRouteEventLayer_lr(MDTGIS_ROUTES_LRM_DC, "CORRIDOR", points, "rid LINE beg_offset end_offset", segment, "", "ERROR_FIELD", "NO_ANGLE_FIELD", "NORMAL", "ANGLE", "LEFT", "POINT")
# Process: Script1
arcpy.Script1_ref()
0 Kudos
10 Replies
JamesCrandall
MVP Frequent Contributor

You must add the parameters to the Toolbox script.  Right-click the Toolbox script and open the Properties dialog to find the Parameters tab.

0 Kudos
TerryGustafson
Occasional Contributor II

That is exactly what I was looking for.. My first_point is a table so would the data type be table for the parameter?

0 Kudos
JamesCrandall
MVP Frequent Contributor

From your example code it looks like you are just passing in the path to the table, so... maybe the parameter should just be a string type?

0 Kudos
TerryGustafson
Occasional Contributor II

My end goal it to add this service to a WAB widget and have the user enter the information from the widget and then it passes the data to the service..

0 Kudos
TerryGustafson
Occasional Contributor II

I spoke to soon, I thought I could set those parameters once the GP service was created. If I set them in the script before I run it to publish it, it crashes.. I set them to string..

Traceback (most recent call last):

File "C:\Users\U0654\Desktop\new_seg4.py", line 29, in

first_point__2_ = arcpy.GetParameterAsText(2)

File "E:\MDTAPPS\Desktop10.2\arcpy\arcpy\__init__.py", line 632, in GetParameterAsText

return gp.getParameterAsText(index)

File "E:\MDTAPPS\Desktop10.2\arcpy\arcpy\geoprocessing\_base.py", line 224, in getParameterAsText

self._gp.GetParameterAsText(*gp_fixargs(args, True)))

RuntimeError: Object: Error in getting parameter as text

0 Kudos
JamesCrandall
MVP Frequent Contributor

Did you set a parameter for  first_point__2_?

0 Kudos
TerryGustafson
Occasional Contributor II

thanks for your help, I have been struggling learning this..  I did look at the parameters in the model and it gave me an example so going to try this..

0 Kudos
TerryGustafson
Occasional Contributor II

I don't know if its going to work as the only reason I made some of those parameters was to get the model to export the variables correctly so it would run.  when I run it through the model with only the first_point and the end_point as parameters and generate the GP service and run it this is the dialog that shows..

0 Kudos
TerryGustafson
Occasional Contributor II

I’m thinking the segment would have an output parameter? So the direction would be output? In the parameter properties?

0 Kudos