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?
layerfile=arcpy.mapping.MapDocument(r"S:\path_to_layer.lyr")
will indeed break because MapDocument expects having a path to an .mxd file.RuntimeError: ERROR 999999: Error executing function.
desc = arcpy.Describe(lyr)
should be indented.