Clipping by view extent

509
11
06-20-2012 07:32 AM
DevonJenkins
Occasional Contributor
Hi everyone,

I want to clip features based on my window extent. Right now I am creating a clip feature class polygon to match my view. instead of inputting "clip features" to clip, is there a syntax to input for the dataframe?

thanks
Tags (2)
0 Kudos
11 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Devon,

You could create a polygon object based on the MXD's data frame extent, and then use the object to clip the feature class.  Here is an example:

import arcpy
from arcpy import env
from arcpy import mapping
env.workspace = r"C:\temp\python\test.gdb"

mxd = mapping.MapDocument(r"C:\temp\python\County.mxd")
df = mapping.ListDataFrames(mxd)[0]
xmin = df.extent.XMin
ymin = df.extent.YMin
xmax = df.extent.XMax
ymax = df.extent.YMax

pnt1 = arcpy.Point(xmin, ymin)
pnt2 = arcpy.Point(xmin, ymax)
pnt3 = arcpy.Point(xmax, ymax)
pnt4 = arcpy.Point(xmax, ymin)

array = arcpy.Array()
array.add(pnt1)
array.add(pnt2)
array.add(pnt3)
array.add(pnt4)
array.add(pnt1)

poly = arcpy.Polygon(array)

arcpy.Clip_analysis("Hospitals", poly, "Hospitals_clip")
del mxd
0 Kudos
DevonJenkins
Occasional Contributor
Hey thanks for the reply. So everything works great up until I run the actually clip. It creates a shape file however it contains no features. Any thoughts?
0 Kudos
JakeSkinner
Esri Esteemed Contributor
One thing I forgot to mention is to make sure your data frame coordinate system matches that of your feature class.
0 Kudos
MathewCoyle
Frequent Contributor
Hi Devon,

You could create a polygon object based on the MXD's data frame extent, and then use the object to clip the feature class.  Here is an example:

import arcpy
from arcpy import env
from arcpy import mapping
env.workspace = r"C:\temp\python\test.gdb"

mxd = mapping.MapDocument(r"C:\temp\python\County.mxd")
df = mapping.ListDataFrames(mxd)[0]
xmin = df.extent.XMin
ymin = df.extent.YMin
xmax = df.extent.XMax
ymax = df.extent.YMax

pnt1 = arcpy.Point(xmin, ymin)
pnt2 = arcpy.Point(xmin, ymax)
pnt3 = arcpy.Point(xmax, ymax)
pnt4 = arcpy.Point(xmax, ymin)

array = arcpy.Array()
array.add(pnt1)
array.add(pnt2)
array.add(pnt3)
array.add(pnt4)
array.add(pnt1)

poly = arcpy.Polygon(array)

arcpy.Clip_analysis("Hospitals", poly, "Hospitals_clip")
del mxd


Hey Jake, just wondering what the reasoning is behind everything broken up like that? This is a similar code I use, I don't remember if I wrote it or stole it from someone. If I did steal it credit to them.
import arcpy

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]

extent = df.extent
array = arcpy.Array()

array.add(extent.lowerLeft)
array.add(extent.lowerRight)
array.add(extent.upperRight)
array.add(extent.upperLeft)
array.add(extent.lowerLeft)

polygon = arcpy.Polygon(array)
...
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Hi Matt,

No reasoning, just wrote it out when I replied to the thread so I didn't have time to simplify the script.  Yours will do exactly the same, just less lines of code.
0 Kudos
DevonJenkins
Occasional Contributor
Coordinate systems are the same.... still generated an empty output. There is only one data frame.... it seems as if my data isn't in the same area as the clipper...which isn't the case but that's the error I'm getting using both syntax from above:

"000117 Warning empty output generated:
A common occurrence of this warning occurs when the Extent (or XYDomain) environment has been set previously for a geographically distinct area. Since the Extent environment is used to limit the features used, an inappropriately set Extent environment could exclude all features."
0 Kudos
MathewCoyle
Frequent Contributor
Coordinate systems are the same.... still generated an empty output. There is only one data frame.... it seems as if my data isn't in the same area as the clipper...which isn't the case but that's the error I'm getting using both syntax from above:

"000117 Warning empty output generated:
A common occurrence of this warning occurs when the Extent (or XYDomain) environment has been set previously for a geographically distinct area. Since the Extent environment is used to limit the features used, an inappropriately set Extent environment could exclude all features."


It definitely works for me. How are you incorporating this script? Is it part of a larger tool or are you just executing this section in the python window?
0 Kudos
DevonJenkins
Occasional Contributor
Just using the code provided in the desktop python window.  Then clipping a feature that's in my window of contents with the poly variable created. Thats it!

import arcpy

#mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]

extent = df.extent
array = arcpy.Array()

array.add(extent.lowerLeft)
array.add(extent.lowerRight)#array.add(extent.upperRight)
array.add(extent.upperLeft)
array.add(extent.lowerLeft)

poly = arcpy.Polygon(array)

arcpy.Clip_analysis('Trapper Lines',poly,"Trapper_Extent")
0 Kudos
MathewCoyle
Frequent Contributor
It looks like you are only inputting 4 points to create your polygon, you need 5 points to create a closed rectangle.
0 Kudos