How to make a Map Document (.mxd) a "Workspace" to do Geoprocessing

6139
1
Jump to solution
09-25-2014 09:03 AM
RolandShield
New Contributor II

I am trying to build some tools using Toolbox and Python for users who are in the ArcMap environment, in an ArcMap session. So the first thing I want to to is get a list of the feature classes and/or layers in the current "Data Frame", pass them into the Toolbox as a parameter, so that the user can then pick one (or more) of them and then begin doing stuff like comparing attributes between 2 feature classes and so forth.

But the problem or challenge is that a "Workspace" is a file folder or a Geodatabase or similar and NOT a "Data Frame". And this is not handy, because to the user, the "Data Frame" is, in fact a storage place for the things they wish to do geoprocessing against.

Does anyone have any ideas for developing a framework for using the Map Document, instead of traditional "Workspaces", as a place to assemble various feature classes, tables, etc. from various domains, file folders, file geodatabases, etc.?

Conceptually, I need to do something like this:

import arcpy
mxd = arcpy.mapping.MapDocument

arcpy.env.workspace = mxd

And this, so I can start doing things like creating lists and other sorts of things, but in the context of the map as the current workspace.

0 Kudos
1 Solution

Accepted Solutions
MicahBabinski
Occasional Contributor III

Hi Roland,

There are several methods within the arcpy.mapping module which allow you to list and manipulate elements of an mxd, including listing data sources, manipulating layer and data frame properties, and even the properties of the mxd itself.

For starters, a nice way to get a "quick and dirty" list of layer objects at the root level of your mxd is:

import arcpy mxd = arcpy.mapping.MapDocument("Current")

lyrList = arcpy.mapping.ListLayers(mxd)

To create a data frame object to which you can add layers (assuming you have only one data frame):

import arcpymxd = arcpy.mapping.MapDocument("Current")

df = arcpy.mapping.ListDataFrames(mxd)[0]

If you are dealing with lots of group layers and want to get at the data sources of all the sublayers:

import arcpy

mxd = arcpy.mapping.MapDocument("Current")

lyrList = arcpy.mapping.ListLayers(mxd)

for l in lyrList:

    lyrs = arcpy.mapping.ListLayers(l)

    for lyr in lyrs:

          if lyr.supports("DATASOURCE"):

              arcpy.addMessage(lyr.dataSource)

Once you have successfully listed/created the mxd, layer, and data frame objects, they can be passed to your custom geoprocessing tool or add-in toolbar as inputs or items in a GUI combo-box, etc.

I hope this is helpful and along the lines of what you were looking for. This section of the help documentation will show a lot more information about arcpy.mapping. There really is a ton you can do with it.

Regards,

Micah Babinski

View solution in original post

1 Reply
MicahBabinski
Occasional Contributor III

Hi Roland,

There are several methods within the arcpy.mapping module which allow you to list and manipulate elements of an mxd, including listing data sources, manipulating layer and data frame properties, and even the properties of the mxd itself.

For starters, a nice way to get a "quick and dirty" list of layer objects at the root level of your mxd is:

import arcpy mxd = arcpy.mapping.MapDocument("Current")

lyrList = arcpy.mapping.ListLayers(mxd)

To create a data frame object to which you can add layers (assuming you have only one data frame):

import arcpymxd = arcpy.mapping.MapDocument("Current")

df = arcpy.mapping.ListDataFrames(mxd)[0]

If you are dealing with lots of group layers and want to get at the data sources of all the sublayers:

import arcpy

mxd = arcpy.mapping.MapDocument("Current")

lyrList = arcpy.mapping.ListLayers(mxd)

for l in lyrList:

    lyrs = arcpy.mapping.ListLayers(l)

    for lyr in lyrs:

          if lyr.supports("DATASOURCE"):

              arcpy.addMessage(lyr.dataSource)

Once you have successfully listed/created the mxd, layer, and data frame objects, they can be passed to your custom geoprocessing tool or add-in toolbar as inputs or items in a GUI combo-box, etc.

I hope this is helpful and along the lines of what you were looking for. This section of the help documentation will show a lot more information about arcpy.mapping. There really is a ton you can do with it.

Regards,

Micah Babinski