I have many hundreds of GeoTiff files to mosaic. The problem is that they are stored in a sub-directory structure whereby I need to traverse down into each sub-directory, mosaic all rasters in that location, and then go back up and down into all the other sub-directories sequentially to perform the same action. Some example directories would appear as follows:
c:\dem\250k\001 (do all rasters here)
............. \002 (do all rasters here)
............. \003 (do all rasters here)
I found the following Python snippet on the forum, but it finds and mosaics all rasters in all sub-directories to a single output layer. I need to find a way to "walk" into the sub-directories and output a single output for each. Thanks.
import arcpy
import os
workspace = r"c:\dem\250k"
arcpy.CheckOutExtension("Spatial")
rasters = []
walk = arcpy.da.Walk(workspace, topdown=True, datatype="RasterDataset")
for dirpath, dirnames, filenames in walk:
dirpaths.append(os.path.join(dirpath, dirnames)
for filename in filenames:
rasters.append(os.path.join(dirpath, filename))
ras_list = ";".join(rasters)
arcpy.MosaicToNewRaster_management(ras_list,workspace,
"test.tif", "", "16_BIT_SIGNED", "", "1", "LAST")
# return SA license
arcpy.CheckInExtension("Spatial")
Solved! Go to Solution.
Hi Don,
Here is a an example on how to do this. The below example mosaicks 3 band rasters together in each directory. It outputs a new TIF called 'Mosaic.tif' to each directory.
import arcpy, os from arcpy import env env.workspace = r"E:\Temp\Python\UserData" 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", "", "", "", 3) except: pass
Hi Don,
Here is a an example on how to do this. The below example mosaicks 3 band rasters together in each directory. It outputs a new TIF called 'Mosaic.tif' to each directory.
import arcpy, os from arcpy import env env.workspace = r"E:\Temp\Python\UserData" 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", "", "", "", 3) except: pass
Thanks very much for your help, Jake. I will give this a try today.