AddLayer loads imagery with incorrect colors

1125
6
Jump to solution
10-16-2013 02:38 PM
ScottOatley
New Contributor II
Hello,

My arcpy script uses the arcpy.mapping.AddLayer() function to add imagery raster datasets to the TOC. However, the symbology is defaulted to blue stretched rather than the appropriate RGB or grey stretched for color or b&w images respectively.

This doesn't happen if I drag-and-drop from Arc Catalog, a windows folder, or use the add data button.

Any idea why this happens?

Thank you,
Scott
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JasonScheirer
Occasional Contributor III
How are you running this code? As a script tool?

Try this:

field = 'FILENAME' cursor = arcpy.SearchCursor(myRefLayer) for row in cursor:     fullpath = row.getValue(field)     path = os.path.dirname(fullpath)     name = os.path.basename(fullpath)     addLayer = arcpy.management.MakeRasterLayer(path, name)[0]     arcpy.mapping.AddLayer(df, addLayer, 'TOP')

View solution in original post

0 Kudos
6 Replies
JasonScheirer
Occasional Contributor III
Use the Make Raster Layer function, don't create arcpy.mapping.Layer objects directly from raster datasets. It will add them to the TOC with the correct symbology.
0 Kudos
ScottOatley
New Contributor II
I tried replacing this:

field = 'FILENAME'
cursor = arcpy.SearchCursor(myRefLayer)
for row in cursor:
    path = row.getValue(field)
    addLayer = arcpy.mapping.Layer(path)
    arcpy.mapping.AddLayer(df, addLayer, 'TOP')


with this:

field = 'FILENAME'
cursor = arcpy.SearchCursor(myRefLayer)
for row in cursor:
    path = row.getValue(field)
    name = os.path.basename(path)
    arcpy.MakeRasterLayer_management(path, name)


The former loads the imagery with the weird colors; the latter doesn't load any images. Is there something additional required with the MakeRasterLayer function? It didn't seem so from the help pages. I did try adding a RefreshTOC(), but that didn't change the result.

Thank you,
Scott
0 Kudos
JasonScheirer
Occasional Contributor III
How are you running this code? As a script tool?

Try this:

field = 'FILENAME' cursor = arcpy.SearchCursor(myRefLayer) for row in cursor:     fullpath = row.getValue(field)     path = os.path.dirname(fullpath)     name = os.path.basename(fullpath)     addLayer = arcpy.management.MakeRasterLayer(path, name)[0]     arcpy.mapping.AddLayer(df, addLayer, 'TOP')
0 Kudos
ScottOatley
New Contributor II
I'm running this as a script from a toolbox; sorry, forgot to mention that initially.

I made a slight change to your code, and the rasters load correctly as you said they would. I used the path including the file name as the first parameter to MakeRasterLayer rather than just the dir name. This also worked nicely to load mosaic datasets.

field = 'FILENAME'
cursor = arcpy.SearchCursor(myRefLayer)
for row in cursor:
    path = row.getValue(field)
#    path = os.path.dirname(fullpath)
    name = os.path.basename(path)
    addLayer = arcpy.management.MakeRasterLayer(path, name)[0]
    arcpy.mapping.AddLayer(df, addLayer, 'TOP')


A couple of follow-up questions...

When do I code using arcpy.management.<function>() versus arcpy.<function>_management() syntax?

Eventually I'll want to put this code in an one of my addIns. Should I expect it to work pretty much as is?

Thank you,
Scott
0 Kudos
JasonScheirer
Occasional Contributor III
arcpy.management.<function>() and arcpy.<function>_management() are functionally equivalent, it's a matter of personal preference.

Once it's an add-in, just calling the tool should add it to the TOC without the AddLayer part.
0 Kudos
ScottOatley
New Contributor II
Thanks for your help Jason.

Best regards,
Scott
0 Kudos