How can I successfully use GetParameterAsText in ArcPy?

11182
11
06-09-2011 10:49 AM
BobbySaleh
New Contributor
I cannot get this code to work. Admittedly, I am new to programming with ArcPy.

I am trying to use the GetParameterAsText to ask the user to give input (to let them choose which featureclass they want to use) from the script. I keep getting the message : "Runtime error <class 'arcgisscripting.ExecuteError'>: ERROR 000735: Input Features: Value is required"

I built this using Model Builder and then exported the model to python to look at the code to start customizing what I want it to do. I cannot get the box to come up asking the user to choose which featureclass to run the script/model on.


[INDENT]CODE:
import arcpy

print "Start time = " + time.asctime()

# Script arguments
Expression = arcpy.GetParameterAsText(0)
if Expression == '#' or not Expression:
Expression = "\"Segment_ID\" In ('C1', 'C2', 'C3', 'C4', 'C5')" # provide a default value if unspecified
Input_Features = arcpy.GetParameterAsText(1)

# Local variables:
Test = Expression
Test_UnsplitLine = Test
ROUTE_FINAL = Test_UnsplitLine
ROUTE_FINAL__2_ = ROUTE_FINAL
Route_Real_OUTPUT_gdb = "G:\\40\\Scratch\\Real_OUTPUT.gdb"

# Process: Feature Class to Feature Class
tempEnvironment0 = arcpy.env.scratchWorkspace
arcpy.env.scratchWorkspace = "G:\\40\\Scratch\\Route_Scratch_Work.gdb"
empEnvironment1 = arcpy.env.outputCoordinateSystem
arcpy.env.outputCoordinateSystem = "PROJCS['NS_Equidistant_Conic',GEOGCS['GCS_North_American_1983',DATUM['D_North_American_1983',SPHEROID['GRS_1980',6378137.0,298.257222101]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Equidistant_Conic'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',-154.0],PARAMETER['Standard_Parallel_1',68.0],PARAMETER['Standard_Parallel_2',74.0],PARAMETER['Latitude_Of_Origin',40.0],UNIT['Foot_US',0.3048006096012192]]"
tempEnvironment2 = arcpy.env.geographicTransformations
arcpy.env.geographicTransformations = "NAD_1983_To_WGS_1984_1"
tempEnvironment3 = arcpy.env.workspace
arcpy.env.workspace = "G:\\40\\Scratch\\Route_Scratch_Work.gdb"
arcpy.FeatureClassToFeatureClass_conversion(Input_Features, Real_OUTPUT_gdb, "Test", Expression, "", "")
arcpy.env.scratchWorkspace = tempEnvironment0
arcpy.env.outputCoordinateSystem = tempEnvironment1
arcpy.env.geographicTransformations = tempEnvironment2
arcpy.env.workspace = tempEnvironment3
# Process: Unsplit Line
arcpy.UnsplitLine_management(Test, Test_UnsplitLine, "Segment_ID", "")
# Process: Create Routes
arcpy.CreateRoutes_lr(Test_UnsplitLine, "Segment_ID", ROUTE_FINAL, "LENGTH", "", "", "UPPER_LEFT", "1", "0", "IGNORE", "INDEX")
# Process: Add Field
arcpy.AddField_management(ROUTE_FINAL, "RID", "TEXT", "", "", "70", "", "NULLABLE", "NON_REQUIRED", "")

print "End Time =" + time.asctime()[/INDENT]


I think that showing all of this code is a bit overwhelming - I really just need to know how to call a user interface box to ask the user to choose which featureclass to perform the model on.

Any help is greatly appreciated!
Bobby
Tags (2)
0 Kudos
11 Replies
JakeSkinner
Esri Esteemed Contributor
Add your customized script to a Toolbox by right-clicking on the toolbox > Add > Script.  Then right-click on the script > Properties and go to the Parameters tab.  Here is where you can set up the parameters for the 'GetParametersAsText'. 

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00150000000n000000.htm
BobbySaleh
New Contributor
Thanks so much for the help Jake, that got me started!

My issue now has something to do with the code not knowing how to find the 'Test' featureclass that is produced from the arcpy.FeatureClassToFeatureClass_conversion. The inability to reference this featureclass that is created ('Test') is what is preventing the code from being able to perform the unsplit line. The code is reading the variable 'Test' for the Unsplit line and Test is assigned to Expression. ?!?

The results box looks like this...

Running script Test2...
<class 'arcgisscripting.ExecuteError'>: Failed to execute. Parameters are not valid.
ERROR 000732: Input Features: Dataset "Segment_ID" In ('C1', 'C2', 'C3', 'C4', 'C5') does not exist or is not supported
ERROR 000670: output Output Feature Class is same as input Input Features
Failed to execute (UnsplitLine).

Failed to execute (Test2).
Failed at Mon Jun 13 09:37:38 2011 (Elapsed Time: 22.00 seconds)


Can anyone help me figure out how to solve this problem?

Thanks so much!
0 Kudos
JakeSkinner
Esri Esteemed Contributor
For your local variables you have:

Test = Expression
Test_UnsplitLine = Test

The 'Unsplit Line' tool will be looking for feature classes and not variables.  Try changing the line to:

arcpy.UnsplitLine_management("Test", "Test_UnsplitLine", "Segment_ID", "")


The quotes will indicate that 'Test' and 'Test_UnsplitLine" are feature classes rather than variables.
0 Kudos
BobbySaleh
New Contributor
Thanks again Jake.

Here is the error message I am getting after adding the quotes...

--------
Running script Test2...
<class 'arcgisscripting.ExecuteError'>: Failed to execute. Parameters are not valid.
ERROR 000732: Input Features: Dataset Test does not exist or is not supported
Failed to execute (UnsplitLine).

Failed to execute (Test2).
Failed at Mon Jun 13 17:24:58 2011 (Elapsed Time: 24.00 seconds)
-----------

The strange part is that I can see the featureclass 'Test' in the GDB...but for some reason the code doesn't see it.

Any suggestions?

Thanks so much - I really need the help.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
I'm a little confused on your local variables:

# Local variables:
Test = Expression
Test_UnsplitLine = Test
ROUTE_FINAL = Test_UnsplitLine
ROUTE_FINAL__2_ = ROUTE_FINAL
Route_Real_OUTPUT_gdb = "G:\\40\\Scratch\\Real_OUTPUT.gdb"


It appears you have variables set to other variables.  Could you explain what you would like the code to do and I can figure out the correct syntax?  Thanks!
0 Kudos
BobbySaleh
New Contributor
Jake,

I am trying to get my code to do a few simple things.

First, it will take input from the user about which adjacent segments will be used to form a route (SQL expression listing segments) - and from what fc.  The code will take those segments and save out a fc (while re-projecting it in the output setting).  At the moment, I am making a permanent fc.  I would rather have this fc be a temporary fc.  At any rate, the code will perform an Unsplit line which will merge the segments into a single-part line fc.  The code should then create a final route from the resultant unsplit line fc.  Next, I want to add 2 text fields to the final fc.  The last step (which I do not yet have in my code) is to update the values in these fields.  One field will need to be updated with the name of the featureclass used to create the route and the other field will need to be updated with the segments used to create the route (I was going to try and grab that from the sql expression the user enters in the beginning).

I agree with your last comment about using variables to set other variables.  The reason this happened is simply because after I made my model (which successfully did everything I just mentioned - with the exception of adding values to those fields), this is the script that was produced by ArcMap.  I was VERY confused by this logic when I saw it was the code - especially given that the model works FINE!

Thanks again...:)
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Can you upload your model as well?  This will help a lot.  Thanks!
0 Kudos
BobbySaleh
New Contributor
Jake,

Here is a graphic of the model. 

Thanks so much,
Bobby
0 Kudos
BobbySaleh
New Contributor
Jake,

I re-visited what I am trying to do and came up with this script instead.

import arcpy
arcpy.env.workspace = "G:\\Scratch\\Route_Scratchwork\\Route_Real_OUTPUT.gdb"

# Script arguments
Expression = arcpy.GetParameterAsText(0)
Input_Features = arcpy.GetParameterAsText(1)

arcpy.MakeFeatureLayer_management(Input_Features ,"route_interim",Expression)
arcpy.Project_management ("route_interim", "route_projected", "PROJCS['NS_Equidistant_Conic',GEOGCS['GCS_North_American_1983',DATUM['D_North_American_1983',SPHEROID['GRS_1980',6378137.0,298.257222101]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Equidistant_Conic'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',-154.0],PARAMETER['Standard_Parallel_1',68.0],PARAMETER['Standard_Parallel_2',74.0],PARAMETER['Latitude_Of_Origin',40.0],UNIT['Foot_US',0.3048006096012192]]"
, "NAD_1983_To_WGS_1984_1")
arcpy.UnsplitLine_management("route_projected", "route_unsplitLine", "Segment_ID", "")
arcpy.CreateRoutes_lr("route_unsplitLine", "Segment_ID", "ROUTE_FINAL", "LENGTH", "", "", "UPPER_LEFT", "1", "0", "IGNORE", "INDEX")
arcpy.AddField_management("ROUTE_FINAL", "RID", "TEXT", "", "", "70", "", "NULLABLE", "NON_REQUIRED", "")


Everything works fine with no errors.  However, The SQL expression that I used did not properly execute so that only those selected features are made into fc "route_interim".  I am not sure what is going wrong.  I also need a little help trying to take the SQl string and parse out the segments so that I can write them as a value into a field.
0 Kudos