Monday 30 April 2018

arcpy - Automating capture of Extent from multiple mxds with multiple dataframes


I want to create a single shapefile 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 shapefile 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.



Can you help complete this script?


 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)

Even with this I get errors...


Traceback (most recent call last):

File "P:\2011\Job_031_TownPlanning_SeriesProduction\Working\mxd\extent_creation.py", line 32, in
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).

Answer



Thanks all, the code has been (fixed) and the basic functionality is working. I am still trying to get it to write the mxd file name, extent name and scale but the extent->polygon shp file works when run using IDLE in the same directory as the mxd's.


Would still appreciate help with the above though - I have posted a new question for this at How do I insert a DATAFRAME_ELEMENT value into a dynamically generated shapefile containing its extent?


(working) CODE in link above.



No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...