1

I'm trying to use ListBookmarks in Python to output my bookmarks to features. I directly copied the code in the sample provided. It "succeeds" and creates the feature class I wanted, but it is empty with no features. Did anybody have the same problem and solve it? Using ArcMap 10.4, Python 2.7.10, and Windows 8.1.

Here's the code I used, but it's essentially the same as the sample:

import arcpy, os

# The map with the bookmarks
mxd = arcpy.mapping.MapDocument(r"D:\GIS\Projects\ACE\Easement Maps\UtilityOutgrants2.mxd")

# The output feature class to be created -
# This feature class will store the bookmarks as features
outFC = r'D:\Default.gdb\Bookmarks'

# A template feature class that contains the attribute schema
# Including a "Name" field to store the bookmark name
template = r'D:\Default.gdb\Template'

if arcpy.Exists(outFC):
    arcpy.Delete_management(outFC)
arcpy.CreateFeatureclass_management(os.path.dirname(outFC),
                                    os.path.basename(outFC), 
                                    "POLYGON", template, 
                                    spatial_reference=template)

cur = arcpy.da.InsertCursor(outFC, ["SHAPE@", "Name"])
array = arcpy.Array()
for bkmk in arcpy.mapping.ListBookmarks(mxd, "Layers"):
    array.add(arcpy.Point(bkmk.extent.XMin, bkmk.extent.YMin))
    array.add(arcpy.Point(bkmk.extent.XMin, bkmk.extent.YMax))
    array.add(arcpy.Point(bkmk.extent.XMax, bkmk.extent.YMax))
    array.add(arcpy.Point(bkmk.extent.XMax, bkmk.extent.YMin))
    # To close the polygon, add the first point again
    array.add(arcpy.Point(bkmk.extent.XMin, bkmk.extent.YMin))
    cur.insertRow([arcpy.Polygon(array), bkmk.name])
    array.removeAll()

print "done"
10
  • I've never had a problem doing this but something to double-check is that your MXD has been saved with those bookmarks in it. Also, I don't think I ever use a template feature class so perhaps try running this without that as a test. – PolyGeo Mar 16 '16 at 23:30
  • Thanks, I double-checked. Bookmarks are saved and I ran it without the template. Same result. One thing to note is that schema locks seem to have retained after code was finished executing. I don't know if that is important, but it makes me want to believe that it is still trying to create the features... – Kwan Mar 16 '16 at 23:47
  • Make sure to delete the cursor at the end of your code. del cur – Fezter Mar 16 '16 at 23:52
  • 1
    Have you tried specifying the DataFrame object in the ListBookmarks() function, as specified in the function documentation? You can get this easily from the mxd object: for bkmk in arcpy.mapping.ListBookmarks(mxd, "Layers", mxd.activeDataFrame) – dmahr Mar 17 '16 at 1:15
  • 1
    @Kwan I think you need to separate your task into the two parts: 1) parsing the points from the bookmarks, and 2) using the insertCursor to output polygons. Double check that 1) is being done correctly by printing the coordinates within the for loop with arcpy.AddMessage(bkmk.extent.JSON). If the coordinates are correct, then something is wrong with 2) your use of the cursor. Perhaps you need to specify the spatial_reference within your arcpy.Polygon() constructor? – dmahr Mar 17 '16 at 20:54