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")
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!
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")
Darren,
This is exactly what I was looking for. Thank you very much!
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
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")
You could use Spatial Join—Help | ArcGIS for Desktop