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!
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
You could use Spatial Join—Help | ArcGIS for Desktop
In most cases (mutually exclusive index features), you could use Spatial Join for this but not necessarily for more complex cases (e.g. coincident or overlapping index features).
I was thinking you could use Match Option HAVE_THEIR_CENTER_IN and get the desired results.
Yes, that would work most of the time, although some index features may not cover the center of the map extent, and the center of some map extents may cover multiple index features. Whether or not that matters would depend on why you wanted to know which feature produced which map extent, or if any would do.
I initially used the Spatial Join geoprocessing tool using the matching option HAVE_THEIR_CENTER_IN, however as Darren eluded to, some index features do not cover the center of the map extent while several others cover multiple index features. The CONTAINS option works similar as well, however there are several index features contained in each map extent so there will be a large amount of manual QC involved by going this route.
Old thread I know - but there is now an easy way in case anyone searching...
arcpy.Extent has a polygon property that neatly returns the extent as a polygon
ex <SPAN class="operator token">=</SPAN> extent<SPAN class="punctuation token">.</SPAN>polygon<SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.