Select to view content in your preferred language

Add bookmark(.dat) file to MXDs using arcpy?

4034
9
12-23-2015 07:37 AM
Yaron_YosefCohen
Frequent Contributor

Does anyone know any python script to import a bookmark (saved as a .dat) that was created and saved in folder by using arcpy?  I tried with this script but without a result- just the name of the projects printed:

import arcpy
from arcpy import env

env.workspace = r"D:\desktop\Project"
for mxdname in arcpy.ListFiles("*.mxd"):
    print mxdname
    mxd = arcpy.mapping.MapDocument(r"D:\desktop\Project\\" + mxdname)
    df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
    for bkmk in arcpy.mapping.ListBookmarks(mxd, 'Bookmark1', df):
        print(str(bkmk.extent))
        df.extent = bkmk.extent
    mxd.save()
del mxd

For clarity, i asked it in Importing external bookmark (.dat) files into many MXDs using arcpy? - Geographic Information System...

Thanks

0 Kudos
9 Replies
DanPatterson_Retired
MVP Emeritus

don't go cheap on the print statements...why not add print(bkmk) to see if there are any bookmarks and ... print(str(bkmk.extent)) to see if there is any extent. etc etc

0 Kudos
Yaron_YosefCohen
Frequent Contributor

i did that - noop, it still just printed the mxdname  😞

0 Kudos
DanPatterson_Retired
MVP Emeritus

which means it didn't even get into the bookmark ListBookmarks loop or maybe it didn't even find an appropriate dataframe.... more print statements

0 Kudos
Yaron_YosefCohen
Frequent Contributor

i will try and let you know- thanks Dan!!

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

YYC.  Interesting idea.

Although I didn't get to the capture part, I did make a few changes so at least you can print it out. Should be easier to work from this point on

import arcpy  
from arcpy import env  
import os
import csv

#ProjDir = r"C:\_beartest\ArcticWork"
ProjDir = r"D:\desktop\Project"
arcpy.env.workspace = ProjDir
arcpy.env.overwriteOutput = True
outFileCSV = os.path.join(ProjDir, "bkmrkOut") + ".csv"
csvFile = open(outFileCSV, "w") 
csvFile.write("mxd, bmName, minX, minY, maxX, maxY \n")

for mxdname in arcpy.ListFiles("*.mxd"):  
    print mxdname  
    mxd = arcpy.mapping.MapDocument(arcpy.os.path.join(ProjDir, mxdname))
    df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]  
    bookmarks = arcpy.mapping.ListBookmarks(mxd)
    for bmName,bmExtent in bookmarks:  
        print("name): {0}  extent: {1}".format(bmName, bmExtent))
        minmax = str(bmExtent).split(" ")
        print("xmin: {0}, ymin: {1}, xmax: {2}, ymax: {3}".format(minmax[0], minmax[1], minmax[2], minmax[3]))
        csvFile.write("{0}, {1}, {2}, {3}, {4}, {5} \n".format(mxdname, bmName, minmax[0], minmax[1], minmax[2], minmax[3]))
        #df.extent = bkmk.extent  
    #mxd.save() 
#del mxd

csvFile.close()
print("outFile is closed:  " + str(csvFile.closed))

EDITED: updated my code to write all the bookmarks (mxd name, bookmark name, xmin, ymin, xmax, ymax) to a .csv file.   So, still not what you are trying to get at, but might be a helpful utility if trying to standardize bookmarks or read in to another mxd.  Something I'll use.

Yaron_YosefCohen
Frequent Contributor

Rebecca, i run your code and get:

>>>
project.mxd
name): Bookmark 1 [2]  extent: 157281.902972993 610080.837486482 158430.021501965 610788.233211336 NaN NaN NaN NaN
xmin: 157281.902972993, ymin: 610080.837486482, xmax: 158430.021501965, ymax: 610788.233211336
outFile is closed:  True
>>>

but the data frame didn't zoomed into the bookmark.

0 Kudos
DanPatterson_Retired
MVP Emeritus

that is because it just "prints" the extent.  If you examine the code there is no zoomtoextent function.  Read code carefully

RebeccaStrauch__GISP
MVP Emeritus

Correct. I'm just inventorying the bookmarks in the mxds. I have not tried to add bookmarks or to zoombut I can see an inventory of bookmarks in various map docs as helpful if you want to standardize and/or read them into another mxd. But I haven't reversed the process, and if you read in multiple extents, you would need to know which one you are wanting it to zoom to.

What is it you are trying to accomplish? Are you trying make sure the mxd is zoomed to a certain area each time you open it, and to have this preset (i.e. Set and saved in Python before you open the mxd?

if you are just want an easy way to zoon to a certain extent with a single click, try setting the extent that the home button zooms too.

Setting a custom full extent for your data frame—Help | ArcGIS for Desktop

C_EHoward
Frequent Contributor

I am searching for a way to do something very similar- grab extents of bookmarks created somewhere (initially thought in AGOnline, but I can use MXD too) and then have those extents in a file somewhere so I can use them to append to my webmap URL to use the same web map in multiple iframes. Your code looks like it will give me what I need but I get this error:

Runtime error

Traceback (most recent call last):

  File "<string>", line 17, in <module>

IndexError: list index out of range

I copied your code from here and just added the references to my folder location. I rarely use Python so I am not sure what I am doing wrong. My CSV file does have the headers for

mxd bmName minX minY maxX maxY

but no bookmakr data itself

any ideas? thanks

0 Kudos