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 have found a workaround to incorporate the attributes into the Polygon_Extent output from the index layer from Data Driven Pages by first sorting my map in ascending alphabetical order and then running the script. The polygons are then created in order. By using the FID field, I can then join it with the index layer when I create a temporary field in that layer and add ascending values into this field.
I did have one other question to see if this is feasible. My map pages have an engineering scale tied to each page (i.e 1" = 210') using the following expression in the text:
SCALE: 1" = <dyn type="dataFrame" name="DataFrameName"
property="scale" pageUnits="in" mapUnits="ft" pageValue="1"
decimalPlaces="0"/>'
Would it be possible to get this value into the Polygon_Extent output file as well?
Thanks again for your assistance.
I believe you can grab the scale inside the loop, once you've got your dataframe object, using:
scale = dataframe.scale
This will return an absolute scale for the dataframe, like 5000 would equal a scale of 1:5000. You could then do some math to calculate how many feet would equal one inch. For example:
scale = dataframe.scale # e.g. 5000 engScale = '1 inch = ' + int(scale/12) + ' feet' # returns "1 inch = 417 feet"
You can escape quotes inside the string, but that's an extra step for me.
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>
サインインしたメンバーは投稿、更新のフォローなどができます。初めてですか?無料アカウントを登録してください。
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.