2

My raster mosaic datasets are in a file gdb ordered by block. For example Block_470 and Block_480.

My workspace has images ordered by block with subfolders with in each block. For example workspace Block_470 contains subfolders BW and RGB. This is the same for workspace Block_480. Meaning workspace Block_480 will also have subfolders BW and RGB.

I need to add the tiff files in workspace Block_470/BW to raster mosaic dataset Block_470 and the tiff files in workspace Block_480/BW will need to be added to raster mosaic dataset Block_480.

How can I iterate through so that the right tiff files in the subfolders go into the corresponding mosaic datasets?

I was thinking something like this:

import arcpy

workspaces = arcpy.ListWorkspaces("//nyx-galli/eGIS/Data/Raster/Imagery/Aerial/SGIC/2008_Mapcon/Block4*", "Folder")
datasets = arcpy.ListDatasets("D:/TestRasterMosaic/BW.gdb/Block_4*", "Mosaic")

for i in workspaces:
    workspaces2 = arcpy.ListWorkspaces("", "Folder")

After building this model did not work for me:

enter image description here

Model Builder Image

2

You can us an os.walk() or an arcpy.da.walk() to go through all your folders as required. I used the arcpy.da.walk() as you can use this to also specify certain dataset types such as feature class, or in this case RasterDataset.

You can then check for the BW folder, and add all the rasters in that folder into a Mosaic Dataset based on the block name.

As I didn't have any mosaic datasets in my test Geodatabase, I had to create those as well. I have commented to explain what different parts of the code do. Some things you will need to change to work in your environment.

import arcpy, os

basePath = r"D:\GIS\SE\Mosaic"
db = os.path.join(basePath, r"MyMosaicDB.gdb")

# Set this to the spatial reference you want set for your mosaic dataset.
sr = arcpy.SpatialReference(3857) 

# Change this to match the filetype e.g. type="TIF"
walk = arcpy.da.Walk(basePath, datatype="RasterDataset", type="PNG") 

for dirpath, dirnames, filenames in walk:
    xPath = os.path.normpath(dirpath).split(os.sep)
    # Checks for 'BW' folder to process
    if xPath[-1] == 'BW':
        arcpy.env.workspace = dirpath
        # block is the parent folder of BW e.g. Block_470
        block = xPath[-2]
        # Name of the Mosaic Dataset e.g. Block_470_Mosaic
        mosaicDataset = "{0}_Mosaic".format(block)
        if filenames:
            print filenames
            # Create the Mosaic Dataset if it doesn't exist
            if not arcpy.Exists(os.path.join(db, mosaicDataset)):
                print "creating mosaic dataset {0}".format(mosaicDataset)
                arcpy.CreateMosaicDataset_management(db, mosaicDataset, sr, "", "", "NONE", "")
            print "adding rasters for block {0}".format(block)
            # Add rasters to their matching Mosaic Dataset
            arcpy.AddRastersToMosaicDataset_management(os.path.join(db, mosaicDataset), "Raster Dataset", filenames) 
  • THANK YOU for explaining that so clearly! I have now learned how to properly use os.walk() and arcpy.da.Walk() from you! – clair0girl Feb 24 '17 at 23:09
  • 1
    You can pass a list of values to tools that take multi value parameters. No need to do ";".join(values) – Paul Feb 25 '17 at 0:16
  • @Paul I have found enough tools that don't allow this to make it worth using the join. I didn't test for this tool – Midavalo Feb 25 '17 at 0:24
  • Interesting, because my experience is just the opposite. Any tools off the top of your head that only accept a delimited string? – Paul Feb 25 '17 at 0:40
  • @Paul I thought I came across one the other day, but can't find it. I'll keep looking, but now you've got me wondering if it's something else that doesn't accept lists. I'll test and change my answer if successful – Midavalo Feb 25 '17 at 0:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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