I have created a data consolidation tool. It consolidates a users mxd's vector and raster data into a project structure. It also allows the user to optionally cache our companies ArcGIS Server Web Maps. The arcpy.gp.ExportMapServerCache produces a compact cache dataset, which is turned into a layer with arcpy.MakeRasterLayer_management and added to an mxd with arcpy.mapping.AddLayer.
The layer is successfully added to the mxd. But it's not the same as when the user does it manually. The arcpy layers "data source" is not the one that was dictated to it, rather it references a temp in the users application data folder instead.
Why does it reference a temp, and NOT the actual raster cache?
### MOVE CONSOLIDATED MXD TO PROJECT CORE LEVELS
MasterMxdPath = UserProjectPath+"\\Extract_"+Project_Name+".mxd" #new mxd name
mxd2Name = UserProjectPathGDB+"\\v101\\"+mxdNAME #get consolidated mxd
mxd2 = arcpy.mapping.MapDocument(mxd2Name)
mxd2.saveACopy(MasterMxdPath, "10.1")
del mxd2
### ADDING WEB CACHE TO MXD
try:
UserProjectPathCache = UserProjectPath+"\\Cache"
arcpy.env.workspace = UserProjectPathCache
RastersL = arcpy.ListRasters("*", "ALL")
RcCount = len(RastersL)
if RcCount >0: ### If there's a web cache:
arcpy.AddMessage(" J: Web Cache Exists:")
arcpy.AddMessage(" J: There are: " +str(RcCount) +" rasters")
### Get Dataframe From MXD
mxd3 = arcpy.mapping.MapDocument(MasterMxdPath)
df = arcpy.mapping.ListDataFrames(mxd3)[0]
### Turn Rasters Into Layers & Add To Dataframe
CNTR = 0
for raster in RastersL:
arcpy.AddMessage(" J: Cache Name: "+str(raster))
CNTR = CNTR + 1
RasterLayerName = "WebServiceCache_"+str(CNTR)
RasterPath = UserProjectPathCache+"\\"+raster
arcpy.AddMessage(" J: New Layer Name: "+ RasterLayerName)
RLayer = arcpy.MakeRasterLayer_management(RasterPath,RasterLayerName)
layer = RLayer.getOutput(0)
arcpy.mapping.AddLayer(df, layer, 'Bottom')
mxd3.save()
arcpy.AddMessage(" J: Mxd Saved With Cache Layer")
del RLayer, layer, df
del mxd3
### Remove Webservices From Dataframes in MXD
mxd4 = arcpy.mapping.MapDocument(MasterMxdPath)
df2 = arcpy.mapping.ListDataFrames(mxd4)[0]
for lyr2 in arcpy.mapping.ListLayers(mxd4, "", df2):
if lyr2.supports("SERVICEPROPERTIES"):
servProp2 = lyr2.serviceProperties
if lyr2.serviceProperties["ServiceType"] != "SDE":
arcpy.AddMessage(" J: Removing WEB SERVICE LAYER: " + lyr.name)
arcpy.mapping.RemoveLayer(df2, lyr2)
mxd4.save()
arcpy.AddMessage(" J: Final Mxd Saved: ")
del mxd4
else: ### If there's no web cache
arcpy.AddMessage("J: No Web Cache Exists:")
except Exception as e:
arcpy.AddError(e.message)
arcpy.AddMessage(" DID NOT ADD RASTER TO MXD")