Selecting Data Frames for Python Scripting

3514
7
08-29-2011 10:50 AM
ChadFoster
Occasional Contributor
I have a .mxd that contains 5 data frames.  I am trying to write a script that will first select the appropriate data frame for the script to process.  Is this possible?
Tags (2)
0 Kudos
7 Replies
JakeSkinner
Esri Esteemed Contributor
You can do this by specifying an index value.  Say, for example, you want to select the data frame called 'Philadelphia', and it's the 3rd data frame in your MXD.  You would use the following:

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


The first data frame in your MXD starts with index 0.  Here is some further information:

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00s30000001p000000.htm
0 Kudos
ChadFoster
Occasional Contributor
ok, i get that syntax and it appears like it is working but I would like to select and activate that data frame.  Maybe my question should have been, can I activate a data frame for my script to work?
0 Kudos
deleted-user-1T_bOHag6M8d
Occasional Contributor
ok, i get that syntax and it appears like it is working but I would like to select and activate that data frame.  Maybe my question should have been, can I activate a data frame for my script to work?


What are you trying to accomplish in the selected dataframe?
0 Kudos
ChadFoster
Occasional Contributor
I have a MXD with 5 data frames, the main data frame is running the Data Driven Pages for my Assessor Parcel Maps.  The second data frame shows the Township and Range of the page, third data frame shows the section within the township and range, and the fourth data frame shows the different divisions within the section.  I want to write a script that will prompt the user for those three pieces of information - TR, SECTION, and DIVISION - and have the page update automatically. 

In my limited Python Scripting I am having trouble selecting layers in non active data frames. 

The next step will be to map the selections according to users input......

import arcpy
mxd = arcpy.mapping.MapDocument ('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd) [1]
mxd.activeView = df
layer = arcpy.GetParameterAsText (0)
grid = arcpy.GetParameterAsText (1)
arcpy.AddMessage (grid)
selstring = "TAG LIKE " + "'" + grid + "'"
arcpy.AddMessage("Selection string = " + selstring)
arcpy.SelectLayerByAttribute_management (layer, "NEW_SELECTION", selstring)
0 Kudos
deleted-user-1T_bOHag6M8d
Occasional Contributor
I have a MXD with 5 data frames, the main data frame is running the Data Driven Pages for my Assessor Parcel Maps.  The second data frame shows the Township and Range of the page, third data frame shows the section within the township and range, and the fourth data frame shows the different divisions within the section.  I want to write a script that will prompt the user for those three pieces of information - TR, SECTION, and DIVISION - and have the page update automatically. 

In my limited Python Scripting I am having trouble selecting layers in non active data frames. 

The next step will be to map the selections according to users input......

import arcpy
mxd = arcpy.mapping.MapDocument ('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd) [1]
mxd.activeView = df
layer = arcpy.GetParameterAsText (0)
grid = arcpy.GetParameterAsText (1)
arcpy.AddMessage (grid)
selstring = "TAG LIKE " + "'" + grid + "'"
arcpy.AddMessage("Selection string = " + selstring)
arcpy.SelectLayerByAttribute_management (layer, "NEW_SELECTION", selstring)


Sounds to me like you need to run the SelectLayerByAttribute on each dataframe by constructing a for loop for ListDataFrames.

For example:
import arcpy
layer = arcpy.GetParameterAsText (0)
grid = arcpy.GetParameterAsText (1)
arcpy.AddMessage (grid)
selstring = "TAG LIKE " + "'" + grid + "'"
arcpy.AddMessage("Selection string = " + selstring)
mxd = arcpy.mapping.MapDocument ('CURRENT')
dfs = arcpy.mapping.ListDataFrames(mxd)
for df in dfs:
    mxd.activeView = df
    arcpy.SelectLayerByAttribute_management (layer, "NEW_SELECTION", selstring)
0 Kudos
ChadFoster
Occasional Contributor
When I try that code for selecting a different data frame it disappears from my table of contents.
0 Kudos
deleted-user-1T_bOHag6M8d
Occasional Contributor
When I try that code for selecting a different data frame it disappears from my table of contents.


I'm not sure I understand. The data frame disappears? As in, it gets deleted from the mxd?
0 Kudos