Tuesday 15 January 2019

Zooming In and Out using ArcPy with ArcGIS Pro?


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
mf.zoomToBookmark(bkmk)

#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")

print(center.name[15:])

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



Answer



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

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...