How to import a .txt into a custom python tool as a variable and use the variable as vertical factor of path distance tool

4027
7
04-17-2015 01:31 PM
AaronSchroeder2
New Contributor

I am building a custom python tool in ArcMap 10.2.2 and one of the Input Variables needs to be Tobler's Hiking Function as a .txt which will be used as the Vertical Factor in the Path Distance tool. So the input Parameter(right-click the tool > properties > parameter tab) is Table and the interface allows me to select the .txt file just fine. I then have been trying to get the .txt into a variable using

Tobler_input = UTILS.getTextParamer(0) and then insert the variable into the path distance tool as a Table vertical factorbut it says "Unable to open file" and then gives the path location up to where the .txt file is located.

Here is basically what the code for that part is.

import arcpy

import SSUtilities as UTILS

from arcpy.sa import *

arcpy.CheckOutExtension("spatial")

#------------------------------------------------

Tobler_input = UTILS.getTextParamer(0)

arcpy.gp.PathDistance_sa(LKL_input, PathDist_output, LCover_aoi_reclass, DEM_aoi_1, "", "BINARY 1 45", DEM_aoi_2, "TABLE Tobler_input", "", Output_backlink_raster)

I am not sure what kind of syntax needs to go into the Vertical Factor parameter for it to recognize the variable that is the .txt file with Tobler's hiking function in it.

(the code works just fine if I hard code the .txt file location in the Vertical Factor parameter. ie: C:\\user\\folder\\folder\\Tober_function.txt)

0 Kudos
7 Replies
JamesCrandall
MVP Frequent Contributor

I'm not entirely certain, but your input parameter is inside of the quotes:

arcpy.gp.PathDistance_sa(LKL_input, PathDist_output, LCover_aoi_reclass, DEM_aoi_1, "", "BINARY 1 45", DEM_aoi_2, "TABLE Tobler_input", "", Output_backlink_raster)

Maybe just set that as:

arcpy.gp.PathDistance_sa(LKL_input, PathDist_output, LCover_aoi_reclass, DEM_aoi_1, "", "BINARY 1 45", DEM_aoi_2, Tobler_input, "", Output_backlink_raster)

0 Kudos
AaronSchroeder2
New Contributor

The example I put "TABLE Tobler_input" was just the last thing I tried so I figured just to put it as an example. I tried what you said with just the Tobler_input as the parameter but that also give a similar error where it says 'unable to open file' but has the variable as the file name instead of the files actual name. The funny thing is when I make the path distance tool in model builder with the same parameters and then export it to python script, it works and gives the result I want but what it puts for the vertical factor parameter is the entire path name with the TABLE word in front within quotes. It looks like this: "TABLE C:\\user\\folder\\folder\\Tobler_function.txt". I don't understand why it has TABLE in the quotes but it seems to be an issue when I try to use a variable like Tobler_input instead of the path name in quotes. It also may be worth noting that the custom input parameter UTILS.getTextParamer(0) may not be the one I want. Initially I was using arcpy.GetParameterAsText but after cross referencing a few other custom tools importing a .txt or table file, they all used UTILS.getTextParamer so I figured I would give it a shot. Any ideas on that?

0 Kudos
JamesCrandall
MVP Frequent Contributor

Yeah, I don't know what the SSUtilities library does.  Can you print out the variable?

Tobler_input = UTILS.getTextParamer(0)
print Tobler_input
0 Kudos
AaronSchroeder2
New Contributor

I'm not actually sure how to get the print statement to work with a custom parameter. Cause PyScripter doesn't prompt for an input so the UTILS.getTextParameter(0) is useless without an ArcMap interface. Do you know how to run it? or do you want me to send you the toblers function .txt file I am trying to use?

0 Kudos
ShaunWalbridge
Esri Regular Contributor

In many cases where a geoprocessing tool requires some more complex type, there is an equivalent class to create an object of that type, usually documented in the type parameter information. The Vertical Factor parameter expects an object derived from one of the Verticlal Factor types. In your case, you should be able to use the VfTable type:

import arcpy

vertical_factor = arcpy.sa.VfTable("c:/path/vffile.txt")
out_path_dist = arcpy.sa.PathDistance("source.shp", "costraster", "", "", "", "",
                          vertical_factor)
out_path_dist.save("C:/workspace/path_dist_by_vf.tif")

Let us know if that does the trick, or if you have any further questions.

Cheers,

Shaun

0 Kudos
AaronSchroeder2
New Contributor

So I understand that part. The only thing left to bring it all together is instead of using "c:/path/vffile.txt". - use a variable that is assigned for the file. So if we use your example, this is what I want it to look like(this doesn't work):

  1. import arcpy 
  2. Toblers_table = arcpy.getParamter(0)
  3. vertical_factor = arcpy.sa.VfTable(Toblers_table
  4. out_path_dist = arcpy.sa.PathDistance("source.shp", "costraster", "", "", "", "",vertical_factor)
  5. out_path_dist.save("C:/workspace/path_dist_by_vf.tif")

I am not sure what function to use for the 2nd line of code. I have tried that^ and

Toblers_table = arcpy.getParameterAsText(0)

Toblers_table = UTILS.getTextParameter(0).

I was also thinking if there was a function to get the path name of the file so I could just put that in the vertical factor section of the path distance tool. I am just trying to avoid having to code in the "c/apth/vfile.txt".

0 Kudos
ShaunWalbridge
Esri Regular Contributor

When you use arcpy.getParameter(0), you're asking for an object that contains the parameter, not the value of the parameter itself. If you create a parameter with a File datatype, you can instead use the 'GetParameterAsText' call if you'd like the value back as a string. It's hard to tell what you're trying to do specifically since we don't have the toolbox or the utilities module, but I've posted a basic example using a Python toolbox here that works:

support/tobler.pyt at master · EsriOceans/support · GitHub

If you check out that repository, it includes sample data to show how it works. Note that with a Python toolbox, we use the parameters[0].valueAsText, but it works the same way as getting the parameter as text would.

0 Kudos