Create polygon / fc from map extent

13008
16
11-21-2011 08:22 AM
MatthiasAbele
New Contributor II
Hallo,

there are several topics on different forums concerning this matter, but
I haven't found a hint how to this in ArcGIS 10.

Do you have an idea or a code snippet?

Thanx,

Matthias
Tags (2)
0 Kudos
16 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Matthias,

Here is a code sample.  The code grabs the extent from an MXDs data frame, creates an array, and then converts the array to a polygon feature class.

import arcpy
from arcpy import env
from arcpy import mapping

env.workspace = r"C:\temp\Test.gdb"

mxd = mapping.MapDocument(r"C:\temp\python\Philadelphia.mxd")
dataframe = mapping.ListDataFrames(mxd, "*")[0]
frameExtent = dataframe.extent
XMAX = frameExtent.XMax
XMIN = frameExtent.XMin
YMAX = frameExtent.YMax
YMIN = frameExtent.YMin
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)
polygon = arcpy.Polygon(array)
arcpy.CopyFeatures_management(polygon, "Polygon_Extent")
MatthiasAbele
New Contributor II
Hallo JSkinn3,

thany, the code works perfectly. Is it possible to loop through a folder, which contains several
mxds and store all polygons in one fc?

Yours,

Matthias
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Take a look at this thread.
0 Kudos
CraigPrisland1
New Contributor II

I have been looking for something like this for a little while now and the Python script that was posted here a few years back worked great.  Thnaks for that!  However, would it be possible to run something like this on multiple pages?  I used Data Driven Pages to create several pages and would like a feature class or shapefile of the map extent of all of these pages.  I noticed that this script will only output the first page.

Thanks in advance and I appreciate the assistance!

0 Kudos
DarrenWiens2
MVP Honored Contributor

You can modify Jake Skinner​'s code as below to accommodate DDPs (mostly tested):

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

extents = []  
mxd = mapping.MapDocument(r"C:\temp\python\Philadelphia.mxd")  
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
     mxd.dataDrivenPages.currentPageID = pageNum
     dataframe = mapping.ListDataFrames(mxd, "*")[0]  
     frameExtent = dataframe.extent  
     XMAX = frameExtent.XMax  
     XMIN = frameExtent.XMin  
     YMAX = frameExtent.YMax  
     YMIN = frameExtent.YMin  
     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)  
     polygon = arcpy.Polygon(array) 
     extents.append(polygon)

arcpy.CopyFeatures_management(extents, "Polygon_Extent") 
0 Kudos
CraigPrisland1
New Contributor II

Darren,

This is exactly what I was looking for.  Thank you very much!

0 Kudos
CraigPrisland1
New Contributor II

Darren,

This code you modified worked well.  I did have one other question if possible.  The "Polygon_Extent" layer that gets outputted does not contain any fields.  Is it possible to incorporate the field that was used to create the Data Driven Pages from (i.e. the index layer) into the outputted layer?

Thanks again

0 Kudos
DarrenWiens2
MVP Honored Contributor

As far as I know, you have to do a fair bit of recoding to incorporate attributes into this type of snippet. It can be done by creating a new feature class and adding the field before the loop, then adding features to the feature class inside the loop using an InsertCursor.

If someone know if it's possible to include attributes somehow using CopyFeatures, I would be eternally grateful. Something like this would be ideal, although I don't believe it's possible:

  extents.append([polygon, field1, field2, etc.])
arcpy.CopyFeatures_management(extents, "Polygon_Extent") 

0 Kudos
WesMiller
Regular Contributor III
0 Kudos