ArcGIS Help ListBookmarks example 3 not working

1250
9
Jump to solution
03-19-2014 07:16 AM
MatthewStarry
New Contributor III
Please refer to the ArcGIS Help document for the ListBookmarks (arcpy.mapping) function, example 3.  Example 3 shows code that is supposed to convert each bookmark in a map document to a feature.

I cannot get this code to output polygons of bookmark extents in a mxd. I set the output feature class.  I created a template feature class with a "Name" attribute (text, 50) with the same spatial reference as the dataframe of the mxd that contains the bookmarks. The code runs without error and creates the output feature class, however, it does not contain any records.

Does anyone have any ideas on how to get this code to work? I'm using ArcGIS 10.2.1.

import arcpy, os  # The map with the bookmarks mxd = arcpy.mapping.MapDocument(r"C:\Project\Counties.mxd")  # The output feature class to be created - # This feature class will store the bookmarks as features outFC = r'C:\Project\Counties.gdb\Bookmarks'  # A template feature class that contains the attribute schema # Including a "Name" field to store the bookmark name template = r'C:\Project\Counties.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):     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()
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MatthewStarry
New Contributor III
I set the output to a shapefile and it worked.

View solution in original post

0 Kudos
9 Replies
MatthewStarry
New Contributor III
I set the output to a shapefile and it worked.
0 Kudos
MichaelVolz
Esteemed Contributor
Can you show the modified code to create a shapefile from the bookmarks?
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Hi Michael,

You will just need to update the outFC variable.  Ex:

outFC = r'C:\Project\Bookmarks.shp'
0 Kudos
MatthewStarry
New Contributor III
I didn't really modify the code. I just set the output feature class to a shapefile instead of a feature class in a FGDB.

import arcpy, os

# The map with the bookmarks
mxd = arcpy.mapping.MapDocument(r"C:\Project\Counties.mxd")

# The output feature class to be created -
# This feature class will store the bookmarks as features
outFC = r'C:\Project\Counties_Bookmarks.shp'

# A template feature class that contains the attribute schema
# Including a "Name" field to store the bookmark name
template = r'C:\Project\Counties.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):
    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()
0 Kudos
MichaelVolz
Esteemed Contributor
Jake:

Any idea why the original code would not work against a file geodatabase?
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Cannot say for sure; it looks like this may be a bug.
0 Kudos
MichaelVolz
Esteemed Contributor
Jake:

Have you confirmed that this python script run with a file geodatabase, modified for your specific environment, does not work in your environment?
0 Kudos
KyleSchaper
New Contributor II
I can get this code to work when the output is a shape.  My issue is that I have my data frame rotated 3.5 degrees for a better display area.  When the bookmark polys are created, the x,y pairs don't take into account this rotation (see attachment).  How can I add this rotation factor in the python code so that my extent rectangles are a true representation of what is being displayed in the data frame?
0 Kudos
IanMurray
Frequent Contributor
Yes that can be done.

I'd take a look at this

so it would be something like

df = arcpy.mapping.ListDataFrames(mxd, "#Yourmapdocumentnamehere")
df.rotation = 3.5

Hope this helps!
0 Kudos