TransWikia.com

Merging two data sets (multiple satellite images) based on their names using ArcPy

Geographic Information Systems Asked by Asif Marazi on January 25, 2021

I have two folders containing 1800 files each. I need to merge or combine them based on their names, e.g. file MOD10A2.A2017177.h24v05.006.2017187224801.tif in folder A should combine/merge with the file MYD10A2.A2017177.h24v05.006.2017191190921.tif in folder B and file MOD10A2.A2017188.h24v05.006.2017183445431.tif in folder A should combine/merge with the file MYD10A2.A2017188.h24v05.006.2017134567541.tif in folder B and so on. And I want the resulted images in folder C.

The following code actually executes successfully but it doesn’t write or save the resulted images or files in folder C.

Here is the code (courtesy BERA)

import os, arcpy
from collections import defaultdict as dd

baseFolder = r'D:data' # containing two sub-folders
d = dd(list)

for root, folders, files in os.walk(baseFolder):
    for file in files:
        d[file.split('.')[1]].append(os.path.join(root, file))

for partname, pathlist in d.items():
    if len(pathlist)==2:
        fullPaths = [os.path.join(root, f) for f in pathlist]
        if all(os.path.isfile(p) and p.endswith('.tif') for p in fullPaths):
            arcpy.MosaicToNewRaster_management(input_rasters=';'.join(fullPaths), output_location='Dcombined_data', raster_dataset_name_with_extension=partname+'.tif', number_of_bands=1)

One Answer

UPDATE

The below code should achieve what you're trying to do. However as with all things programming there are many ways to go about doing this. Circling back to your original post, it looks like you are missing the colon ":" in your output file path. You put 'Dcombined_data' instead of 'D:combined_data' If that was an error in your original script, that would cause it to not be output as well. Either way, fixing the error in your code, or using the sample below, should achieve what you're after. In the snippet below, I tried to use your paths as you showed them. I tested this on my machine with two sample rasters that I named exactly as the ones you showed a raster was successfully output.

Try this:

import arcpy,os

baseFolder =r"D:data"
subfolders = [ f.path for f in os.scandir(baseFolder) if f.is_dir() ]


#set directories
Dir_a = subfolders[0]
Dir_b = subfolders[1]
OutputLocation = r"D:combined_data"

#set workpace and get list of all rasters in Dir_a
arcpy.env.workspace = Dir_a
rasters_a = [os.path.join(Dir_a,f) for f in arcpy.ListRasters()]

#set workpace and get list of all rasters in Dir_b
arcpy.env.workspace = Dir_b
rasters_b = [os.path.join(Dir_b,f) for f in arcpy.ListRasters()]


ToCombine = {}
for raster in rasters_a:
  rasterkey = raster.split('.')[1]
  paths = [raster, [f for f in rasters_b if f.split('.')[1] == rasterkey][0]]
  ToCombine[rasterkey] = paths



for key, rasterPaths in ToCombine.items():
  arcpy.MosaicToNewRaster_management(rasterPaths, OutputLocation, raster_dataset_name_with_extension=key+".tif", number_of_bands=1)

Correct answer by John on January 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP