Select to view content in your preferred language

MapFrame and using exportToPNG(), is this a bug?

2918
12
Jump to solution
04-22-2022 07:00 AM
Labels (1)
DuncanHornby
MVP Notable Contributor

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

0 Kudos
12 Replies
jlgprime
Emerging Contributor

I'm also experiencing this issue; has it been fixed?  I'm using Arc Pro v3.3.2 and have this issue whether I run my script directly in Pro or standalone using VS Code (although it seems to take a good bit longer to export an image when running stand-alone).  The extent of the image defined in my script is much less than what is exported to png.  The X values of the layout extent update just fine and export correctly but the Y values keep getting overridden to the larger/random extent.  It doesn't seem to matter what map frame units I've selected or whether or not the 'preserve aspect ratio' is selected.. 

I'd like to not have to update the layout frame manually each time I export out an image. 

Thanks in advance

0 Kudos
JeffBarrette
Esri Regular Contributor

@jlgprime I can NOT reproduce this issue on released versions of the software.  Could you please provide a coordinate system and perhaps a couple of screen shots so I can try to reproduce your issue.

Jeff - arcpy.mp team

Preview
 
 
 
0 Kudos
jlgprime
Emerging Contributor

@JeffBarrettethe coordinate system I'm using is WGS 1984 Web Mercator (auxiliary sphere), WKID 3857, Linear Unit Meters (1.0).  FYI I notice that the extent that I specify in my code gets updated only in the Y dimension to match the aspect ratio of the page size set in the project layout, which in my case is 8.5 x 11.  Is there a setting to disable this for the purpose of running my script?

I should note that I was previously able to get the desired result using the similar Map View method, but ran into issues with not being able to adjust map rotation within my script (for other images), which I think I can accomplish with the Map Frame method.  Attached is the map view result (which I'm trying to obtain with my current code), a screenshot of the map frame in my project, and the result I'm currently getting with the Map Frame method.  Thanks again-

 

0 Kudos