Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have a Python script-based tool in ArcGIS. One of the parameters for the tool is an input raster file, which I've set (using the properties of the tool within the toolbox) to be a Raster Layer. This means that when I execute the tool I get the option to select that parameter from a dropdown list of currently loaded raster layers, or by navigating to find a new raster layer within the filesystem.

I am then using the standard arcpy.GetParameterAsText(0) code to get the parameter into my Python script. This works fine when I select a file by browsing through the filesystem, as the text in the dialog is the full path to the file, but when I just select from the dropdown list the text I get back is just the filename (eg. file.tif).

The code I am running needs to know the full path to the file - how do I get this?

If I could assume that the file was always in the workspace then I could append the filename to arcpy.env.workspace, but I can't assume that. Do I need to iterate through all of the layers that are loaded until I find one with the same name, and then find its full path, or is there an easier way?

share|improve this question
3  
I'm thinking that what you are choosing from the pulldown list is a layer name from your TOC (which happens to be the same as its filename). As an idea perhaps you can iterate through ListLayers to find that layer name and once found access its dataSource property to get the workspacePath and the datasetName properties combined. –  PolyGeo Aug 23 '12 at 11:09
    
Why do you need the full path? This important part of the question might help someone answer your question with a work around. –  Michael Markieta Aug 23 '12 at 14:43
    
@MichaelMarkieta: I'm running some code in my Python script that is using a completely separate library (outside of anything that arcpy provides) and that requires the full path of the input file for it to do its processing. –  robintw Aug 23 '12 at 14:47
add comment

3 Answers 3

up vote 5 down vote accepted

I had the same issue a while back. It's a pretty easy fix, just use the the describe tool.

Your already getting the layer name from your parameters. So all you have to do is describe the layer, find the path then merge the two.

layer = arcpy.GetParameterAsText(0)
desc = arcpy.Describe(layer)
path = desc.path
layersource = str(path) + "/" + layer

That should do it no problem.

Hope this helps

share|improve this answer
add comment

If you want to account for the possibility that the user might specify a raster within the filesystem:

from os.path import split, join
layer = arcpy.GetParameterAsText(0)

#Check if there is a path on the input parameter. If not, prepend the path.
if not split(layer)[0]:
    layer = join(arcpy.Describe(layer).path, "{}.tif".format(layer))
share|improve this answer
add comment

There is a little bit shorter way to do this also. describe data objects have a catalogPath property which is the full path to the file.

With the 10.1 version we're using, you can do:

layer = arcpy.GetParameterAsText(0)
desc = arcpy.Describe(layer)
layersource = desc.catalogPath
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.