Select to view content in your preferred language

Set Environment to Current Extent

958
3
06-11-2012 10:49 PM
JulianInskip
Frequent Contributor
I am trying to set the environment for my output files to my current map extent so that when I run a Select_analysis it only uses the features in the current extent.

I found the following code on a previous forum and thought I could use it to set my env.extent:
--------------------------------------------------------
import arcpy
import arcpy.mapping


## Sets the MXD file
IMXD = arcpy.mapping.MapDocument("CURRENT")
## Sets the Dataframe
DF = arcpy.mapping.ListDataFrames(IMXD, "Layers")[0]

print DF.extent

--------------------------------------------------------
This works well and shows me the extent of my current map extent.

I tried to modify it slightly to use the extent output to set the env.extent so that whenever I run the script it will only use the current extent (I know that this will limit me to only running it in ArcMap with a specific extent)
--------------------------------------------------------
import arcpy
import arcpy.mapping


## Sets the MXD file
IMXD = arcpy.mapping.MapDocument("CURRENT")
## Sets the Dataframe
DF = arcpy.mapping.ListDataFrames(IMXD, "Layers")[0]
arcpy.env.extent = "DF"

--------------------------------------------------------

This is incorrect and gives me an error (Runtime error <type 'exceptions.RuntimeError'>: UNKNOWN).

I have clearly done something wrong here. Am I on the right path or way off target here?

Thanks for any help.
Julian.
Tags (2)
0 Kudos
3 Replies
MarcinGasior
Frequent Contributor
Try to pass the extent object from data frame to environment setting.
This code works for me:
arcpy.env.extent = DF.extent
0 Kudos
NobbirAhmed
Esri Regular Contributor
Set extent as:

arcpy.env.extent = DF.extent
0 Kudos
JulianInskip
Frequent Contributor
WOW, that was an exceptionally fast reply.

Thanks Marcin and Nobbir, your replies worked and tool is working brilliantly now.