0

I have a year of daily raster datasets (365) that I would like to use the Cell Statistics tool on; however, each raster is located in a separate folder so I cannot add all of them at once.

ie:

-raster_0001
       -raster_0001
            dblbnd.adf
            hdr.adf
            prj.adf
            sta.adf
            w001001.adf
            w001001x.adf
       -info
            arc.dir
            arc0000
            arc0000.nit
            arc0001
            arc0001.nit
        raster_0001.aux
-raster_0002
       -raster_0002
            dblbnd.adf
            hdr.adf
            prj.adf
            sta.adf
            w001001.adf
            w001001x.adf
       -info
            arc.dir
            arc0000
            arc0000.nit
            arc0001
            arc0001.nit
        raster_0002.aux

-raster_0003
       -raster_0003
            dblbnd.adf
            hdr.adf
            prj.adf
            sta.adf
            w001001.adf
            w001001x.adf
       -info
            arc.dir
            arc0000
            arc0000.nit
            arc0001
            arc0001.nit
        raster_0003.aux
...

-raster_0365
       -raster_0365
            dblbnd.adf
            hdr.adf
            prj.adf
            sta.adf
            w001001.adf
            w001001x.adf
       -info
            arc.dir
            arc0000
            arc0000.nit
            arc0001
            arc0001.nit
        raster_0365.aux

I have done a test and if I add a few rasters to a file geodatabase, then I can select these files all at once in the Cell Statistics tool.

My question: Is using python to add these rasters to a file geodatabase the best option? If yes, or no, how come and what would the code/workflow look like?

My guess is "Yes", and would be as simple as iterating through each folder using the "arcpy.RasterToGeodatabase_conversion" method described here raster_to_gdb...but I am not sure how that would look exactly? I am also not so sure that would be the best solution and thought it useful to ask the community for opinions.I would think this is a common problem for people: iterating through multiple folders to add content from each into a geodatabase (for example).

Is a geodatabase even the way to go? I haven't tried an approach using a mosaic raster set yet, not sure if that would work. Catalog? Another tool? Perhaps using Model Builder instead? Thoughts?

  • They are GRID raster. I'd just leave them as they are. If you want to get all the paths open a command prompt and type in dir /b/s <your path>\hdr.adf > Listing.txt then copy and paste into the tool... it's old school but it works. – Michael Stimson Jun 29 '15 at 4:54
1

You can run your cell statistics with all the rasters (GRIDs) as they are. Provided you haven't put them into folders that are too deep in the folder structure (more than 128 characters), but there are ways to get around even that:

In a command window type SUBST /?, this will show you the help for the subst command. You can set a 'fake' drive to a path deep in your folder structure, for example:

subst z: "C:\Program Files (x86)\Microsoft SQL Server Compact Edition\v3.5\Include"

Now Z drive is a fake drive pointing to the folder C:\Program Files (x86)\Microsoft SQL Server Compact Edition\v3.5\Include, if the 'to' path contains spaces you must surround it with quotes.

Arcpy has a walk function that will search the current folder and all subfolders, this can be restricted to just rasters and then just GRIDs. Using this you can build up the list needed for the inputs to CellStatistics then run that from python... here is an example for a tool that creates the mean:

import os, sys, arcpy

BaseDir = sys.argv[1]
OutStat = sys.argv[2]

# get the spatial analyst license (if not already checked out)
if arcpy.CheckExtension("Spatial") == "Available":
    arcpy.AddMessage("Checking out Spatial")
    arcpy.CheckOutExtension("Spatial")
else:
    arcpy.AddWarning("Unable to get spatial analyst extension")
    sys.exit(0)

AllRasters = [] # new, empty list
arcpy.AddMessage("Getting inputs")

for dirpath, dirnames, filenames in arcpy.da.Walk(BaseDir,datatype = "RasterDataset",type="GRID"):
    for ThisGRID in filenames:
        arcpy.AddMessage("Found %s" % (ThisGRID))
        AllRasters.append(os.path.join(dirpath,ThisGRID))

if len(AllRasters) > 1:
    arcpy.AddMessage("Finished searching, now calculating")
    MeanRaster = arcpy.sa.CellStatistics(AllRasters,"MEAN")
    arcpy.AddMessage("Saving")
    MeanRaster.save(OutStat)
else:
    arcpy.AddWarning("One or less rasters located")

# clean up
del MeanRaster
arcpy.CheckInExtension("Spatial")

You can create a toolbox and add this script as a tool; inputs Folder, output Raster Dataset.

0

You might try an OS Crawl while listing your rasters and running the CellStatistics and saving the result into the same location as the parent. I got the following to work on folders of .tiff rasters.

    import arcpy
    import os
    import math
    arcpy.CheckOutExtension("Spatial")
    from arcpy.sa import*
    import sys
    arcpy.env.overwriteOutput = True

    statistics_type ="MEDIAN" 
    ignore_nodata ="DATA"
    NewTextName = "CS_"

    FolderOrWorkspace = "C:\\Temp"
    print "======= START"

    for dirname, dirnames, filenames in os.walk(FolderOrWorkspace): #Iterate through folder
        for subdirname in dirnames:
            arcpy.env.workspace = os.path.join(dirname, subdirname) # Workspace set to folder
            print " "
            print " FOLDER: "+ subdirname
            RasterList = arcpy.ListRasters()
            print " RASTER LIST: "
            print RasterList
            print " "
            for MyRaster in RasterList:
                desc = arcpy.Describe(MyRaster)
                ActualName = desc.name
                NewName = (NewTextName+ActualName)
                print " Save Name: "+ NewName
                OutRaster = CellStatistics(MyRaster, statistics_type, ignore_nodata)
                print " Cell Statistics Have Run"
                NewRasterSavePath = os.path.join(dirname,subdirname,NewName)
                print " Saving: "+str(NewRasterSavePath)
                OutRaster.save(NewRasterSavePath)
                print " ------------------"
    print "======= END"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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