@JeffBarrette ,
Here is a map package with a totally different dataset. I have applied a def query on one polygon (PID = 26), then run the code below:
import arcpy
arcpy.env.overwriteOutput = True
myPrj = arcpy.mp.ArcGISProject('current')
myLayout = myPrj.listLayouts("FirstPrintLayout")[0]
mapframe = myLayout.listElements("MAPFRAME_ELEMENT", "Map Frame 1")[0]
for lyr in mapframe.map.listLayers("fcTestData"):
if lyr.supports("DEFINITIONQUERY"):
print(lyr.name, ' has defQuery ', lyr.definitionQuery)
print ('Excellent! So now lets get the extents')
# Select zoom to then clear selection on the polygon defined in definition query
sql = "PID = 26"
arcpy.SelectLayerByAttribute_management(lyr,'NEW_SELECTION',sql)
mapExtent = mapframe.getLayerExtent(lyr,True)
arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION")
print (f'5 - mapFrame XMin extents: {mapExtent.XMin}')
print (f'Ymin {mapExtent.YMin}')
print (f'XMax {mapExtent.XMax}')
print (f'YMax {mapExtent.YMax}')
mapframe.camera.setExtent(mapExtent)
# Export Map Frame
mapframe.exportToPNG(out_png='c:\Temp\outimg.png', resolution=60)
print('Export completed!')
I have modified the code slightly to zoom into the selected feature
sql = "PID = 26"
arcpy.SelectLayerByAttribute_management(lyr,'NEW_SELECTION',sql)
mapExtent = mapframe.getLayerExtent(lyr,True)
arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION")
So I open the project, open Python console and paste the above code in and run it. The output is an png zoomed to the extent of the layer, not the zoomed in selected feature, BUT the map fame has zoomed in, you can see this in the layout. It seems to me that export process is not honouring the extent the map frame is displaying but that of the layer?