Select to view content in your preferred language

Python: parameters, launching from vba code, and more

5539
11
10-29-2010 12:48 PM
LornaMurison
Regular Contributor
Hello everyone,
Very new to python.
I have VBA code that should eventually allow a user to input a dataset (watersheds) and launch python scripts that will calculate various attributes of those watersheds.

The first script I am working on calculates the area of different land cover classes within each watershed by  A) intersect watersheds and landcover (both vector) B) Dissolve based on unique watershed and land cover IDs C) export the attribute table.

1. How do I launch a python script from a VBA code?
2. How do I set up the parameters for the script?  i.e. get the watersheds and land cover from the vba code.

If you could point me towards websites/books/anything that may help I would appreciate it, or offer your own insight :).

Thanks
0 Kudos
11 Replies
HugoAhlenius
Deactivated User
You should be able to launch the python.exe executable, like you launch any program in VBA. Then pass the script path/name as the first parameter, and then additional parameters.

On the Python side, import sys and look at sys.argv for the arguments/parameters. You can find more information about that in your favorite manual/documentation - just look for argv!
0 Kudos
LornaMurison
Regular Contributor
ArcGIS help for 9.3 recommends using gp.GetParameterAsText() instead of sys.argv[] so I am trying to use that.  Unless there is a particular reason why one is better than the other.

Below is what I have and although the errors must be painfully obvious to most of you, I cannot figure out how to get the parameter syntax right.
It is the output I am having trouble with.
Thanks!

import arcgisscripting
gp = arcgisscripting.create(9.3)
Catchments = gp.GetParameterAsText(0)
LandCover = gp.GetParameterAsText(1)
Output = gp.GetParameterAsText(2)
gp.Toolbox = "Analysis"
gp.Intersect ("LandCover; Catchments", "Output + 'output.shp'", "ALL", "", "INPUT")
0 Kudos
ChrisSnyder
Honored Contributor
FYI:

gp.GetParameterAsText() is used only when getting arguments from an ArcToolbox tool interface

sys.argv[] is used when getting arguments from another "non-toolbox" source. For example:

#master script (calls 100 instances of "subprocess.py"
subProcessScript = r"C:\temp\subprocess.py"
pythonExePath = r"C:\Python25\python.exe"
rootDir = r"D:\temp"
for tileNumber in range (1,101):
   parameterList = [pythonExePath, subProcessScript, rootDir, str(tileNumber)]
   os.spawnv(os.P_WAIT, pythonExePath, parameterList)

#subprocess program (subprocess.py)
rootDir = sys.argv[1]
tileNumber = sys.argv[2]
os.mkdir(rootDir + "\\tile_" + str(tileNumber)
0 Kudos
ChrisSnyder
Honored Contributor
Is Output = gp.GetParameterAsText(2) a featureclass output?

If so:

gp.Intersect ("LandCover; Catchments", Output, "ALL", "", "INPUT") #don't put quotes around the variable because if you do, it just becomes a string...

If Output is the directory where "output.shp" will be stored, then:

gp.Intersect ("LandCover; Catchments", Output + "\\output.shp", "ALL", "", "INPUT")

You might want to consider making a toolbox GUI, then there is easy control over the "types" of parameters (e.g. Folder vs. a FeatureClass vs. a Raster).
0 Kudos
LornaMurison
Regular Contributor
Thanks for your help,

It's supposed to be a feature class output.  But... if i set the script parameter as an output, it no longer shows up on the tool dialog.

I still get the following error:

<class 'arcgisscripting.ExecuteError'>: Failed to execute. Parameters are not valid.
ERROR 000732: Input Features: Dataset Catchments #;LandCover # does not exist or is not supported
ERROR 000735: Output Feature Class: Value is required
Failed to execute (Intersect).


import arcgisscripting
gp = arcgisscripting.create(9.3)
Catchments = gp.GetParameterAsText(0)
LandCover = gp.GetParameterAsText(1)
Output = gp.GetParameterAsText(2)
gp.Toolbox = "Analysis"
gp.workspace = "F:\Hydrology_Tools\Data"
gp.Intersect ("Catchments; LandCover", Output, "ALL", "", "INPUT")
0 Kudos
ChrisSnyder
Honored Contributor
your code of:

gp.Intersect ("Catchments; LandCover", Output, "ALL", "", "INPUT")

should be:

gp.Intersect (Catchments + ";" LandCover, Output, "ALL", "", "INPUT")

When you used "Catchments; LandCover"  in your code, you are using the string equivalent instead of the variables (formatted as a string in order to be used in the tool).
0 Kudos
LornaMurison
Regular Contributor
That makes sense...
I was looking at an example that got the files from the string path name.

Now I get this error:

<type 'exceptions.SyntaxError'>: invalid syntax (<string>, line 7)
Failed to execute (Intersect).

import arcgisscripting
gp = arcgisscripting.create(9.3)
Catchments = gp.GetParameterAsText(0)
LandCover = gp.GetParameterAsText(1)
Output = gp.GetParameterAsText(2)
gp.Toolbox = "Analysis"
gp.Intersect (Catchments + ";" LandCover, Output, "ALL", "", "INPUT")
0 Kudos
ChrisSnyder
Honored Contributor
Here's how I would do it:

import arcgisscripting
gp = arcgisscripting.create(9.3)
gp.overwriteoutput = True
catchmentsFC = gp.GetParameterAsText(0) #in a toolbox GUI, this should be Type = FeatureLayer
landCoverFC = gp.GetParameterAsText(1) #in a toolbox GUI, this should be Type = FeatureLayer
outputFC = gp.GetParameterAsText(2) #in a toolbox GUI, this should Type = FeatureClass
gp.Intersect_Analysis (catchmentsFC + ";" landCoverFC, outputFC, "ALL", "", "INPUT")
0 Kudos
LornaMurison
Regular Contributor
I used the exact code you posted.
I still got the same error:
<type 'exceptions.SyntaxError'>: invalid syntax (<string>, line 7)
Failed to execute (Intersect).

The parameters in the Toolbox GUI are Feature Layer Input, Feature Layer Input, Feature Class Output.
0 Kudos