Arcpy - user selecting a bookmark

269
2
Jump to solution
06-07-2022 06:14 AM
MichaelFowler1
New Contributor II

Hi all!

I'm trying to create a script that will automatically create a map after a few user inputs. The map template I'm using will have all bookmarks predefined and the end user will just need to select the one they want from the drop down.

The below code is what i have tried so far, although the output is always 'South China Sea' as that is the last bookmark in my pre-defined list (in the toolbox).

I have looked at all the ArcGIS Pro documentation and none of it has helped so far.

Any help would be greatly appreciated!

import arcpy
workspace = arcpy.GetParameterAsText(0)
productName = arcpy.GetParameterAsText(1)
aprx = arcpy.mp.ArcGISProject('CURRENT')
lyt = aprx.listLayouts()[0]
aprxMap = aprx.listMaps('Map')[0]
mf = lyt.listElements('MAPFRAME_ELEMENT', 'Map Frame')[0]
bookmark = arcpy.GetParameterAsText(2)
bookmark = mf.map.listBookmarks()
for bkmk in bookmark: # loop through all bookmarks until find specific bookmark
    if bkmk.name == 'Australia': 
        mf.zoomToBookmark(bkmk) 
        arcpy.AddMessage('Australia bookmark set')
        # then do layers on/off
    elif bkmk.name == 'Papua New Guinea': 
        mf.zoomToBookmark(bkmk) # zoom to Australia
        arcpy.AddMessage('Papua New Guinea bookmark set')
        # then do layers on/off
    elif bkmk.name == 'Indonesia': 
        mf.zoomToBookmark(bkmk) 
        arcpy.AddMessage('Indonesia bookmark set')
        # then do layers on/off
    elif bkmk.name == 'South China Sea': 
        mf.zoomToBookmark(bkmk) 
        arcpy.AddMessage('South China Sea bookmark set')
        # then do layers on/off
    else:
        arcpy.AddWarning('Bookmark does not exist')
out_pdf = (workspace + f'\\Output\\{productName}')
lyt.exportToPDF(out_pdf)
aprx.save()

 

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
MarkBryant
New Contributor III

There are two issues.

  1. You have two variables called bookmark. The user input Line8 and list of bookmarks in the map on Line9. Your user input is overwritten by the list of bookmarks.
  2. You never compare the bkmk.name in the loop with the user input to find the actual bookmark your are looking for.
# get the user input bookmark name
in_bookmark = arcpy.GetParameterAsText(2)
bookmark = mf.map.listBookmarks()
for bkmk in bookmark: # loop through all bookmarks until find specific bookmark
    if bkmk.name == in_bookmark: # compare the bookmark name with the user input
        mf.zoomToBookmark(bkmk) 
        arcpy.AddMessage(f'{in_bookmark} bookmark set')
        # then do layers on/off
    else:
        arcpy.AddWarning('Bookmark does not exist')

Mark.

View solution in original post

2 Replies
MarkBryant
New Contributor III

There are two issues.

  1. You have two variables called bookmark. The user input Line8 and list of bookmarks in the map on Line9. Your user input is overwritten by the list of bookmarks.
  2. You never compare the bkmk.name in the loop with the user input to find the actual bookmark your are looking for.
# get the user input bookmark name
in_bookmark = arcpy.GetParameterAsText(2)
bookmark = mf.map.listBookmarks()
for bkmk in bookmark: # loop through all bookmarks until find specific bookmark
    if bkmk.name == in_bookmark: # compare the bookmark name with the user input
        mf.zoomToBookmark(bkmk) 
        arcpy.AddMessage(f'{in_bookmark} bookmark set')
        # then do layers on/off
    else:
        arcpy.AddWarning('Bookmark does not exist')

Mark.
MichaelFowler1
New Contributor II

Thank you that works perfectly!

0 Kudos