1

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

Returns (from 2 dirs in this example, 00114 and 00628):

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
  • Are you using Python 2 or 3?
    – Aaron
    Commented Sep 24, 2020 at 7:42

1 Answer 1

1

The Mosaic tool takes a semicolon separated string of images as its first parameter, which you can see in this code sample that I reduced from the Help:

import arcpy
arcpy.env.workspace = r"\\workspace\PrjWorkspace\RasGP"
arcpy.Mosaic_management("landsatb4a.tif;landsatb4b.tif","Mosaic\\landsat.tif","LAST","FIRST","0", "9", "", "", "")

At the moment you are assembling your images (with full paths) and printing them. If instead you appended them into a list, then you would have something ready to turn into a semicolon separated string (see How would you make a comma-separated string from a list of strings?) that you could feed into the Mosaic tool.

You could bypass creating the list and code straight for the semicolon separated string but lists are created in so many places by ArcPy, that I think this is a useful coding pattern for any ArcPy developer to know about.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.