4

i wonder how i can add a raster layer to a remote (not the current) mxd file with arcpy. I need to not use a lyr file for that.

For vector Layer i have the following lines working fine :

mxdPath = "D:\FormationSpecialisee\IGN_IFI\doc1.mxd"
mxd = arcpy.mapping.MapDocument(mxdPath)
layer = arcpy.mapping.Layer(fcADD)
arcpy.mapping.AddLayer(df, layer , "AUTO_ARRANGE")

but it seems to not work for Raster Layer.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jul 21, 2011 at 10:02

4 Answers 4

11

You can create an in-memory RasterLayer by using arcpy.MakeRasterLayer to create the layer without an .lyr file and then add it to your DataFrame.

The following code will load a raster into a new raster layer, add it to the first dataframe of the specified mxd, and then save the mxd.

mxdPath = r'{path to mxd}'
rasterPath = r'{path to raster file}'
rasterLayerName = 'name to give new raster layer'
md = arcpy.mapping.MapDocument(mxdPath)
df = arcpy.mapping.ListDataFrames(md)[0]
result = arcpy.MakeRasterLayer_management(rasterPath, rasterLayerName)
layer = result.getOutput(0)
arcpy.mapping.AddLayer(df, layer, 'AUTO_ARRANGE')
md.save()
answered Jul 21, 2011 at 17:47
3

mxdPath = r"D:\FormationSpecialisee\IGN_IFI\doc1.mxd"

or

mxdPath = "D:/FormationSpecialisee/IGN_IFI/doc1.mxd"

or

mxdPath = "D:\\FormationSpecialisee\\IGN_IFI\\doc1.mxd"

For use in Python

answered Jul 21, 2011 at 11:05
3

In a python window, just write:

arcpy.MakeRasterLayer_management(rasterFileName, "tmpLyr")

where rasterFileName is a string containing the path and name of your raster.

answered Sep 18, 2012 at 8:41
2

You'll need to have a .lyr file pointing to a raster with your predefined symbology, then open it with arcpy.mapping.Layer(yourlayerfile), add it to your map like you have, then use one of the layer datasource update methods to swap out the old data source with the new one.

answered Jul 21, 2011 at 17:19

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.