arcpy.CreateFeatureclass_management(path, 'mxd2.shp', "POLYGON") point_object = mxd2.shp
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.shpNameError: name 'mxd2' is not defined
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)
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
Do you have your env.workspace set to a File Geodatabase?
Can you post the entire code you are using?
#scriptPath = sys.path[0] scriptPath = r'P:\2011\Job_031_TownPlanning_SeriesProduction\Working\mxd\1'
Nicholas,Thanks for sending the code. When I try it, I get the following even though there are ver 10 MXD's in the directory. Please see the attachment (2.rar) to my last post. Following change was made to the code... #scriptPath = sys.path[0] scriptPath = r'P:\2011\Job_031_TownPlanning_SeriesProduction\Working\mxd\1' >>>Executing: mapindexStart Time: Tue May 10 11:59:56 2011Running script mapindex...Script path is P:\2011\Job_031_TownPlanning_SeriesProduction\Working\mxd\1No ArcMap Documents were found in this folder. Please try another folder.Completed script mapindex...Failed to execute (mapindex).Failed at Tue May 10 11:59:56 2011 (Elapsed Time: 0.00 seconds)>>>best,
env.workspace = path + 'extents.gdb'
env.workspace = path + '\extents.gdb'
for mxd in mxdList: mxd = mapping.MapDocument(mxd) print mxd # Printing status for error checking dataframe = mapping.ListDataFrames(mxd2, "*")[0]
for mxd in mxdList: mxd2 = mapping.MapDocument(mxd) print mxd # Printing status for error checking dataframe = mapping.ListDataFrames(mxd2, "*")[0]
import arcpy, glob, os from arcpy import env from arcpy import mapping env.overwriteOutput = True path = os.getcwd() # Script in same directory as files being processed mxdList = glob.glob(path + "\*.mxd") env.workspace = path + '\extents.gdb' # Directory as files being processed print env.workspace x = 0 y = 1 z = 1 while y < 7: for mxd in mxdList: mxd2 = mapping.MapDocument(mxd) try: dataframe = mapping.ListDataFrames(mxd2, "*") 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(z)) z = z + 1 except IndexError: pass x = x + 1 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)
polygon = arcpy.Polygon(array) #Create Polygon Shapefile arcpy.CreateFeatureclass_management(path, "Polygon_Extent" + "_" + str(z), "POLYGON") # arcpy.CopyFeatures_management(polygon, "Polygon_Extent" + "_" + str(z)) z = z + 1
for el in arcpy.mapping.ListLayoutElements(mxd2, "DATAFRAME_ELEMENT"): ExtentIDStore = str(el.name) print ExtentIDStore #used to see which item to add to the shape file print ExtentIDStore # should show the full entry at storage x but instead shows the character at x
Jake,1. while y < 7: === this loop creates a shp file with the extents of each dataframe? I guess I can use the same loop to also add in the scale and name of the dataframe into the shp file?2. for fc in lstFCs: ==== take all the "polygon_extent_*.shp" files and merge them into extents.shp?3. for item in list: ==== delete the "polygon_extent_*.shp"?
>>> Reading mxd files from P:\2011\Job_031_TownPlanning_SeriesProduction\Working\mxd\1Working in P:\2011\Job_031_TownPlanning_SeriesProduction\Working\mxd\1\temp_extentsProcessing P:\2011\Job_031_TownPlanning_SeriesProduction\Working\mxd\1\TownPlanning_Bushfire_Overlay_Ver2-Herberton.mxdCreated...Polygon_Extent_1IrvinebankIWatsonvilleWOverviewOHerbertonH
Traceback (most recent call last): File "P:\2011\Job_031_TownPlanning_SeriesProduction\Working\mxd\1\extent_creation4.py", line 78, in <module> rows = arcpy.InsertCursor(FileRef) File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\__init__.py", line 837, in InsertCursor return gp.insertCursor(*args) File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\geoprocessing\_base.py", line 367, in insertCursor self._gp.InsertCursor(*gp_fixargs(args)))RuntimeError: ERROR 999999: Error executing function.>>>
I have added to the code to capture the extent ID (it can fill in filename as 'mxd' and not sure how to capture scale) but it is giving errors in identifying the x'th entry in to the list -it prints just the x'th character. Also it's not clear where to put a print statment to give feedback that the program is working on the 2,3,4th etc mxd file.
for el in arcpy.mapping.ListLayoutElements(mxd2, "DATAFRAME_ELEMENT"): ExtentIDStore = str(el.name) list2.append(ExtentIDStore)
print list2[0]
I also notice that it loops first through the mxd's creating extent 1, then loops through creating extent 2 etc. I was initially thinking that it opened the first mxd, cycled through and created all the extent polygons and then opened the next mxd and created the extents.
Also it's not clear where to put a print statement to give feedback that the program is working on the 2,3,4th etc mxd file.
for mxd in mxdList: mxd2 = mapping.MapDocument(mxd) print mxd
Then you can use print statement for the list:print list2[0]
File "P:\2011\Job_031_TownPlanning_SeriesProduction\Working\mxd\1\Extent_Generation.py", line 81, in <module> rows = arcpy.InsertCursor(FileRef)NameError: name 'FileRef' is not defined
import sys, arcpy dataDir = "C:/Temp" arcpy.overwriteOutput = True arcpy.env.workspace = dataDir #Get extent fc = "input.shp" desc = arcpy.Describe(fc) extent = desc.Extent #Create polygon pts = [ [extent.XMin, extent.YMin], [extent.XMin, extent.YMax], [extent.XMax, extent.YMax], [extent.XMax, extent.YMin] ] point = arcpy.Point() array = arcpy.Array() for pt in pts: point.x = pt[0] point.y = pt[1] array.add(point) array.add(array.getObject(0)) SR = desc.spatialReference poly = arcpy.Polygon(array,SR) #Copy features arcpy.CopyFeatures_management(poly, "Polygon_Extent")
point.x = pt[0] point.y = pt[1]
I am very new to Python (and coding), and I cannot manage to get any of the codes on this page to work. All but one ends in errors, and the one that doesn't, posted by Jake Skinner on May 10, 2011, seems to freeze or something. I can't tell if it is actually doing anything, so I exit out.
I tried Nicholas Peihl's Map Index found here: npeihl / MapIndexTool / wiki / Home — Bitbucket . It has so much potential of being the perfect tool for what I need, but it only works on the biggest map extent in each MXD, and unfortunately, my series of maps and data frame in question is not the biggest map on the layout (think of it as an insert). I tried changing it to a for loop instead of an if else, but upon running it tells me it is formatted incorrectly, and I can't find the problem. Same problem if I change it to look for a specific data frame name. If I could get it to work, I would add another field where it includes the Data Frame name.
Can somebody help me?
Hi Heather,
Can you post the failing code you tried with the for loop? Then I could probably give you some advice.
I have attached two files I have tried to modify below (I seemed to have deleted the real for loop one, but these are still good enough examples of my attempts). It could be me and how I am attempting to run them, as I know you bundled them up into a nice ArcTool box, but when I try to replace the py file, it seems to not acknowledge my changes and runs as you originally wrote it. So, when I go into IDLE and run it with my changes, I always get the following:
Thank you so much for looking at this!
The map_index_dfname.py appears to be very close to working. But you have a reference to a pFrame variable which doesn't exist. In your for loop, you are creating the df variable. Maybe try changing pFrame to df.
I can't tell if I am making headway or not with that window complaining about the formatting. I cannot find any spaces. I'm such a noob in over my head.
The idea to call for a specific data frame came from the bottom of this page: ListDataFrames—Help | ArcGIS for Desktop
Thank you!
The second error in the popup describes the issue. Look at the screenshot above. My IDE shows lines with pink highlighting that begin with tabs. Every other line using spaces. Either replace the tabs with the correct number of spaces or vice versa. You can't have both.
Whether you use tabs or spaces is personal preference.
Hi Nicholas, thank you for being so patient with me. I feel like I'm asking such basic questions, but I am making progress!
Long story short, I have tried numerous methods, but what finally worked was deleting every indent on every line and hitting tab. Once I did it to the pink region, it was willing to work.
I then put the file in the same folder as my MXDs, and tried to run it and I got a line 30 "TypeError: object of type 'NoneType' has no len()", which I read was probably because it was waiting on an input (python - What is a 'NoneType' object? - Stack Overflow).
I renamed and replaced the file in your Scripts folder and reloaded the Map Index Toolbox into catalog, but this is a whole new realm for me, and it is obviously not pulling the py script when it runs. The results are the same as if I hadn't changed any code. I tried right clicking it in ArcToolbox and importing my py script, and that failed.
Creating the new Map Index shapefileOpening map document Z:\Projects\60132\Maps\Strip Maps\Arc\0027-2016.mxdFailed script MapIndex...Traceback (most recent call last): File "C:\Users\heather\Documents\ArcGIS\AddIns\npeihl-mapindextool-0da4e723ca4a\Map Index Toolbox.tbx#MapIndex.py", line 45, in <module> File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\utils.py", line 182, in fn_ return fn(*args, **kw) File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\mapping.py", line 1496, in ListDataFrames result = mixins.MapDocumentMixin(map_document).listDataFrames(wildcard) File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\arcobjects\mixins.py", line 745, in listDataFrames return list(reversed([fr for fr in self.dataFrames if wildcardmatch(wildcard, fr.name)])) File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\arcobjects\mixins.py", line 710, in dataFrames return map(convertArcObjectToPythonObject, self.pageLayout.dataFrames) File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\arcobjects\mixins.py", line 694, in pageLayout return convertArcObjectToPythonObject(self._mxd._arc_object.pageLayout)AttributeError: 'unicode' object has no attribute '_arc_object'Failed to execute (MapIndex).Failed at Fri Sep 16 10:03:50 2016 (Elapsed Time: 4.12 seconds)
Creating the new Map Index shapefileOpening map document Z:\Projects\60132\Maps\Strip Maps\Arc\0027-2016.mxdFailed script MapIndex...
Traceback (most recent call last): File "C:\Users\heather\Documents\ArcGIS\AddIns\npeihl-mapindextool-0da4e723ca4a\Map Index Toolbox.tbx#MapIndex.py", line 45, in <module> File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\utils.py", line 182, in fn_ return fn(*args, **kw) File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\mapping.py", line 1496, in ListDataFrames result = mixins.MapDocumentMixin(map_document).listDataFrames(wildcard) File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\arcobjects\mixins.py", line 745, in listDataFrames return list(reversed([fr for fr in self.dataFrames if wildcardmatch(wildcard, fr.name)])) File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\arcobjects\mixins.py", line 710, in dataFrames return map(convertArcObjectToPythonObject, self.pageLayout.dataFrames) File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\arcobjects\mixins.py", line 694, in pageLayout return convertArcObjectToPythonObject(self._mxd._arc_object.pageLayout)AttributeError: 'unicode' object has no attribute '_arc_object'
Failed to execute (MapIndex).Failed at Fri Sep 16 10:03:50 2016 (Elapsed Time: 4.12 seconds)
Suggestions?
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.