Select to view content in your preferred language

Create blank MXD from python?

14654
11
Jump to solution
03-26-2010 02:46 PM
AndrewRudin
Deactivated User
I was looking through the new ArcPy functions, and I didn't see a function or Toolbox tool to just create a new blank MXD from scratch.  Does something like this exist?  Seems like a great function to have as a starting point for map manipulation.
1 Solution

Accepted Solutions
DanielSmith
Frequent Contributor

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)

}

View solution in original post

0 Kudos
11 Replies
JasonScheirer
Esri Alum
The workflow for this is to use an already saved blank MXD on the filesystem, open it, and use MapDoc.saveAs to have a new, blank map document to work with.
0 Kudos
AndrewRudin
Deactivated User
Thanks for the solution.  I thought of that as well, but to me that sounds more like a workaround. 

If that's the way to do it, then great.  Just seemed like a gap in the functionality and wanted to bring it up.  There are lots of functions and tools for modifying and packaging up MXD's, but no way to create a blank one to start manipulating from the beginning... using code.

Thanks!
JasonScheirer
Esri Alum
Not so much a workaround as much as a conscious decision: the default legal-sized page layout with full-page map frames makes sense in a Desktop environment, but it is not the most appropriate layout or page size to be used across the board. The onus is on the author of the map scripts of the decision of map layout template to be used -- this may seem like an inconvenience on the surface, but we anticipate it will result in much higher quality maps in automation than if a default were forced on users as it integrates the design of the map document's layout into the thought process of providing map automation services to end users of the services.
0 Kudos
JoshWhite
Honored Contributor
While I can understand the desire for this, I'm not sure why you would want to use Python to  open a blank mxd when you can just as easily fire up a brand new ArcMap session through your operating system.
Josh White, AICP
Principal Planner

City of Arkansas City
0 Kudos
JasonScheirer
Esri Alum
You can always use the templates in the system as a starting point as well.

starting_point = arcpy.mapping.MapDocument(os.path.join(arcpy.GetInstallInfo()['InstallDir'], 'bin', 'Templates', 'LetterLandscape.mxt'))
AndrewRudin
Deactivated User
Thanks for the responses.  I guess I was taking the angle of creating something like the old ArcInfo Workstation ArcEditor AML equivelant of opening arcmap, in which you basically chose which layers you wanted and their symbology in the script and your map was created 'on the fly' every time.  This way I can manage the SDE login and version components of the layers dynamically for every user in the script.

At my office we use individual SDE logins for each user, and we have several servers, so creating MXD's with passwords stripped out of layers gets pretty annoying for end users.  And operating system authentication is apparantly out of the question according to our DBA's.

I'm sure I'll figure something out.... if I ever do this.
0 Kudos
RichardSutton
New Contributor
Not so much a workaround as much as a conscious decision...


similiar issue - I need to be able to generate (from python) an mxd containing a FGDB raster layer symbolized with a standard .lyr file.   I need to do this dozens or hundreds of times with different rasters symbolized identically.  Needs to be a data view (not layout) with an extent zoomed to the raster.   All of the gp. functions up to this point work great - I can maneuver the data into perfect shape - but I can't see the elegant way to 1) open/create mxd 2) add FGDB raster layer 3) import .lyr and  4) save from python.  

Any help would be enormously appreciated.
0 Kudos
JasonScheirer
Esri Alum
So something like this:

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)
0 Kudos
R_Spitzer
Esri Contributor
Hi,

thx for the example. I just needed to change 1 line:
 my_layer.dataSource = new_raster

to
my_layer.replaceDataSource(arcpy.env.workspace, 'RASTER_WORKSPACE', fcname[:-4])


Kind Regards,
Rainer
0 Kudos