Input parameter - ListRasters

1399
8
Jump to solution
04-05-2018 09:53 AM
ChrissyWhite
New Contributor III

I am pretty new to python and have come across a problem that I am not understanding how to get around.

I am building a custom tool in ArcMap and it has an input form. On that form, one of the parameters is a multivalue list of rasters.

My python script is receiving that input parameter:

      inRasterB = arcpy.GetParameterAsText(3).split(';')

without any problems. I get a perfect, comma-delimited list.

However, I need to loop through each of these individual rasters the user has provided. I originally had it setup with a ListRasters(inRasterB), but when I started getting results for every raster in my geodatabase, I realized that this isn't going to work because the list is being used as the wildcard, which obviously isn't going to match.

I tried changing my loop to iterate through the input directly:

    #rasters = sorted(arcpy.ListRasters(inRasterB)) #original code
    #for raster in rasters: #original code
    for raster in inRasterB: #new code

But now, it isn't recognizing those inputs as a list of rasters, but rather just a list of files (and can't find them on the filesystem due to the way geodatabases contain the rasters).

So........any thoughts on how I can go about getting a specific list of rasters from the user and looping through those rasters, and only those rasters, rather than every raster in the directory/gdb?

0 Kudos
1 Solution

Accepted Solutions
JamesCrandall
MVP Frequent Contributor

Flip the search around and iterate the list of raster names and paths to evaluate, use os.path.basename to strip the raster name from the full path name.  Probably a much more elegant way to go about it.

#replicate a list of raster names
raslist = ['E:\blah\blah\mygdb.gdb\TDSDiff_y2013m288_y2013m12_111','E:\blah\blah\mygdb.gdb\TDSDiff_y2013m288_y2013m218_111','E:\blah\blah\mygdb.gdb\TDSDiff_y2040m218_y2013m218_111']

#specify the workspace where the rasters are located
ws = r'H:\RasterExport\ras.gdb'
arcpy.env.workspace = ws

#set a list of rasters in that workspace
rasters = arcpy.ListRasters('*')

#iterate the list of rasters and evaluate whether or not the 3 rasters in the list exist in the workspace
for rasternameAndPath in raslist:
    rasterName = os.path.basename(rasternameAndPath)
    if rasterName in rasters:
        print rasterName

View solution in original post

8 Replies
DanPatterson_Retired
MVP Emeritus

Did you check the multivalue parameter options?  For any parameter, it can be set so that the list of raster comes from the geodatabase, but the user can select more than one from the list

0 Kudos
JamesCrandall
MVP Frequent Contributor
#replicate a list of raster names
raslist = ['TDSDiff_y2013m288_y2013m12_111','TDSDiff_y2013m288_y2013m218_111','TDSDiff_y2040m218_y2013m218_111']

#specify the workspace where the rasters are located
#in my test, this workspace contains 6 rasters in total includes 3 in replicated list above
ws = r'H:\ras.gdb'
arcpy.env.workspace = ws

#set a list of rasters in that workspace
rasters = arcpy.ListRasters('*')

#iterate the list of rasters and evaluate whether or not the 3 rasters in the list exist in the workspace
for raster in rasters:
    if raster in raslist:
        print raster
ChrissyWhite
New Contributor III

That sounds perfect. I will give that a go shortly. Thanks a ton!

0 Kudos
ChrissyWhite
New Contributor III

It didn't work. The list that comes through is like ['E:\blah\blah\mygdb.gdb\someraster1', 'E:\blah\blah\mygdb.gdb\someraster2', etc...]

And the listRasters() is coming back with just 'someraster1' or 'someraster2' (rather than the full path), so they aren't matching what's in the list. Maybe I need to split the list one additional time and pull out whatever characters are at the end up until the first '\'? Not sure how to go about that offhand, but does it sounds plausible.....?

0 Kudos
VinceAngelo
Esri Esteemed Contributor

List functions are always relative to the active workspace, so you'd need to os.path.join(ws,raster) to reconstruct the fully qualified path.

- V

JamesCrandall
MVP Frequent Contributor

Flip the search around and iterate the list of raster names and paths to evaluate, use os.path.basename to strip the raster name from the full path name.  Probably a much more elegant way to go about it.

#replicate a list of raster names
raslist = ['E:\blah\blah\mygdb.gdb\TDSDiff_y2013m288_y2013m12_111','E:\blah\blah\mygdb.gdb\TDSDiff_y2013m288_y2013m218_111','E:\blah\blah\mygdb.gdb\TDSDiff_y2040m218_y2013m218_111']

#specify the workspace where the rasters are located
ws = r'H:\RasterExport\ras.gdb'
arcpy.env.workspace = ws

#set a list of rasters in that workspace
rasters = arcpy.ListRasters('*')

#iterate the list of rasters and evaluate whether or not the 3 rasters in the list exist in the workspace
for rasternameAndPath in raslist:
    rasterName = os.path.basename(rasternameAndPath)
    if rasterName in rasters:
        print rasterName
ChrissyWhite
New Contributor III

That seems to have gotten me further, but now it appears that the basename line is acting odd. I put (how do you get that handy gray box for the code?):

    rasters = arcpy.ListRasters('*')
    arcpy.AddMessage("Processing Raster1 for " + str(rasters)) #help debug - This line looks fine, just a big list
    for rasterfull in inRasterBot:
        arcpy.AddMessage("Processing Raster2 for " + rasterfull) #help debug - This line looks fine, full path surrounded by single quotes
        raster = os.path.basename(rasterfull)
        arcpy.AddMessage("Processing Raster3 for " + raster) #help debug - This line looks bad, has a ' at the end of the raster name (UnitAB_DEM_bottom'). I am guessing it is because it is bringing everything after the last \ maybe?
        if raster in rasters:
            unit = raster.split('_')[0]
            arcpy.AddMessage("Processing Raster4 for " + unit) #help debug - Never makes it here

I am going to try to do something to remove that last ' on the basename line and see if that helps.

0 Kudos
ChrissyWhite
New Contributor III

I changed the basename line to:

os.path.basename(rasterfull)[0:-1]

And it seems to be working now. It is at least running and going through each of the rasters instead of immediately ending "successfully". Thanks for the help!!!! I really appreciate it.