Solved! Go to Solution.
old thread but i thought i would post my answer as it does not require an existing map doc
this does assume that you have arcMap installed and not just arcpy. but who is using arcpy w/o having ArcMap installed?
{
map_file = r'c:\test\test_map..mxd'
# open and close a file
open(map_file,'r').close()
# open the file with default application
os.startfile(map_file)
# sleep for a couple minutes to allow the file to be fully opened
time.sleep(120)
# kill the application in a pretty violent way yet not corrupting the file.
os.system("taskkill /im arcmap.exe /f")
# work with yout map
mxd = arcpy.mapping.MapDocument(map_file)
}
Not so much a workaround as much as a conscious decision...
import arcpy
import os
arcpy.env.workspace = r"c:\data\file.gdb"
my_mxd = arcpy.mapping.MapDocument(r"c:\path\to\my.mxd")
data_frame = arcpy.mapping.ListDataFrames(my_mxd)[0]
# Switch to data view
my_mxd.activeView = data_frame.name
# Load standard layer with symbology, append to TOC in first data frame
my_layer = arcpy.mapping.Layer(r"c:\path\to\some.lyr")
arcpy.mapping.AddLayer(data_frame, my_layer, 'TOP')
# Loop through rasters in workspace, swap out layer's source with raster's
for fcname in arcpy.ListRasters():
# Full path to raster
new_raster = os.path.join(arcpy.env.workspace, fcname)
# Set layer's source
my_layer.dataSource = new_raster
# Zoom to extent
df.extent = arcpy.Describe(new_raster).extent
# Save to disk
my_mxd.saveACopy("c:\\output\\out_%s.mxd" % fcname)
my_layer.dataSource = new_raster
my_layer.replaceDataSource(arcpy.env.workspace, 'RASTER_WORKSPACE', fcname[:-4])