1

I have a number of sub-directories within my "BP Tiles" directory. Each of these sub-directories represents a different year (2016, 2017, 2018, etc.) and within each of these sub-directories is a list of raster layers. I am attempting to use the Walk module to mosaic each list of Raster layers within the individual year directories. See Code Source

import arcpy, os
from arcpy import env

# SET GEO_PROCESSING ENVIRONMENTS
arcpy.env.workspace = r"C:\Users\BP_Tiles"

walk = arcpy.da.Walk(env.workspace, topdown=True, datatype="RasterDataset")

for dirpath, dirnames, filenames in walk:
    print
    dirpath
    rasterList = []
    for file in filenames:
        raster = os.path.join(dirpath, file)
        rasterList.append(raster)
    try:
        arcpy.MosaicToNewRaster_management(rasterList, dirpath, + "Mosaic.tif", "", "", "", 1)
    except:
        pass

The code is set up to save the output "Mosaic.tif" within each sub-directory. I'd like to manipulate this code such that all of the Mosaics are saved to the same output folder. Id also like to append the sub-directory name (Ex. 2016) to each output such that I know how to differentiate the outputs.

Would anyone happen to know how to do this?

1
  1. All of the Mosaics are saved to the same output folder.

Instead of using dirpath when creating your new raster use the same path each time.

outPath  = r"C:\some\path"
for dirpath, dirnames, filenames in walk:
    .... #other code
    arcpy.MosaicToNewRaster_management(rasterList, outPath, "Mosaic.tif", "", "", "", 1)
  1. Append the sub-directory name (Ex. 2016) to each output such that I know how to differentiate the outputs

Update "Mosaic.tif" with the sub-directory name.

for dirpath, dirnames, filenames in walk:
    ....#other code
    dirName = os.path.basename (dirpath)
    arcpy.MosaicToNewRaster_management(rasterList, outPath, "Mosaic_{}.tif".format (dirName), "", "", "", 1)

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.