0

I have 200+ directories, each directory contains x amount of TIFFs which I want to mosaic.

I have a snip of code that will list the TIFF contents of all dirs under a parent directory, now I just need to parse the list (per dir), pass it to the mosaic program and run all TIFFs in the first dir, then loop to the next dir in the list and repeat the mosaic operation.

import arcpy 
import os

#Set the workspace enviorment setting
arcpy.env.workspace = r"C:/Work/SpaceNet/Test2"

walk = arcpy.da.Walk(datatype="RasterDataset")

for dir_path, dir_names, file_names in walk:
    for filename in file_names:
        print(os.path.join(dir_path, filename))


C:\Work\SpaceNet\Test2\00114 SN6_Train_AOI_11_Rotterdam_SAR-Intensity_20190822100114_20190822100413_tile_678.tif
C:\Work\SpaceNet\Test2\00114 SN6_Train_AOI_11_Rotterdam_SAR-Intensity_20190822100114_20190822100413_tile_679.tif
C:\Work\SpaceNet\Test2\00114 SN6_Train_AOI_11_Rotterdam_SAR-Intensity_20190822100114_20190822100413_tile_680.tif
C:\Work\SpaceNet\Test2\00628 SN6_Train_AOI_11_Rotterdam_SAR-Intensity_20190823100628_20190823100943_tile_5430.tif
C:\Work\SpaceNet\Test2\00628 SN6_Train_AOI_11_Rotterdam_SAR-Intensity_20190823100628_20190823100943_tile_5431.tif
C:\Work\SpaceNet\Test2\00628 SN6_Train_AOI_11_Rotterdam_SAR-Intensity_20190823100628_20190823100943_tile_5432.tif
1

You are very close with your code, you only need to make a list and derive the filename:

import arcpy
import os

# Set the workspace enviroment setting
arcpy.env.workspace = r"C:/Work/SpaceNet/Test2" # doesn't need to be a raw string using / path delimiter'

walk = arcpy.da.Walk(datatype="RasterDataset")

for dir_path, dir_names, file_names in walk:
    OutputRaster = dir_path + '.tif' # the TIFF file in the parent directory with the same name
    AllFiles     = []                # empty list
    for filename in file_names:
        print(os.path.join(dir_path, filename))
        AllFiles.append(os.path.join(dir_path, filename)) # add this one to the list
    if len(AllFiles) > 0 :  # make sure there is acutally rasters in this folder
        arcpy.MosaicToNewRaster_management(AllFiles, os.path.dirname(OutputRaster), os.path.basename(OutputRaster))

There are two handy functions here, os.path.dirname which returns the folder a file or folder is in and os.path.basename which returns the name of the last folder or file name. Mosaic to New Raster is different to most other tools in that it asks for the folder and file name separately instead of using a full output path.

You will need to decide on what (if any) optional parameters are required in the Mosaic to New Raster and add them to the code.

| improve this answer | |
0

Get a list of all the directories, then for each directory do:

  1. Set arcpy.env.workspace to the current directory
  2. Get list of rasters using arcpy.ListRasters()
  3. Pass the list to arcpy.MosaicToNewRaster_management (see here). Make sure to specify a full path for the output

You if all the directories w/ your tiffs are in the same parent directory and it doesn't have any other files you can just use os.listdir(). Otherwise you could use something like os.path.walk if you need to iterate through nested directories.

| improve this answer | |
  • I will try implementing the suggested process and post the results asap! – Mark S. Sep 21 at 18:46
  • Ok, so far I can run a single folder worth of .tif to run out a mosaic using your example. In my case, I have 203 folders (each with between 8 and 15 .tifs) that I would like to make individual mosaics from. And for each, name the output mosaic using the folder as the prefix ie. I have a folder name "00112" with 8 tifs, so the process would mosaic the 8 tifs together and name the output 00112.tif, then go to the next folder and repeat. – Mark S. Sep 21 at 19:29
  • Please edit your post to include your code. As I mentioned above the easiest route is probably os.listdir() if all 203 folders are in the same parent folder. That will give you a list of directory names as strings, then you can iterate through that list with a for loop, using the name of the current directory to a) set the output file name using string formatting, b) create a fully qualified path using os.path.join() and the parent directory path, and c) use said path to set arcpy.env.workspace appropriately – Nick Sep 21 at 19:36
  • You might want to look up arcpy.da.Walk(datatype='RasterDataset') to traverse the subfolders resources.arcgis.com/en/help/main/10.2/index.html#//… then os.path.basename of the current path which will return the last part as a string to create the new raster to. If you're interested in a python solution please edit your question to include python options and include some code to start from, even if it doesn't work, to give us something to start from. – Michael Stimson Sep 22 at 2:56

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.