Select to view content in your preferred language

adding raster data from a file gdb to the current mxd  TOC using python

4081
6
10-17-2012 10:17 AM
DaleSchaper
Emerging Contributor
I want to create a toolbox using python that adds two sets of raster data located in a gdb into my current .mxd

The best that I can do is create some new layer and then have it adds into the TOC but then it disappears when the tool completes:  It is also not the original name for the raster but the name with an added number.  I want to add the original raster and its original name.  I used 'n' in the for loop so that I could add an index to the name.


    fc_list=[fc_meanSun,fc_lowSun]


    mxd = arcpy.mapping.MapDocument("CURRENT")
    df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]
    for n in range(len(fc_list)):
        fc_add=fc_list
        fc=fc_path+'\\'+fc_add
        xxx=fc_add+str(n)

        arcpy.MakeRasterLayer_management(fc, xxx)
        addLayer = arcpy.mapping.Layer(xxx)
        arcpy.mapping.AddLayer(df,addLayer)
Tags (2)
0 Kudos
6 Replies
ArkadiuszMatoszka
Frequent Contributor
Hi,
arcpy.MakeRasterLayer_management is meant to other purpose, so you can skip this. Code below should help. If you want to change layer name use:
addLayer.name = new_name
before adding layer to map doc.
fc_list=[fc_meanSun,fc_lowSun]
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]
for ras_name in fc_list:
    fc=fc_path+'\\'+ras_name
    addLayer = arcpy.mapping.Layer(fc)
    arcpy.mapping.AddLayer(df,addLayer)



Regards.
Arek
0 Kudos
JakeSkinner
Esri Esteemed Contributor
You could create a script that runs the 'Make Raster Layer' function for each raster.  If you have 'Add results of geoprocessing to the display" checked on within the Geoprocessing options, the raster layers will automatically be added to the current map document.

arcpy.MakeRasterLayer_management("C:/DATA/RASTER/DEM/example", "example")
arcpy.MakeRasterLayer_management("C:/DATA/RASTER/DEM/example2", "example2")
0 Kudos
DaleSchaper
Emerging Contributor
Hi Arek,
thanks for the quick reply.  This should be a simple task!  I changed the script to your approach but received an error message:
   Object: CreateObject Layer invalid data source

I think it has something to do with rasters being treated differently than feature classes or shape files.

not sure how to fix this

dale





Hi,
arcpy.MakeRasterLayer_management is meant to other purpose, so you can skip this. Code below should help. If you want to change layer name use:
addLayer.name = new_name
before adding layer to map doc.
fc_list=[fc_meanSun,fc_lowSun]
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]
for ras_name in fc_list:
    fc=fc_path+'\\'+ras_name
    addLayer = arcpy.mapping.Layer(fc)
    arcpy.mapping.AddLayer(df,addLayer)



Regards.
Arek
0 Kudos
DaleSchaper
Emerging Contributor
Hi Jake,
I have the geoprocessing option checked.  I think it does it by default.  The new rasters appear and then disappear.  But, I also had to rename them and create the new layers.  If I did them manually in the .mxd then they would be added from the .gdb.  Am I suppose to do a save or close a schema lock?  This seems as if it should be a simple and common task so I must be missing a basic point to it all.

thanks for your quick reply

dale



You could create a script that runs the 'Make Raster Layer' function for each raster.  If you have 'Add results of geoprocessing to the display" checked on within the Geoprocessing options, the raster layers will automatically be added to the current map document.

arcpy.MakeRasterLayer_management("C:/DATA/RASTER/DEM/example", "example")
arcpy.MakeRasterLayer_management("C:/DATA/RASTER/DEM/example2", "example2")
0 Kudos
DaleSchaper
Emerging Contributor
Hi Jake,
I just ran this simple script where there is a polygon feature class in the .gdb and want to add it to the TOC of the current .mxd using a tool script.  It tells me that I have an invalid data path.  That is the data path ... so what am I missing

thanks,
dale


mxd = arcpy.mapping.MapDocument("CURRENT")
    df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]


    addLayer = arcpy.mapping.Layer(r"F:\GIS 249\Wk02_Spatial Analyst\Thursday\Learn01\VirtualCampus\LearnSA9\MapAlgebra\Piru.gdb\FirePer")
    arcpy.mapping.AddLayer(df,addLayer)


results error message:

Start Time: Thu Oct 18 11:05:49 2012
Running script AddFireZonePoly...
<type 'exceptions.ValueError'>: Object: CreateObject Layer invalid data source
Failed to execute (AddFireZonePoly).
Failed at Thu Oct 18 11:05:51 2012 (Elapsed Time: 2.00 seconds)
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Hi Dale,

Your code appears correct.  What version of ArcGIS are you running?

Also, try specifying the UNC path rather than the network drive.  Ex:
addLayer = arcpy.mapping.Layer(r"\\<server name>\GIS 249\Wk02_Spatial  Analyst\Thursday\Learn01\VirtualCampus\LearnSA9\MapAlgebra\Piru.gdb\FirePer")
0 Kudos