Select to view content in your preferred language

Py 2.4 - using sys.argv with gp.Feature Class to Coverage

1456
11
02-07-2012 06:58 AM
DebraGajdos
Deactivated User
Hello everyone,

I am trying to put the gp tool into a script (see my post about os.walk and sys.argv). The proccessing tool looks like this in script form:

# Import system modules
import sys, string, os, arcgisscripting

# Create the Geoprocessor object
gp = arcgisscripting.create()

# Set the necessary product code
gp.SetProduct("ArcInfo")

# Load required toolboxes...
gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Conversion Tools.tbx")

# Script arguments..

##lines_shp = sys.argv[1]

lines_shp = "C:\\test\\lines.shp"

# Local variables...

# Process: Feature Class To Coverage...
gp.FeatureclassToCoverage_conversion("C:\\test\\lines.shp ARC", lines_cov, "", "DOUBLE")



This works when I run it in IDLE ver 1.1.1.

But when I try to use the sys.arg like this:

# Script arguments..

lines_shp = sys.argv [1]

##lines_shp = "C:\\test\\lines.shp"

# Local variables...

# Process: Feature Class To Coverage...
gp.FeatureclassToCoverage_conversion("lines_shp ARC", linesCopy_cov, "", "DOUBLE")




I get this error:

Traceback (most recent call last):
File "C:/scripts/FC to Cov script.py", line 29, in ?
gp.FeatureclassToCoverage_conversion("lines_shp ARC", linesCopy_cov, "", "DOUBLE")
NameError: name 'linesCopy_cov' is not defined
>>>


So I guess my question is, why does FC to Coverage only play nice if the path is hard coded into the gp line? I've written other scripts and used sys.argv with no problem...

Thanks in Advance.
Tags (2)
0 Kudos
11 Replies
DarrenWiens2
MVP Alum
I'm surprised that the first example works - in both examples your output coverage is an undefined variable (ie. you haven't said what either 'lines_cov' or linesCopy_cov' are). Also, are you trying to make a coverage or a shapefile as output (they are different things)?
0 Kudos
RDHarles
Regular Contributor
I agree with Darren that you haven't defined linesCopy_cov  --> but also the first argument should look like this:

lines_shp = sys.argv[1]
gp.FeatureclassToCoverage_conversion(lines_shp+" ARC", linesCopy_cov, "", "DOUBLE")
0 Kudos
curtvprice
MVP Alum
I think your code just needs a little debugging
lines_shp = sys.argv[1] # or: gp.getParameterAsText(0) (yes, 0)
linesCopy_cov = sys.argv[2] # or: gp.getParameterAsText(1) 

##lines_shp = "C:\\test\\lines.shp"

# Local variables...

# Process: Feature Class To Coverage...
gp.FeatureclassToCoverage_conversion(lines_shp + " ARC", linesCopy_cov, "", "DOUBLE")



GetParameterAsText() works better with more complex variables like coordinate systems, so I now use it instead of sys.argv. Except for sys.argv[0] which convenienely contains the path of the script (say, to help you find paths to associated scripts or tools).
0 Kudos
DebraGajdos
Deactivated User
Sorry, I didn't clean up the code properly. 

Ok, here it is from the very begining of the story...I start with Model Builder in ArcGIS 9.2.

I create a model for the input (lines.shp), the output (lines_cov) and the the gp tool (gp.FeatureclassToCoverage_conversion("# ARC", lines_cov, "", "DOUBLE").  I set the input and output features as Parameters.

When I run the Model from the Toolbox, I get a new coverage in my root directory.

I export the Model to Python:

# Import system modules
import sys, string, os, arcgisscripting

# Create the Geoprocessor object
gp = arcgisscripting.create()

# Set the necessary product code
gp.SetProduct("ArcInfo")

# Load required toolboxes...
gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Conversion Tools.tbx")

# Script arguments...
lines_shp = sys.argv[1]

lines_cov = sys.argv[2]

# Local variables...

# Process: Feature Class To Coverage...
gp.FeatureclassToCoverage_conversion("# ARC", lines_cov, "", "DOUBLE")



When I add the script to a Toolbox, set the parameters, and then run it from Toolbox, I get this error in the dialog window:

Executing (OnlyFC2Cov_8): OnlyFC2Cov C:\test\lines.shp C:\test\lines_cov
Start Time: Tue Feb 07 16:36:06 2012
Running script OnlyFC2Cov...
Error in script OnlyFC2Cov.
Error in executing: cmd.exe /C Q:\scratch\FC_Cov.py  "C:\test\lines.shp" "C:\test\lines_cov"

Failed to execute (OnlyFC2Cov_8).
End Time: Tue Feb 07 16:36:06 2012 (Elapsed Time: 0.00 seconds)


Now, what could be the difference between running this in Model Builder and running as a script?

Eventually, this will be incorporated into a script that uses os.walk to go through a directory tree.
Any help would be appreciated.
0 Kudos
RDHarles
Regular Contributor
Only thing that looks wrong to me is to change this line to:

gp.FeatureclassToCoverage_conversion(lines_shp+" ARC", lines_cov, "", "DOUBLE")
0 Kudos
DebraGajdos
Deactivated User
Holy Moly!

I would have never guessed to use the "+"!  That does not seem intuitive to me. 

Thank you very much!
0 Kudos
DarrenWiens2
MVP Alum
Now, what could be the difference between running this in Model Builder and running as a script?

The difference is that the Model Builder -> Script translator hardly ever works exactly right...
0 Kudos
curtvprice
MVP Alum

Eventually, this will be incorporated into a script that uses os.walk to go through a directory tree.
Any help would be appreciated.


I know you're obviously using 9.3, but I want to add to the thread that you can do "os.walk" type things with a model easily with ModelBuilder 10.0 iterator tools. It's one of the things that make 10.0 worth the hassles.

But even in 9.3, if you have a working model, you can call the model it from a python script -- no need to export/edit in Python:

... os.walk code ...
    gp.AddToolbox(r"x:\path\to\my\toolbox.tbx")
    gp.MyModelTool_mytools(arg1,arg2)
0 Kudos
RDHarles
Regular Contributor
Holy Moly!

I would have never guessed to use the "+"!  That does not seem intuitive to me. 

Thank you very much!


It's simply something that's required when you combine a variable (lines_shp) with a string (" ARC").
0 Kudos