2

I have a collection of rasters (all in separate geodatabases) that I am trying to put into a MOSAIC using a python script. I have a bunch of these to make so I was thinking that the easiest way to do it would be to create a .lyr file containing all the rasters I want to add and then have a script read through that layer file and add the rasters using AddRastersToMosaicDataset_management. The problem is that I haven't been able to read through the contents of a layer file. This line seems to break my code: layerfile=arcpy.mapping.MapDocument(r"S:\path_to_layer.lyr"). I try to catch the error message with except: print arcpy.GetMessages() but nothing prints.

I thought I'd try the other approach and use an mxd instead of a layer file. I created an mxd with just the rasters I wanted to add, but I couldn't read the path of the raster (shapefiles did work though). Here is my code:

import arcpy
mxd=arcpy.mapping.MapDocument(r"S:\path_to_mxd.mxd")
dataframes=arcpy.mapping.ListDataFrames(mxd)
for dataframe in dataframes:
    for lyr in arcpy.mapping.ListLayers(mxd, "", dataframe):
        desc = arcpy.Describe(lyr)
        print desc.catalogPath
        #print(arcpy.Describe(lyr).catalogPath)

This will print a path to a shapefile, but gives IOError: "Raster_Name" does not exist when the layer is a raster.

At the moment my only working solution is to manually generate a text file with the path to each of the rasters and read through that line by line. I don't want to have to generate a text file for every MOSAIC I need to create. I feel like I've got 4 possible paths I could go down and I'm getting stuck on each one. Can anyone help me either;

  • read through a .lyr file or an mxd and extract the paths of the raster datasets; or
  • convert a .lyr or an mxd to a text file listing the paths of the datasets contained?
  • I am on ArcGIS 10.3.1 and am able to run the code you published to get the path to a raster dataset that is added into an mxd file. I have added a .tif file into ArcMap and then run your code in Python window. What version of ArcGIS do you have? How do you execute the code? What is the raster format? – Alex Tereshenkov Oct 15 '15 at 6:30
  • 2
    NB: layerfile=arcpy.mapping.MapDocument(r"S:\path_to_layer.lyr") will indeed break because MapDocument expects having a path to an .mxd file. – Alex Tereshenkov Oct 15 '15 at 6:31
  • @AlexTereshenkov ArcGIS 10.1, executing in Windows PowerShell ISE(x86), raster format is FGDBR (File Geodatabase Raster Dataset?) Just changed it to a random TIFF file I found online and got RuntimeError: ERROR 999999: Error executing function. – jazzabeanie Oct 15 '15 at 7:08
  • How do you manage to have several rasters in one layer? Are they already in a raster catalog or mosaic dataset? – GISGe Oct 15 '15 at 7:19
  • Try running your code inside the Python window in ArcMap or from a Windows cmd. Your mxd should contain just one raster dataset added. PS. Have you published the code you run as is? You need to fix the indentation - desc = arcpy.Describe(lyr) should be indented. – Alex Tereshenkov Oct 15 '15 at 7:23
0

Your code tries to get the catalogPath property of the group layer containing your rasters, which doesn't exist. Instead, I would retrieve the dataSource property of the Layer, after testing that it is indeed applicable with the supports() method (group layers don't support this property).

Your code would look like this:

import arcpy
mxd=arcpy.mapping.MapDocument(r"S:\path_to_mxd.mxd")
dataframes=arcpy.mapping.ListDataFrames(mxd)
for dataframe in dataframes:
    for lyr in arcpy.mapping.ListLayers(mxd, "", dataframe):
        if lyr.supports(dataSource):
            print lyr.dataSource

See the help page for Layer properties and methods for more details.

If you want to follow @Hélène's suggestion and list all rasters in a gdb, use the arcpy.ListRasters() function. If all your rasters are in the same workspace, this would indeed be more straightforward than putting everything in an mxd to get your list.

  • Even if I call catalogPath on a single raster within an mxd, I still get the same error. So I'm guessing that the catalogPath property doesn't exist for gdb rasters? – jazzabeanie Oct 21 '15 at 23:06
  • @jazzbeanie I've tested your code with a gdb raster in an mxd and it works (I have 10.3.1). What about calling the layer.dataSource property? Does it return an error as well? – GISGe Oct 22 '15 at 18:30
  • NameError: name 'dataSource' is not defined but doesn't matter anymore, thanks for your help. It's nice to know that it would've worked if I had access to decent resources. – jazzabeanie Oct 23 '15 at 4:46
0

in fact, you can actually use the source databases as input path to the AddRastersToMosaicDataset tool - this simplifies things enormously, as you 'only' need to loop through the folder(s) containing the databases, for instance, assuming you load file geodatabase rasters only:

import os, arcpy, sys
path_geodata = r"PATH TO PARENT FOLDER CONTAINING YOUR DATABASES"
path_mosaic = r"PATH TO YOUR MOSAIC"

for path, subdirs, files in os.walk(path_geodata):
    print path
    if path.lower().endswith(".gdb") == True:
        raster_type = "Raster Dataset"
        try:
            arcpy.AddRastersToMosaicDataset_management(path_mosaic, raster_type, path,
                                                       "UPDATE_CELL_SIZES","UPDATE_BOUNDARY",
                                                       "UPDATE_OVERVIEWS","#","0","1500","#",
                                                       "#","SUBFOLDERS","EXCLUDE_DUPLICATES",
                                                       "BUILD_PYRAMIDS","CALCULATE_STATISTICS",
                                                       "BUILD_THUMBNAILS","#",
                                                       "NO_FORCE_SPATIAL_REFERENCE")
        except:
           type, value, traceback = sys.exc_info()
           print "error value", str(value)
           for msg in range(0, arcpy.GetMessageCount()):
               if arcpy.GetSeverity(msg) == 2:
                   print msg
  • I tried this but couldn't use os.walk (I think because I don't have Service Pack 1). I ended up using arcpy.ListRasters and concatenating them which didn't quite work that well. It didn't really matter in the end because there was that much variation on the naming convention that my regular expression missed a lot of rasters. Good idea though. – jazzabeanie Oct 23 '15 at 3:28

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.