All,
I came across this Q&A on GIS Stack exchange, the poster wanted to export the MapFrame in a layout to a PNG which was zoomed to a feature defined by a definition query, but it creates a much large extent of output when exported .
Someone answered, but their code does not really work. I adapted their code to adjust the extent of the MapView and camera objects, but like the original poster I too end up exporting a much larger PNG.
So my question is, is this by design or a bug in ArcPro 2.9.2 arcpy? If the MapFrame is showing a zoomed in section of the data I would have expected the export process to export only that?
The code is this:
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("River Centreline 2006"):
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.camera.setExtent(mapExtent)
theMapView = myPrj.activeView # This line expects the Map to be Active Not Layout.
theMapView.panToExtent(mapExtent)
# Export Map Frame
mapframe.exportToPNG(out_png='c:\Temp\outimg.png', resolution=60)
print('Export completed!')
Note: my sample data is in British National Grid