Select to view content in your preferred language

Automating capture of Extent from multiple mxds with multiple dataframes

4234
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
AlessandroCinnirella
New Contributor III
the problem in you code is mxd.shp is interpreted as mxd = variable (defined in the for loop "for mxd in mxds_List") and .shp is interpreted as an attiribute of your mxd variable.

try changing the name of the point object.

Ciao,
AC
0 Kudos
George_ChandeepCorea
New Contributor III
Thanks,

When I use   
arcpy.CreateFeatureclass_management(path, 'mxd2.shp', "POLYGON")
point_object = mxd2.shp


I get...

Processing 14map documents...
Traceback (most recent call last):
  File "P:\2011\Job_031_TownPlanning_SeriesProduction\Working\extent.py", line 29, in <module>
    point_object = mxd2.shp
NameError: name 'mxd2' is not defined
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Here is some sample code that I was able to get working. You will need to create an array and add the XMAX, XMIN, etc to the array to create the polygon feature. Then you can create a feature class/shapefile from this array. I had trouble getting the Append tool to work with the array feature so I created a feature class for each extent, merged them together, and then deleted the original extent feature classes.

import arcpy, glob, os
from arcpy import env
from arcpy import mapping
env.overwriteOutput = True

path = r"C:\temp"
mxdList = glob.glob(path + "\*.mxd")

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

y = 1

for mxd in mxdList:
    mxd2 = mapping.MapDocument(mxd)
    dataframe = mapping.ListDataFrames(mxd2, "*")[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" + "_" + str(y))
    y = y + 1


list = []

lstFCs = arcpy.ListFeatureClasses("Polygon_Extent*")
for fc in lstFCs:
    list.append(fc)

arcpy.Merge_management(list, "Extent")

for item in list:
    arcpy.Delete_management(item)
0 Kudos
George_ChandeepCorea
New Contributor III
J, Thanks for the code...

When I run it I got the following

>>>
Traceback (most recent call last):
  File "P:\2011\Job_031_TownPlanning_SeriesProduction\Working\mxd\extent_creation.py", line 32, in <module>
    arcpy.CopyFeatures_management(polygon, "Polygon_Extent" + "_" + str(y))
  File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\management.py", line 1943, in CopyFeatures
    raise e
ExecuteError: ERROR 000210: Cannot create output Polygon_Extent_1
Failed to execute (CopyFeatures).

>>>

I tried to open/close the file

    polygon = arcpy.Polygon(array)
    TempFile = open(polygon, "Polygon_Extent" + "_" + str(y)) # open file
    arcpy.CopyFeatures_management(polygon, "Polygon_Extent" + "_" + str(y))
    TempFile.close() # close file
    y = y + 1 


but then get
>>>
Traceback (most recent call last):
  File "P:\2011\Job_031_TownPlanning_SeriesProduction\Working\mxd\extent_creation.py", line 32, in <module>
    TempFile = open(polygon, "Polygon_Extent" + "_" + str(y)) # open file
TypeError: coercing to Unicode: need string or buffer, Polygon found
>>>

I am using Python 2.65 which came with ArcGIS 10.

Thanks for your assistance,
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Do you have your env.workspace set to a File Geodatabase?  Can you post the entire code you are using?
0 Kudos
deleted-user-1T_bOHag6M8d
Occasional Contributor
I recently created an application very similar to what you are looking for. However, my application only captures the largest dataframe in each map. But I think you could easily adapt my code using a for loop for the dataframes. I've attached a zip file of my application.

You'll find my code in the Scripts folder.
0 Kudos
George_ChandeepCorea
New Contributor III
Do you have your env.workspace set to a File Geodatabase?


Nope. How would I do this?

Can you post the entire code you are using?


It is the same that you sent me with just the changes I included.

Thanks,
0 Kudos
JakeSkinner
Esri Esteemed Contributor
To set the environment workspace, you will use arcpy.env.workspace.  Ex:

arcpy.env.workspace = r"C:\data\Philadelphia.gdb"

It will still help to post all of your code.  I can check to see if there are any errors with the changes you made.
0 Kudos
George_ChandeepCorea
New Contributor III
Thanks Jake.

Please see the attached script and rar file with some of the MXD's I am trying to get the extents from. I appreciate your time.

With this code I now get the following errors. I'm not sure why some values (YMax/Min? and Pnt3&4) get reported as Not a Number (NaN)

>>>
P:\2011\Job_031_TownPlanning_SeriesProduction\Working\mxd\1
<geoprocessing Map object object at 0x15D750A0>
<geoprocessing Data Frame object object at 0x15D7B4A8>
219157.947938379 8072842.27839025 377350.947938381 8205437.27839025 NaN NaN NaN NaN
377350.947938 219157.947938 8205437.27839 8072842.27839
219157.947938379 8072842.27839025 NaN NaN 219157.947938379 8205437.27839025 NaN NaN 377350.947938381 8205437.27839025 NaN NaN 377350.947938381 8072842.27839025 NaN NaN
Traceback (most recent call last):
  File "P:/2011/Job_031_TownPlanning_SeriesProduction/Working/mxd/1/extent_creation2.py", line 38, in <module>
    TempFile = open(polygon, "Polygon_Extent" + "_" + str(y)) # open file
TypeError: coercing to Unicode: need string or buffer, Polygon found
>>>

best,
0 Kudos