[GIS] Renaming 1,200 rasters in File Geodatabase using ArcPy

arcmaparcpycode-reviewrasterrename

I've been tasked with renaming a large amount of raster files within a file geodatabase (using ArcGIS 10.2.2).

I'm taking weather data which has a name format as such: "G"mmddhhhh
I'm trying to add a year tag in between the G and month: "G"yyyymmddhhhh

Here is the function that I've written:

# ----------------------------------------------------------------------------- #
@timeit
def renameRasters(FGDB):
    origdir = arcpy.env.workspace
    arcpy.env.workspace = FGDB

    for rstr in arcpy.ListRasters("G*"):
        try:
            newrstr = "G" + "2014" + rstr.replace("g", "")
            arcpy.Rename_management(rstr, newrstr)

        except:
            print "Failed to rename: " + rstr

    arcpy.env.workspace = origdir
    return None
# ----------------------------------------------------------------------------- #

This function works fine for a small to medium amount of rasters within a FGDB and takes about 1.5 seconds per raster renamed. I run into issues when there are a large number of files to be renamed. 1,200 files takes > 10 hours. At the same rate as the smaller raster sets this should take < 1 hour…

Does anyone have an idea on how to make this process go faster?

Best Answer

I recently had a similar task. It seems arcpy.Rename_management() is slow by nature. I did find that adding arcpy.env.addOutputsToMap = False improves performance ever-so-slightly, as ArcMap doesn't have to spend time drawing the rasters as they're added. Alternatively, the script can be run from ArcCatalog.

I added additional timers and print statements to your script (see example outputs below. My Virtual machine was running extra slow, so times are exagerated):

import arcpy, timeit

arcpy.env.addOutputsToMap = False

def renameRasters(FGDB):
    origdir = arcpy.env.workspace
    arcpy.env.workspace = FGDB

    for rstr in arcpy.ListRasters("*"):
        rstrTimerStart = timeit.default_timer()
        try:
            newrstr = "G" + "2015" + rstr.replace("G2015G2014", "G2015")
            arcpy.Rename_management(rstr, newrstr)
            printMsg = "--Raster renamed " + newrstr + ". Timer: "

        except Exception as e:
            printMsg = "--Rename failed: " + rstr + ". Exception: " + str(e).replace("\n","") + "  Timer: "
        
        rstrTimerStop = timeit.default_timer()
        print printMsg + str(rstrTimerStop - rstrTimerStart) + " seconds."

    arcpy.env.workspace = origdir
    return None

start = timeit.default_timer()

print "Begin Raster Rename Function"
renameRasters(r"C:\Temp\Test_RasterRename.gdb")
print "Raster Rename Function Complete"

stop = timeit.default_timer()
print "Took " + str(stop - start) + " seconds to complete."

addOutputsToMap = True, From network drive

arcpy.env.addOutputsToMap = True, From network drive

addOutputsToMap = False, From network drive

arcpy.env.addOutputsToMap = False, From network drive

addOutputsToMap = False, From local drive

arcpy.env.addOutputsToMap = False, From local drive

Related Question