AnsweredAssumed Answered

The loop of adding rasters to hundreds of empty Mosaic Datasets

Question asked by zoexili on Sep 12, 2017
Latest reply on Sep 13, 2017 by zoexili

I have three lists in total, list 1 and list 2 are both satellite images but they locate right next to each other (path/row 125052 and path/row 125053). I have a lot of them with different dates. List 3 contains many empty mosaic datasets that are named after list 1's images. Right now I need to put each pair of images into its designated mosaic dataset.

 

For example, LC08_L1TP_125053_20140411_20170423_01_T1_sr_band2_masked.tif AND LC08_L1TP_125052_20140411_20170423_01_T1_sr_band2_masked.tif  GO TO  the mosaic dataset called LC08_L1TP_125052_20140411_20170423_01_T1_sr_band2_mosaic. The same logic applies to the other hundreds of pairs of images.

 

Here is my code, I feel I have fixed all the errors that popped up but when I check my mosaic dataset in arcgis desktop, no rasters were added into my mosaic dataset. Please help, thank you!!!!

 

import arcpy,re,os
basepath = arcpy.env.workspace = "E:/sample data"
lst1 = arcpy.ListRasters("*125052*")
lst2 = arcpy.ListRasters("*125053*")

arcpy.env.workspace = "E:/sample data/sample.gdb"
lst3 = arcpy.ListDatasets()

outworkspace = "E:/sample data/sample.gdb/"

for raster in lst1:
    rasterName = raster
    partnerName = ""
    datasetName = ""
    for partner in lst2:
    # I used regular expression, split function to split the rasterName to extract the date (ex, "20140411") and the band (ex, "band2") so that rasterName and partnerName can match each other. #
       if re.split('_',partner)[3] == re.split('_',rasterName)[3]:
          if re.split('_',partner)[8] == re.split('_',rasterName)[8]:
             partnerName = partner
             break
    for dataset in lst3:
    # the same logic here, I hope both raster and partner can go to the specific mosaic dataset. #
         if re.split('_',dataset)[3] == re.split('_',rasterName)[3]:
            if re.split('_',dataset)[8] == re.split('_',rasterName)[8]:
                datasetName = dataset
                outname = os.path.join(outworkspace, datasetName)
                arcpy.AddRastersToMosaicDataset_management(outname,"Raster Dataset",os.path.join(basepath,rasterName) + os.path.join(basepath,partnerName))

Outcomes