0

I am trying to add new raster layers to an existing .mxd (and then eventually remove the old ones, once I've updated the new raster layer's symbology to match the old ones). I'm running into an issue where the InsertLayer line is returning this error:

Traceback (most recent call last): File "C:\Users\bpaladino2\Documents\GRACE_Forecast_Test\AddToMxd.py", line 24, in arcpy.mapping.InsertLayer(df, ref_layer, new_layer) File "C:\Program Files (x86)\ArcGIS\Desktop10.8\ArcPy\arcpy\utils.py", line 182, in fn_ return fn(*args, **kw) File "C:\Program Files (x86)\ArcGIS\Desktop10.8\ArcPy\arcpy\mapping.py", line 1432, in InsertLayer assert isinstance(insert_layer, Layer) AssertionError

Here is the code I am working with (so far):

import arcpy
from arcpy import env
import os

env.workspace = "C:\\Users\\bpaladino2\\Documents\\GRACE\\ForecastTest\\output\\archive\\TEST"

path = "C:\\Users\\bpaladino2\\Documents\\GRACE\\ForecastTest\\output\\archive\\TEST\\" #Path to Raster

mxd = arcpy.mapping.MapDocument("C:\\Users\\bpaladino2\\Documents\\GRACE\\ForecastTest\\GRACE_FO_GWS.mxd") 

raster_list = arcpy.ListDatasets("*","Raster")

##for line in raster_list:
    #print line

raster = raster_list[0]

##print raster

df = arcpy.mapping.ListDataFrames(mxd)[0]
new_raster = arcpy.management.MakeRasterLayer(raster, "GWS_30D.tif")
new_layer = arcpy.management.SaveToLayerFile(new_raster, "GWS_30D.lyr")
ref_layer = arcpy.mapping.ListLayers(df)[2]
arcpy.mapping.InsertLayer(df, ref_layer, new_layer)


#print ref_layer

Any insight?

1 Answer 1

0

You might check that new_layer or ref_layer are indeed raster layers.

Or this might work.

new_raster = arcpy.management.MakeRasterLayer(raster, "GWS_30D.tif").getOutput(0)
new_layer = arcpy.management.SaveToLayerFile(new_raster, "GWS_30D.lyr")
ref_layer = arcpy.mapping.ListLayers(df)[2]
arcpy.mapping.InsertLayer(df, ref_layer, new_raster)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.