Select to view content in your preferred language

Zoom to FC

970
9
Jump to solution
10-15-2013 05:29 AM
JamesSmith7
New Contributor
When a Fields polygon is selected the script zooms to the polygon extent.  But, I want to return the AddMessage line if the correct Feature Class is not selected. The script does not get to the else statement for other Feature Classes. But, when another polygon or polyline Feature Class is selected it zooms to full extent, as expected. So, why does the script not use the else statement for Feature Classes other than Fields?

fds = arcpy.mapping.ListLayers(mxd, "Fields", df)[0] for fds in arcpy.mapping.ListLayers(fds):     desc = arcpy.Describe(fds)     type = desc.shapeType     if type == "Polygon":        df.extent = fds.getSelectedExtent()     else:        arcpy.AddMessage("Fields polygon not selected")
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Frequent Contributor
This line should only return the first layer in your dataframe.
lyr = arcpy.mapping.ListLayers(mxd, "", df)[0]


If you want to test if "Fields" has any features selected you can do this.

df = arcpy.mapping.ListDataFrames(mxd)[0] lyr = arcpy.mapping.ListLayers(mxd, "Fields", df)[0] desc = arcpy.Describe("Fields") if desc.FIDSet:     df.extent = lyr.getSelectedExtent() else:     arcpy.AddMessage("Fields polygon not selected")

View solution in original post

0 Kudos
9 Replies
MathewCoyle
Frequent Contributor
You are probably getting your specific issue from limiting your fds variable to "Fields" only. However, you have a few other issues as well.

Primarily, these lines are very confusing.

fds = arcpy.mapping.ListLayers(mxd, "Fields", df)[0]
for fds in arcpy.mapping.ListLayers(fds):


In the first line you assign your "Fields" layer to the fds variable. Then in the second line you create a new fds variable listing the layers in your fds variable. Try writing it a little cleaner using unique variable names.
0 Kudos
JamesSmith7
New Contributor
I removed the redundant statement.  I want to limit the selection to "Fields" or else return the AddMessage line.  Otherwise, the zoom will work for all Feature Classes. 

My modified code now returns the AddMessage for when "Fields" are selected and when the are not.  The script still zooms to selected "Fields" and zooms to full extent when other Feature Classes are selected.  How can I get the Message to only return when a Feature Class other than "Fields" is selected?  The message retuns approx 50 times.  I just need one instance of the Message.

          
for df in arcpy.mapping.ListDataFrames(mxd):
  for lyr in arcpy.mapping.ListLayers(mxd, "", df):
   if lyr.name == "Fields":
    df.extent = lyr.getSelectedExtent()
   else:
    arcpy.AddMessage("Fields polygon not selected")
0 Kudos
JamesSmith7
New Contributor
Ideas/suggestions/comments? 

Thanks.
0 Kudos
TimBarnes
Occasional Contributor III
Perhaps try explicitly stating the false condition with an elif?

for df in arcpy.mapping.ListDataFrames(mxd):
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
if lyr.name == "Fields":
    df.extent = lyr.getSelectedExtent()
elif lyr.name != "Fields":
    arcpy.AddMessage("Fields polygon not selected")
0 Kudos
JamesSmith7
New Contributor
Perhaps try explicitly stating the false condition with an elif?

for df in arcpy.mapping.ListDataFrames(mxd):
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
if lyr.name == "Fields":
    df.extent = lyr.getSelectedExtent()
elif lyr.name != "Fields":
    arcpy.AddMessage("Fields polygon not selected")


Thanks for the suggestion.  But, the AddMessage statement is returned when a "Fields" polygon is selected and when a polygon or polyline from another feature class is selected.  The AddMessage statement is still returned 50 times.

Changing the ListLayers from "" to "Fields" is the complete opposite.  The AddMessage statement is not returned for "Fields" or for another selected Feature Class.
0 Kudos
MathewCoyle
Frequent Contributor
Unless you have multiple "Fields" layers you don't need to loop through anything.

df = arcpy.mapping.ListDataFrames(mxd)[0]
lyr = arcpy.mapping.ListLayers(mxd, "Fields", df)[0]
df.extent = lyr.getSelectedExtent()
0 Kudos
JamesSmith7
New Contributor
Thanks, but I still do not understand the AddMessage issue.  Why does specifying "Fields" using ListLayers not return the AddMessage and leaving the ListLayers as "" returns the AddMessage for all Feature Class selections, including "Fields"?

lyr = arcpy.mapping.ListLayers(mxd, "Fields", df)[0] - Specifies "Fields" FC

lyr = arcpy.mapping.ListLayers(mxd, "", df)[0] - Includes all Layers within TOC
0 Kudos
MathewCoyle
Frequent Contributor
This line should only return the first layer in your dataframe.
lyr = arcpy.mapping.ListLayers(mxd, "", df)[0]


If you want to test if "Fields" has any features selected you can do this.

df = arcpy.mapping.ListDataFrames(mxd)[0] lyr = arcpy.mapping.ListLayers(mxd, "Fields", df)[0] desc = arcpy.Describe("Fields") if desc.FIDSet:     df.extent = lyr.getSelectedExtent() else:     arcpy.AddMessage("Fields polygon not selected")
0 Kudos
JamesSmith7
New Contributor
The describe is what I needed.  Thanks!!!
0 Kudos