2

I'm using this script tool, in ModelBuilder for ArcGIS Pro, to set up a map layout, reference a bookmark, and export the layout as a PDF. This model iterates through Urgent Care centers and its corresponding data one at a time (in alphabetical order by center name). Since my model loops through each Urgent Care center (via the Iterate Field Values), it repeats the same bookmark through every loop/iteration. How do I tell it to go from one bookmark to the other?? (I have about 90 centers, so that means 90 bookmarks also)

Here is my script tool:

import arcpy

#input layer
lyr = arcpy.GetParameterAsText(0)

# input name of layout
p = arcpy.mp.ArcGISProject("CURRENT")
lyt = p.listLayouts("Layout_King")[0]

# Reposition the scale bar
scaleBar = lyt.listElements("MAPSURROUND_ELEMENT", "Scale Bar")[0]
mf = scaleBar.mapFrame
scaleBar.elementPositionX = mf.elementPositionX + 0.0
scaleBar.elementPositionY = mf.elementPositionY - 0.5

# Reposition the north arrow
northArrow = lyt.listElements("MAPSURROUND_ELEMENT", "North Arrow")[0]
mf = northArrow.mapFrame
northArrow.elementPositionX = mf.elementPositionX + 8.8
northArrow.elementPositionY = mf.elementPositionY + 0.7

# Align the title with the center of the map frame
title = lyt.listElements("TEXT_ELEMENT","Name of Map Text")[0]
mf = lyt.listElements('MAPFRAME_ELEMENT',"Map Frame")[0]
title.elementPositionX = mf.elementPositionX + (mf.elementWidth / 3.7)
title.elementPositionY = mf.elementPositionY + (mf.elementHeight / 0.98)

# Reposition the Legend and fix legend title
legend = lyt.listElements("LEGEND_ELEMENT", "Legend")[0]
legend.title = "Legend"
legend.elementPositionX = mf.elementPositionX + 7.7
legend.elementPositionY = mf.elementPositionY + 7.15

# setting layout to bookmark
aprx = arcpy.mp.ArcGISProject("Current")

# add name of layout
lyt = aprx.listLayouts("Layout_King")[0]
mf = lyt.listElements("MAPFRAME_ELEMENT")[0]

# add name of bookmark
bkmks = mf.map.listBookmarks()
bkmks.sort(key=lambda x: x.name, reverse=True)
for bkmk in bkmks:
  mf.zoomToBookmark(bkmk)
lyt.exportToPDF(r"C:\arcGIS_Shared\Exports" + "\\" + bkmk.name + ".pdf")

I have heard that this could be accomplished via a for-loop, but don't know how to write one.

2
  • 1
    well, you already are using a for loop: for bkmk in bkmks:. Doesn't it work to just indent your final line so that you are exporting to pdf on each iteration of the loop?
    – mr.adam
    Commented Nov 21, 2017 at 19:44
  • 2
    Data driven pages do exactly what you are after. Consider converting bookmarks to polygons and use them . I am using script by @PolyGeo for this purpose, cannot find post though.
    – FelixIP
    Commented Nov 21, 2017 at 19:56

1 Answer 1

5

you need to add the export function inside the for loop, indent the line

for bkmk in bkmks:
    mf.zoomToBookmark(bkmk)
    lyt.exportToPDF(r"C:\arcGIS_Shared\Exports" + "\\" + bkmk.name + ".pdf")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.