Automating capture of Extent from multiple mxds with multiple dataframes

3957
35
04-19-2011 03:14 PM
George_ChandeepCorea
New Contributor III
Hi,

I want to create a single shape file from multiple mxd's that have multiple frame sets with different extents in them. I have found/started a script to do this (attached) but can't figure out how to write the captured X&Y Max/Min into the shape file that is created for this. See output below. I also want it to write the scale and title of the frame as well as the file name of the mxd.

Would appreciate your help in completing this script.

Thanks,

>>> import arcpy, os, glob
... #path = 'c:\\temp\\george\\'
... path = 'P:\\2011\\Job_031_TownPlanning_SeriesProduction\\Working\\mxd\\1'
... os.chdir(path)
... mxds_List = glob.glob('*.mxd')
... count_Mapdocs = len(mxds_List)
... print 'Processing ' + str(count_Mapdocs) + 'map documents...'
... #Create Polygon Shapefile
... arcpy.CreateFeatureclass_management(path, 'extents.shp', "POLYGON")
... #Start Loop
... for mxd in mxds_List:
...     mapDoc = arcpy.mapping.MapDocument(mxd)
...     dataframe = arcpy.mapping.ListDataFrames(mapDoc,'*')[0]
...     frameExtent = dataframe.extent
...    
...     #Frame Scale
...     frameScale = dataframe.scale
...     #Frame Extent
...     ExtentXMax = frameExtent.XMax
...     ExtentXMin  = frameExtent.XMin
...     ExtentYXax  = frameExtent.YMax
...     ExtentYMin  = frameExtent.YMin
...    
...     point_object = mxd.shp
...     #Write in table scale
...     #Write in table

Processing 14map documents...

Runtime error <type 'exceptions.AttributeError'>: 'str' object has no attribute 'shp'
Tags (2)
0 Kudos
35 Replies
deleted-user-1T_bOHag6M8d
Occasional Contributor

This line

for pFrame in arcpy.mapping.ListDataFrames(mxd, "t*"):

should be

for pFrame in dataframes:

since you already created a list of dataframes in the line before it.

0 Kudos
HeatherCowley1
New Contributor

Fixed that line and re-imported it into ArcToolbox. It ran, but the shapefile is completely empty. No polygons or table data. So disappointing.

0 Kudos
deleted-user-1T_bOHag6M8d
Occasional Contributor

Hi Heather,

Take a close look at this line. See if you can figure out what the issue is.

if pFrame.name.lower == "Strip":
0 Kudos
HeatherCowley1
New Contributor

Looking at ListDataFrames—Help | ArcGIS for Desktop  and then DataFrame—Help | ArcGIS for Desktop , it looks like I should just change it to if pFrame.name == "Strip":. Did that and it opens the first layer, then tries to add the extent to the index, then fails.

Creating the new Map Index shapefile
Opening map document Z:\Projects\60132\Maps\Strip Maps\Arc\0027-2016.mxd
Adding the extent of Z:\Projects\60132\Maps\Strip Maps\Arc\0027-2016.mxd to the index.
Failed script MapIndex...

Traceback (most recent call last):
  File "C:\Users\heather\Documents\ArcGIS\AddIns\npeihl-mapindextool-0da4e723ca4a\Map Index Toolbox.tbx#MapIndex.py", line 76, in <module>
NameError: name 'rows' is not defined

Failed to execute (MapIndex).
Failed at Fri Sep 16 11:03:31 2016 (Elapsed Time: 4.23 seconds)

If I am counting correctly,

Line 75: rows = arcpy.InsertCursor(index, spatialRef)

Line 76: row = rows.newRow()

I went back to the original code, and it is not something I have modified.

0 Kudos
deleted-user-1T_bOHag6M8d
Occasional Contributor

Hi Heather,

I do not know why it is failing. Line 76 is the very last line where the rows variable is deleted. 

Perhaps we don't need to instantiate the InsertCursor on every iteration of the loop. You could try moving the creation of the rows InsertCursor out of the dataframes loop (perhaps directly under the creation of the index variable). You would just need to make sure the del rows line at the end is at the same indentation level as the creation of the InsertCusor.

But that doesn't explain why your code is failing, so I really don't know if it'll work. Welcome to the world of programming.

0 Kudos
HeatherCowley1
New Contributor

So, I moved it just like you suggested, and things seemed to work. It opened up all the files and added the extent to the index. However, after the last file, I got the same exact error, so it acted like it didn't finish. However, upon looking at the shapefile, it is all there. Crazy, but it works!  

Thank you!

0 Kudos