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?

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

    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')
            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)
        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.AddMessage(" DID NOT ADD RASTER TO MXD")

Image Showing Layer Paths

  • This issue occurs with all our ArcGIS builds including 10.1 and 10.2. It occurs at every test location across our global testing teams. I discovered that when I run a simple script containing only a loop that add's rasters to an mxd it works as intended. I then painstakingly managed memory leaks in the main script by ensuring unneeded parameters and variables were deleted once they were no longer needed. This was not effective either. ...Esri Inc can't replicate this issue. – GISI Feb 27 '15 at 2:58

Here is your answer:

As you are trying to add a Cached Image Folder, you must use MakeImageServerLayer_management instead of MakeRasterLayer_management.

Here is the simple snippet to do what you need. You can simply paste it into the python window and see the response. just change the path to your compact cache folder (Raster):

import arcpy

mxd = arcpy.mapping.MapDocument("CURRENT")
dataFrame = arcpy.mapping.ListDataFrames(mxd, "*")[0]
layerpath = r"D:\Cache\World Cities Population"
RLayer = arcpy.MakeImageServerLayer_management(layerpath ,"ImageServerCacheLayer")
arcpy.mapping.AddLayer(dataFrame, "ImageServerCacheLayer", "BOTTOM")

I hope it helps.

| improve this answer | |
  • I tested this. An extracted raster cache is not a valid input parameter, it only accepts image service strings. It was a good idea. – GISI Feb 27 '15 at 2:31
  • I know that as it is mentioned in the documentation, it only accepts image service strings, but I tested it with a compact cache and it worked. and the datasource of the raster was the real datasource and not the one in a temp folder. – Farid Cheraghi Feb 27 '15 at 8:44
  • With an Exploded cache folder,it works fine too. – Farid Cheraghi Feb 27 '15 at 9:28

As it turns out this is caused by an "issue" in the env.settings. I used arcpy.ResetEnvironments() and arcpy.ClearEnvironment("workspace") before using the arcpy.arcpy.MakeRasterLayer_management and everything worked as expected.


    AddRasterMxd = arcpy.mapping.MapDocument(MasterMxdPath)
    AddRasterdf = arcpy.mapping.ListDataFrames(AddRasterMxd)[0]
    arcpy.env.workspace = UserProjectPathCache
    RasterList = []
    count = 0
    arcpy.AddMessage("    - Adding Raster Cache To Extract Mxd:")
    for dirname, dirnames, filenames in os.walk(UserProjectPathCache):
        for subdirname in dirnames:
            arcpy.env.workspace = os.path.join(dirname, subdirname)
            RasterList = arcpy.ListRasters("Layers", "")
            RasterLayerName = subdirname
            for raster in RasterList:
                if raster == "Layers":
                    count = count + 1
                    arcpy.AddMessage("      - Raster "+str(count)+" Name: " +str(subdirname))
                    RLayer = arcpy.arcpy.MakeRasterLayer_management(raster,RasterLayerName)
                    FinalRasterLayer = RLayer.getOutput(0)
                    arcpy.mapping.AddLayer(AddRasterdf, FinalRasterLayer, 'Bottom')

    arcpy.env.spatialreference = StoredSR
    del AddRasterMxd
    del AddRasterdf
    del RLayer
    del FinalRasterLayer
| improve this answer | |

There is a bug called NIM082145 - In-memory raster layer is missing source information and tabs are not all correct.

A .afr file is created instead of a .lyr file. This issue has been found in 10.1 and in fixed in 10.1 SP1.

So, easy fix: upgrade to 10.2/3 or just install 10.1 SP1 from here.

| improve this answer | |
  • I have tested on ArcGIS Service Pack: 1 (build and it's still an issue. Our testing team have also tested on ArcGIS 10.2 and again it's still an issue. On a side note, I had noticed the effects of this NIM (pre SP1) and did not realize it was a bug. – GISI Feb 27 '15 at 3:15

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.