Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

Thanks in advance.

Guillaume

share|improve this question
add comment

5 Answers

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()
share|improve this answer
add comment

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

share|improve this answer
add comment

In a python window, just write:

arcpy.MakeRasterLayer_management(rasterFileName, "tmpLyr")

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

share|improve this answer
add comment

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.

share|improve this answer
add comment

Are you certain that your path is correct?

D:\FormationSpecialisee\IGN_IFI\doc1.mxd should be

D:\\FormationSpecialisee\\IGN_IFI\\doc1.mxd or

D:/FormationSpecialisee/IGN_IFI/doc1.mxd

or its raw variant, in Python

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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