I have code that zooms a layout to a bookmark and then exports that layout to PDF. This step helps me produce maps on a large scale because I have about 40 bookmarks. However, if someone asks me to create the maps again but zoomed in or out more than that creates a problem because then I have to change all the bookmarks. I'm hoping there is some kind of code that would zoom to the bookmark and then I could tell it to zoom in or out a certain amount from there.

Here is the code I have currently:

import arcpy

p = arcpy.mp.ArcGISProject(r'C:\arcGIS_Shared\Python\CenterHeatMaps.aprx')
lyt = p.listLayouts("Layout_King")[0]
mf = lyt.listElements("MAPFRAME_ELEMENT", "Map*")[0]
bkmks = mf.map.listBookmarks()
m = p.listMaps()[0]
OnLyrList = m.listLayers('Topographic')
OnNxtList = m.listLayers('Open_GoHealth_Centers')
CenterList = m.listLayers("CASum*")

#Loop through center layers that begin with 'SumWithin'
for center in CenterList:

   #Loop through all bookmarks until finding a match for the active center
    for bkmk in bkmks:       
        if bkmk.name == center.name[19:]:

            #Turn off all layers
               lyrList = m.listLayers()
               for lyr in lyrList:
                   lyr.visible = False

            #Turn on desired layers
               for lyr in OnLyrList:
                   lyr.visible = True
               for lyr in OnNxtList:
                   lyr.visible = True
               center.visible = True

            #Zoom to active bookmark

            #Change Layout Title
               for elm in lyt.listElements("TEXT_ELEMENT"):
                   elm.text = "Annual_CA Summary by Block 2017 - " + center.name[19:]

            #Export to PDF
               lyt.exportToPDF(r"C:\arcGIS_Shared\Python\Export\CenterHeatMaps_CA" + "\\Annual_CA" + center.name[15:] + ".pdf")

Is what I'm asking possible? I haven't been able to find anything online.

  • 1
    There is a panToExtent method on your MapFrame object... if you can get the current MapFrame extent after zooming to the bookmark you could adjust the extent around the centre (expand/contract) then pan to it. – Michael Stimson Jan 17 '18 at 21:31

To zoom in or out on the current extent of a map frame you should be able to use the scale property of the map frame's Camera object.

For example, this code will zoom out 5%:

mf.camera.scale = mf.camera.scale * 1.05

or in a more concise way, as suggested by @MarceloVilla-Piñeros:

mf.camera.scale *= 1.05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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