2

This is my first time using this site and I'm a bit new to ArcPy. I've looked around for a while but couldn't quite find the answer I was looking for. I have many folders with georeferenced topos in JPEG format and I'm trying to use a for loop to add each one to an mxd one at a time. Here's my code:

import arcpy

arcpy.env.workspace = "C:\folder path"
mxd = arcpy.mapping.MapDocument("C:\file path.mxd")
df = arcpy.mapping.ListDataFrames(mxd,"*")[0]

path = "C:\file path\\"

rasterList = arcpy.ListRasters()

for jpg in rasterList:
    path += str(jpg)
    lyr = arcpy.mapping.Layer(path)
    arcpy.mapping.AddLayer(df, lyr, "TOP")

arcpy.RefreshActiveView()
arcpy.RefreshTOC()

The path string was my way of concatenating each raster list element to the folder path to be passed to arcpy.mapping.Layer() since it accepts a file path - it managed to run without errors but I don't see any changes in my mxd. I assume this is because arcpy.mapping.Layer() accepts a .lyr file only? With this logic I tried using arcpy.MakeRasterLayer_management() within the loop but I got an AssertionError and didn't know what that meant. Anyone know where I went wrong?

  • 2
    What is the context? Are you working with a standalone script and editing an mxd on disc? If so, you need to save the mxd for the changes to commit. – Ben S Nadler Jan 17 '16 at 2:21
  • Import env from arcpy. Env.workspace=path and proceed with listrasters. Amend path+=... to pnew=path+jpg. Also c\\file with double slashes – FelixIP Jan 17 '16 at 3:47
0

Modifying your script slightly, I had to add the path into the loop and add a save at the end:

import arcpy

arcpy.env.workspace = "C:\\FolderPath"
mxd = arcpy.mapping.MapDocument("C:\\FolderPath\\Rasters.mxd")
df = arcpy.mapping.ListDataFrames(mxd,"*")[0]

rasterList = arcpy.ListRasters()

for jpg in rasterList:
    path = "C:\\FolderPath\\"
    path += str(jpg)
    lyr = arcpy.mapping.Layer(path)
    arcpy.mapping.AddLayer(df, lyr, "TOP")

arcpy.RefreshActiveView()
arcpy.RefreshTOC()
mxd.save()

Alternatively if you want to run from within ArcMap you can remove the save and change the mxd to "CURRENT", which will allow you to save or not save the open Map Document:

import arcpy

arcpy.env.workspace = "C:\\FolderPath"
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd,"*")[0]

rasterList = arcpy.ListRasters()

for jpg in rasterList:
    path = "C:\\FolderPath\\"
    path += str(jpg)
    lyr = arcpy.mapping.Layer(path)
    arcpy.mapping.AddLayer(df, lyr, "TOP")

arcpy.RefreshActiveView()
arcpy.RefreshTOC()
  • as @FelixIP says above, including a pnew = path += str(jpg) and changing lyr to lyr = arcpy.mapping.Layer(pnew) would be tidier and would keep the path out of the loop. – Midavalo Jan 17 '16 at 4:34
  • Yep, I had to save the mxd among the other syntax edits you and @FelixIP suggested. Other than the issue of having to reopen the IDE for it to run, it worked. Now my issue is the symbology is incorrect and using arcpy.ApplySymbologyFromLayer_management() didn't seem to change anything but I suppose that will be the subject of another question. Thanks for your help! – cevondanady Jan 18 '16 at 5:26
1

I think your error is mainly in path formatting and string handling. Try this:

import arcpy
import os

arcpy.env.workspace = "C:\\folder_path"
mxd = arcpy.mapping.MapDocument("C:\\file_path.mxd")
df = arcpy.mapping.ListDataFrames(mxd,"*")[0]

path = "C:\\file_path"

rasterList = arcpy.ListRasters()

for jpg in rasterList:
    # This sets a new path with correct slashes each time:
    rastpath = os.path.join(path, jpg)
    lyr = arcpy.mapping.Layer(rastpath)
    arcpy.mapping.AddLayer(df, lyr, "TOP")

arcpy.RefreshActiveView()
arcpy.RefreshTOC()

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.