Tuesday 17 October 2017

arcpy - Getting full path of layer selected in drop-down box for ArcGIS Python script tool?


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?



Answer



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


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...