arcpy.mapping module problems.

1015
4
07-19-2012 02:58 PM
ChrisBates1
New Contributor
Hi All!

I have a few questions regarding the arcpy.mapping module that hopefully some of you can shed some light on.

I am currently working on a project for a fairly complicated raster processes for some non-GIS type people. As such I burried the script in a script tool to make it user friendly. Some have asked that once the process is complete - I output the resulting raster dataset into an ArcMap Document (MXD). It seems like this can be done using the mapping module, but alas issues keep coming up.

1. Do I have to have an MXD created before I can dump the outputs into it? I would love to open ArcMap using something like: subprocess.Popen([arcmap]) where arcmap is the path to the .exe. But then when I try refering to the document as: arcpy.mapping.MapDocument("CURRENT") it doesn't seem to recognize the document.

2. If I have to create a default MXD I will. but I would like to see the raster I add to the TOC without saving the document (it will be read only). Instead I have to use: mxd.saveACopy(mxdoutput) to see the rasters in the TOC, and I would rather the users just decide where they want to save the map to.

3. Is there any way to open a closed MXD using arcpy.mapping?

4. If i can't open a closed MXD and have to use subprocess.Popen to open ArcMap. I think I have to use Popen.wait to keep the process from killing itself. Is there anyway to set up some checks so that the script with stop waiting?

Chris
Tags (2)
0 Kudos
4 Replies
JasonScheirer
Occasional Contributor III
The mapping module isn't what you want for your script tool if you're outputting new data to the current map. You'll want to add an output parameter of type raster layer or raster dataset into your script. Then it will add itself to the table of contents once it's done.

arcpy.mapping.MapDocument("current") only works from within ArcMap. That means that child process can't get to it either.

If for some reason you really need to edit an MXD out of process and add things to it, then os.startfile is what you'll want. os.startfile(filename) will behave in the same way as if you'd double clicked the file in Windows Explorer.

I'm still not entirely sure as to what you are trying to accomplish. I think an output parameter is all you need here.
0 Kudos
ChrisBates1
New Contributor
Thanks for the quick response Jason. As you can probably tell it was the end of the day when I wrote my problem so maybe I can clarify a bit.

I am not interested in adding an output parameter to my script tool. For my purposes the script tool is a super simple GUI that will only ask the user for a few inputs. I have hard coded the names and locations of all of the outputs so I know where everything will end up.

I would like to try os.startfile (maybe i'll give it a shot this morning).

All that I am trying to accomplish is dumping the results of the script (5 rasters) into a blank MXD so that the user can take a look at the outputs and save the MXD if he or she chooses. They likely will not have and MXD open (which is why i was hoping to avoid using arcpy.mapping...) and will be running the script tool from catalog.

Thanks for your help though!

Chris
0 Kudos
markdenil
Occasional Contributor III
Arcpy.mapping cannot add an element to a map document, but it can manipulate elements that are already present.

Therefore, what you will want to do (to do it in arcpy.mapping), is to have an mxd prepared with place holding layers similar to the your yet-to-be-created output rasters, preferably with your prefered symbolization already applied.

You can then identify the layer in the data frame by the name you have given it, and perform method operations like
layer.replaceDataSource (to point the layer at your new raster)
or toggle the layer.visible property (to turn off layers you don't need or turn on ones you want to show).

You can also manipulate other layout elements other than the data frame: TEXT_ELEMENTs for instance.
These, again, have to already exist in the layout, but by using the elementPositionX and/or elementPositionY properties,
you can "puppet" them on of off the layout. (Say, add 100 to the elementPositionX to a text element you want to hide)
You can, of course, modify the text of a text element, too.

So arcpy.mapping is rather limited, but there is a good deal one can, with a bit of ingenuity, do within the limits.
To make new elements, and add new layers, and whatnot, you will need ArcObjects
(You can still do it in VBA with or without ArcObjects by requesting a free VBA-in-Arc licence to use VBA in arc 10 or 10.1 (and no further))
0 Kudos
JeffBarrette
Esri Regular Contributor
A slight correction to the last email.  It is correct in that arcpy.mapping can't create new MXDs but with arcpy.mapping you can add layers using the "AddLayer", "AddLayerToGroup", or InsertLayer" functions.  This would require having pre-authored layer files or a reference to a layer from another MXD.

Jeff
0 Kudos