From my limited understanding of your code.
You are currently setting up a loop through the layers in your Map and checking if there are any Definition Queries associated with the layers. You then get the layer extents.
The loop runs through all the layers and gets the extents for everything, and then exits the loop. You then try set up the mapframe using the max and min x and y coordinates and then export it...
So it looks like the script is doing what you told it to do.
To fix this, perhaps try adding the last lines of code to within the loop.
layout = proj.listLayouts("FirstPrintLayout")[0]
print (f'2 - Which contains a layout named: {layout.name}')
mapframe = layout.listElements("MAPFRAME_ELEMENT", "Map Frame 1")[0]
print (f'3 - And the layout has a map frame where everything starts...{mapframe.name}')
print (f'4 - The map inside the mapframe is {mapframe.map.name}')
for lyr in mapframe.map.listLayers("MyLayer"):
if lyr.supports("DEFINITIONQUERY"):
print(lyr.name, ' has defQuery ', lyr.definitionQuery)
print ('Excellent! So now lets get the extents')
mapExtent = mapframe.getLayerExtent(lyr)
print (f'5 - mapFrame XMin extents: {mapExtent.XMin}')
print (f'Ymin {mapExtent.YMin}')
print (f'XMax {mapExtent.XMax}')
print (f'YMax {mapExtent.YMax}')
mapframe.zoomToAllLayers (selection_only=True)
print ('Should be at the correct level of zoom')
mapframe.exportToPNG(out_png='out_png', resolution=60)
exit()
You will need to dynamically change the outfile too.
Something like:
out_png = "out_png" + lyr.name + ".png"
Might work.
EDIT
Here is an extract from a script that cycles through a series of polygons and extracts a PDF / JPEG.
Modding it to your code:
layout = proj.listLayouts("FirstPrintLayout")[0]
print (f'2 - Which contains a layout named: {layout.name}')
mapframe = layout.listElements("MAPFRAME_ELEMENT", "Map Frame 1")[0]
print (f'3 - And the layout has a map frame where everything starts...{mapframe.name}')
print (f'4 - The map inside the mapframe is {mapframe.map.name}')
for lyr in mapframe.map.listLayers("MyLayer"):
if lyr.supports("DEFINITIONQUERY"):
print(lyr.name, ' has defQuery ', lyr.definitionQuery)
print ('Excellent! So now lets get the extents')
mapExtent = mapframe.getLayerExtent(lyr)
print (f'5 - mapFrame XMin extents: {mapExtent.XMin}')
print (f'Ymin {mapExtent.YMin}')
print (f'XMax {mapExtent.XMax}')
print (f'YMax {mapExtent.YMax}')
fid = 0
sql = """"Title" >= '%s'""" % fid
arcpy.SelectLayerByAttribute_management(in_layer_or_view=lyr,
selection_type='NEW_SELECTION',
where_clause=sql)
mapframe.zoomToSelectedFeatures()
arcpy.RefreshActiveView()
out_png = r'Path_to_location' + lyr.name + ('.png')
print ('Should be at the correct level of zoom')
# proj below is the aprx file
mapframe.exportToPNG(proj, out_png=out_png', resolution=60)
exit()
I haven't tested it, its using some of my old code for the ExportToPDF
function in arcpy.
This should select each polygon where the FID >= 0, zoom to the selection and export. Handling the panning and scaling as it goes. It also creates an "out_png" file based on the layer name.
Hope that at least helps a little.
EDIT 2 - FOR COMPLETENESS
As Mr. Hornby pointed out. There could be a potential bug in ArcGIS Pro when setting the export extent.
I tried a number of interations of the same processes of setting the view extent for the export (I tried PDF, PNG and JPG, all with the same result).
Here is my final code:
import arcpy, os
from arcpy import env
from functools import reduce
env.overwriteOutput = True
aprx = arcpy.mp.ArcGISProject(r"C:\temp\test1.aprx")
m = aprx.listMaps("Map")[0]
lyr = m.listLayers("Depot_Area_Coverage")[0] ## Name of your layer
lyt = aprx.listLayouts("FirstPrintLayout")[0] ## Name of your layout
fc = r"c:\temp\Depot_Area_Coverage.shp"
#mf = lyt.listElements("MAPFRAME_ELEMENT", "Map Frame")[0] ## Name of the map frame
count = int(arcpy.management.GetCount(lyr)[0])
print (count)
for page in range (0, count):
print (page)
sql = """"FID" = %s""" % page
#with arcpy.da.SearchCursor(fc,"SHAPE@",sql) as cur:
# fullextent = reduce(arcpy.Geometry.union, (shp for shp, in cur)).extent
# print (fullextent)
#print (fullextent.XMin)
#print (fullextent.YMin)
#mf.camera.setExtent(fullextent)
#print (mf.camera.scale)
#mf.camera.scale = mf.camera.scale*0.5
#mf.camera.setExtent(mf.getLayerExtent(lyr))
#print (mf.camera.scale)
#mapextent = mf.getLayerExtent(lyr)
#print (mapextent)
#lyr.definitionQuery = sql
print (arcpy.GetCount_management(lyr))
lyr = arcpy.SelectLayerByAttribute_management(lyr,'NEW_SELECTION',sql)[0]
print (arcpy.GetCount_management(lyr))
mf = lyt.listElements("mapframe_element")[0]
# Choose the output Height and Width
mf.elementHeight = 10
mf.elementWidth = 10
# Set the extent
mf.camera.setExtent(mf.getLayerExtent(lyr, True, False))
mf.panToExtent(mf.getLayerExtent(lyr, True, False))
# Clear the selection
arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION")
#mf.camera.setExtent(fullextent)
#print (mf.camera.scale)
#print (mf.camera.Extent)
out = os.path.join(r"C:\temp",str(page) + "_.jpg")
#png = mapframe.exportToPNG(out, resolution = 10)
jpg = mf.exportToJPEG(out, resolution =10, jpeg_quality = 10)
print (out)
sql = ""
lyr.definitionQuery = sql
This cycles through the polygons in the input layer. It then removes the polygons not needed for the export using a definition query, selects the polygon and changes the map view based on the selected polygon.
Printing out the various extents along the way shows that the extents do change, and the map scale changes with it. The export however is still at the full extent of the input layer, it just pans around as the polygons change.
It is also very slow at times.
I have kept a few lines of code in there to show some of the experiments I tried, I have commented these out.
Ultimately, I think creating a map series and running the export that way is the only real way to do this in ArcGIS Pro right now. I found a few articles that explained why the functionality from ArcMap was removed in ArcGIS Pro. Interestingly, there are suggestions that some sort of work-arounds being developed for release in ArcGIS Pro 3 (expected around the time of the conference in July 2022).