Select to view content in your preferred language

addLayer error

3043
7
03-03-2011 11:48 AM
ScottOatley
Emerging Contributor
Hi Folks,

I'm trying to load some image layers to a dataframe. The file name / path of the image is read from a feature class using a search cursor.

...

ss = arcpy.SearchCursor("MyLayer", "", "", "FILENAME")
for s in ss:
    arcpy.AddMessage(s.FILENAME)
    addLayer = arcpy.mapping.Layer(s.FILENAME)
    arcpy.mapping.AddLayer(df, addLayer, "TOP")


The AddMessage works and shows the correct name, but I get the following error on the addLayer code:

<type 'exceptions.ValueError'>: Object: CreateObject Layer invalid data source


Is it possible that it's a path error? The value from the feature class is a Windows path so it contains \'s. I've post-processed the name to replace \'s with /'s, but that didn't make a difference.

I assume addLayer works with .sid, .tif, etc?

Thanks,
Scott
Tags (2)
0 Kudos
7 Replies
JasonScheirer
Esri Alum
You can only create layer objects from .lyr files.
0 Kudos
ScottOatley
Emerging Contributor
You can only create layer objects from .lyr files.


OK. So is there some other command that can be used to directly add data to the TOC? Or some other method that doesn't involve creating a lyr file? Our production work involves loading and removing layers repeatedly. I don't really want to create temporary files all the time just to load data.

Scott
0 Kudos
JasonScheirer
Esri Alum
If you're working within ArcMap, the MakeFeatureLayer and MakeRasterLayer tools should do what you want.
0 Kudos
ScottOatley
Emerging Contributor
Greetings:

Well, I've been trying to get this to work. I got my code to run without error. However, nothing shows up in the TOC. I added an arcpy.RefreshTOC() statement, but that didn't change the result. I decided to make a model and dump the code to python.

# ---------------------------------------------------------------------------
# makeRaster.py
# Created on: 2011-03-07 15:34:04.00000
#   (generated by ArcGIS/ModelBuilder)
# Description: 
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy

# Local variables:
layer_jpg = "C:\\tmp\\layer.jpg"
MakeRas_jpg2 = "MakeRas_jpg2"

# Process: Make Raster Layer
arcpy.MakeRasterLayer_management(layer_jpg, MakeRas_jpg2, "", "-0.5 -13868.5 13765.5 0.5", "")


The model worked and added the "MakeRas_jpg2" layer to the TOC. Running it as a python script, it runs to completion successfully, but doesn't add the new layer to the TOC. Same result if I add the "Refresh" command.

What am I missing?

Thanks,
Scott
0 Kudos
KimOllivier
Honored Contributor
You have to run it as a tool and add an output parameter. In the tool the parameter is set to DERIVED and in the script you set the parameter to the layer.
0 Kudos
ScottOatley
Emerging Contributor
Hello,

You have to run it as a tool and add an output parameter. In the tool the parameter is set to DERIVED and in the script you set the parameter to the layer.


OK, I got this to work per the above. My next issue I've been working on is using the DERIVED output parameter as MULTIVALUE. I tried the following. I do a select by location in a table and then do a search cursor on a field containing full file names. I've got 1 input parameter so I set my index at 1.

arcpy.SelectLayerByLocation_management( blah, blah, ... )
ss = arcpy.SearchCursor( ... )
for s in ss:
    arcpy.SetParameterAsText(1, arcpy.MakeRasterLayer_management(s.FILENAME, os.path.basename(s.FILENAME)))


This sort of works as it loads the image layer for the last record in the search cursor. Other than setting multivalue to yes on the tool properties, what else must be done?

Thank you,
Scott
0 Kudos
NathanPugh
Emerging Contributor
Scott,
Did you ever get this to return multiple values?  I am working on something similar. 

Thanks
Nathan
0 Kudos