Using arcpy.da.Walk to mosaic multiple rasters?

2328
2
Jump to solution
03-16-2016 04:52 AM
DonRaymond
New Contributor

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")

0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

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

View solution in original post

2 Replies
JakeSkinner
Esri Esteemed Contributor

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
DonRaymond
New Contributor

Thanks very much for your help, Jake. I will give this a try today.

0 Kudos