0

I am trying to add raster layers in specific data frames @current mxd. The script works in the ArcGIS 10.3 Python console but when I created a custom toolbox and add the script in the custom toolbox the script tool neither works nor gives an error.

Here is the script:

import arcpy,os
from arcpy import env


mxd=arcpy.mapping.MapDocument('Current')
workspace=arcpy.GetParameterAsText(0)
dflist=arcpy.mapping.ListDataFrames(mxd)
listraster=arcpy.ListRasters()
for raster in listraster:
    lyr=arcpy.mapping.Layer(raster)
    if str(lyr)=='om_in_percent':
        arcpy.mapping.AddLayer(dflist[0],lyr)
    elif str(lyr)=='cec_status':
        arcpy.mapping.AddLayer(dflist[1],lyr)
    elif str(lyr)=='n_inpercent':
        arcpy.mapping.AddLayer(dflist[2],lyr)
    elif str(lyr)=='si_status':
        arcpy.mapping.AddLayer(dflist[3],lyr)
    else:
        print(arcpy.GetMessage(1))

Any Help?

  • 1
    What was the error saying? Please edit your question to add the error message. – ahmadhanb Aug 2 '17 at 9:05
  • 1
    do you care introducing print from line 4, this would help in debugging.. – saviour123 Aug 2 '17 at 9:11
1

You seem to be missing a step to set the workspace to that provided by the input from the tool.

Try:

import arcpy,os
from arcpy import env
mxd=arcpy.mapping.MapDocument('Current')
workspace=arcpy.GetParameterAsText(0)
dflist=arcpy.mapping.ListDataFrames(mxd)

# Set workspace before listing rasters
arcpy.env.workspace = workspace
listraster=arcpy.ListRasters()
#...
  • @Amsalu No problem! If you're happy that this has answered your question, would you mind accepting it by clicking the tick to the left. – Ali Aug 3 '17 at 10:32

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.