1

I'm using ArcGIS Pro 2.9.3, and I want to use the Mosaic to New Raster tool to merge groups of rasters. I have many days that each have multiple rasters within them. For example, I have

July3a.tif

July3b.tif

July3c.tif

July10a.tif

July10b.tif

July10c.tif

July11a.tif

July11b.tif

July11c.tif

So in this case, I'd want to merge July3a.tif-July3c.tif into one raster, July3.tif, then July10a.tif-July10c.tif into July10.tif, and so on.

I've scratched out a basic while loop, but it only works for one day at a time, and it would be awfully time consuming to adjust the arguments for each and every day.

i = July3a.tif

while i = July3*.tif

arcpy.management.MosaicToNewRaster(i, output_location, July3.tif, {coordinate_system_for_the_raster}, {pixel_type}, {cellsize}, number_of_bands, {mosaic_method}, {mosaic_colormap_mode})

i += 1

Is it possible to make a loop that merges multiple days at once?

2
  • For each day do you only ever have the a, b and c versions or do they randomly go to z?
    – Hornbydd
    May 18, 2022 at 19:23
  • @Hornbydd The latter. My rasters are satellite images that have been filtered for clouds, so some dates have more or less images than others.
    – Beck
    May 18, 2022 at 19:26

1 Answer 1

2

Here is the code you can use, obviously I don't have your data so I spoofed it up with text files, so you need to edit the code and replace the txt with tif.

Here is my sample data:

Spoofed data

# Import modules
import arcpy
import glob
import os
import collections

# Read tiff files in folder into a list
folder = r"C:\temp"
lstTIFFS = glob.glob(folder + r"\*.txt") # replace txt with tif

# Create special dictionary where the items will be inserted into a set
dicFiles = collections.defaultdict(set)

# Build dictionary, key is day and item is a set of characters, e.g. june6,{a,b,c}
for fp in lstTIFFS:
    fn = os.path.basename(fp)[:-4]
    character = fn[-1] # returns something like a
    day = fn[:-1] # returns something like june6
    dicFiles[day].add(character)

# Mosaic data for each day
for k,v in dicFiles.iteritems():
    # create empty list of files for a single day
    lstInputs = list()

    # Build full path and insert into list
    for character in v:
        fp = folder + "\\" + k + character + ".txt"
        lstInputs.append(fp)

    # Mosaic data
    outputname = "Merged_" + k + ".tif"
    arcpy.MosaicToNewRaster_management(lstInputs,r"C:\temp",outputname) # Important! set other parameters

This code is developed for ArcMap so if you want to run it in ArcGIS Pro you need to change this line:

for k,v in dicFiles.iteritems():

to

for k,v in dicFiles.items():
0

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.