clip to shape

1792
2
Jump to solution
07-25-2016 08:24 PM
王伟
by
New Contributor

How can i use python to set the dataframe's range as a shapefile,hope for you reply

0 Kudos
1 Solution

Accepted Solutions
RebeccaStrauch__GISP
MVP Emeritus

Check out the thread Create a polygon from data frame which has several options for creating a polygon from an extent.

Darren Wiens code (copied below) might be you simplest solution, just need to change the output to a shapefile.

mxd = arcpy.mapping.MapDocument("CURRENT") # map  
df = arcpy.mapping.ListDataFrames(mxd)[0] # get 1st data frame  
sr = df.spatialReference # get spatial reference  
ext = df.extent # extent object  
BL = arcpy.Point(ext.XMin,ext.YMin) # bottom left  
BR = arcpy.Point(ext.XMax,ext.YMin) # bottom right  
TR = arcpy.Point(ext.XMax,ext.YMax) # top right  
TL = arcpy.Point(ext.XMin,ext.YMax) # top left  
df_poly = arcpy.Polygon(arcpy.Array([[BL,BR,TR,TL,BL]]),sr) # create polygon  
arcpy.CopyFeatures_management(df_poly,r'in_memory\poly') # write to disk  

If that is not what you are trying to do, you may need to supply additional information (Desktop or Pro, version, etc)

View solution in original post

2 Replies
NeilAyres
MVP Alum

Do you have any experience of writing geometries?

Start here :

Writing geometries—Help | ArcGIS for Desktop

And see the various properties of the data frame in the mapping module.

ArcGIS Help 10.1

RebeccaStrauch__GISP
MVP Emeritus

Check out the thread Create a polygon from data frame which has several options for creating a polygon from an extent.

Darren Wiens code (copied below) might be you simplest solution, just need to change the output to a shapefile.

mxd = arcpy.mapping.MapDocument("CURRENT") # map  
df = arcpy.mapping.ListDataFrames(mxd)[0] # get 1st data frame  
sr = df.spatialReference # get spatial reference  
ext = df.extent # extent object  
BL = arcpy.Point(ext.XMin,ext.YMin) # bottom left  
BR = arcpy.Point(ext.XMax,ext.YMin) # bottom right  
TR = arcpy.Point(ext.XMax,ext.YMax) # top right  
TL = arcpy.Point(ext.XMin,ext.YMax) # top left  
df_poly = arcpy.Polygon(arcpy.Array([[BL,BR,TR,TL,BL]]),sr) # create polygon  
arcpy.CopyFeatures_management(df_poly,r'in_memory\poly') # write to disk  

If that is not what you are trying to do, you may need to supply additional information (Desktop or Pro, version, etc)