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.

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()
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

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.

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.

Your Answer

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

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