The loop of adding rasters to hundreds of empty Mosaic Datasets

906
4
Jump to solution
09-12-2017 04:50 PM
ZoeChen
New Contributor III

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))‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

Code Formatting ++ to format so syntax can be checked would be useful and could you throw some print statements in to see what is getting printed out within some of the links.

Also, you base path has a space in it.... and you are mixing methods of forming paths... see below, and see a standardization, but I would put stuff in a folder without space in any event... EVEN if it isn't an issue now.

But didn't see any *.tif file extension (unless I missed it).  So if my example using your file is correct, then you need to make *tif files, since an extensionless raster is an esri grid, which definitely doesn't like spaces in it.

basepath = "E:/sample data"

a =  r'LC08_L1TP_125052_20140411_20170423_01_T1_sr_band2_masked.tif'

a0 = a.split('_')[3]

a1 = a.split('_')[8]

import os

os.path.join(basepath, a0)
'E:/sample data\\20140411'

os.path.join(basepath, a1)
'E:/sample data\\band2'

# you might want to try......

os.path.normpath(os.path.join(basepath, a1))
'E:\\sample data\\band2'‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

4 Replies
DanPatterson_Retired
MVP Emeritus

Code Formatting ++ to format so syntax can be checked would be useful and could you throw some print statements in to see what is getting printed out within some of the links.

Also, you base path has a space in it.... and you are mixing methods of forming paths... see below, and see a standardization, but I would put stuff in a folder without space in any event... EVEN if it isn't an issue now.

But didn't see any *.tif file extension (unless I missed it).  So if my example using your file is correct, then you need to make *tif files, since an extensionless raster is an esri grid, which definitely doesn't like spaces in it.

basepath = "E:/sample data"

a =  r'LC08_L1TP_125052_20140411_20170423_01_T1_sr_band2_masked.tif'

a0 = a.split('_')[3]

a1 = a.split('_')[8]

import os

os.path.join(basepath, a0)
'E:/sample data\\20140411'

os.path.join(basepath, a1)
'E:/sample data\\band2'

# you might want to try......

os.path.normpath(os.path.join(basepath, a1))
'E:\\sample data\\band2'‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
ZoeChen
New Contributor III

Hi Dan,

I read your codes again and again, but I just do not know why do I need line 12 and line 15? I don't understand. 

Li 

DanPatterson_Retired
MVP Emeritus

Your paths are malformed... line 10, 11 and 13, 14 are examples of how they appear from your sample file... line 18, 19 shows how to use os.path.normpath to standardize a path based on your operating system (windows, in this case).

If you need to provide path names in a script, you can use

'E:/My_folder'

'E:\\My_folder' or use 'raw' formatting ...

r'E:\My_folder'  ... notice the r in front of the path name

ZoeChen
New Contributor III

Hello guys, in case if any of you will meet this kind of problem, when no error pop up and you still do not get the right result, you should throw your code into the python window in arcgis. At least you get some warnings, well, the warnings I get does not necessarily tell me what to do. Finally, I figured I need a colon between my ras_path and par_path!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! My code finally gives me the result I want. Finally!!!!