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?
2 Answers 2
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– jazzabeanie2015年10月21日 23:06:24 +00:00Commented Oct 21, 2015 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– GISGe2015年10月22日 18:30:15 +00:00Commented Oct 22, 2015 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– jazzabeanie2015年10月23日 04:46:59 +00:00Commented Oct 23, 2015 at 4:46
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– jazzabeanie2015年10月23日 03:28:03 +00:00Commented Oct 23, 2015 at 3:28
Explore related questions
See similar questions with these tags.
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.