[GIS] arcpy export – unable to set exact extent of dataframe

arcpyexport

I am trying to use arcpy's ExportToPNG function to produce tiles for Nasa WorldWind, which will require very specific extents of outputted tiles. Pretty simple code – abbreviated below:

 mxd = arcpy.mapping.MapDocument(mxdPath)
 df = arcpy.mapping.ListDataFrames(mxd)[0]
 xmin, ymin, xmax, ymax = getWorldWindTileBounds(tileX, tileY, level)

 newExtent = df.extent
 newExtent.XMin, newExtent.YMin = xmin,ymin
 newExtent.XMax, newExtent.YMax = xmax,ymax
 df.extent = newExtent

 tileFilePath = '%s/%s/%s/%s_%s.%s' % (cacheDirectory, level, tileY, tileY, tileX, tileFormat)
 tileDir = os.path.split(tileFilePath)[0]
 if not os.path.exists(tileDir):
      os.makedirs(tileDir)

 if 'png' in tileFormat:
      arcpy.mapping.ExportToPNG(mxd, tileFilePath, df, df_export_width=512, df_export_height=512)
 else:
      arcpy.mapping.ExportToJPEG(mxd, tileFilePath, df, df_export_width=512, df_export_height=512)

The extent of outputted tiles is very close, but not identical to the extent set in this block of code. In reviewing the documentation for the arcpy DataFrame extent property, I found this: "If the aspect ratio of the extent does not match the shape of the data frame, the final extent will be adjusted to fit the new extent within the shape of the data frame. In other words, if you set explicit X, Y coordinates, you may not get the same values returned if you attempt to read them later."

Since I am using the dataframe instead of a page layout, I initially didnt think was applicable to my use case. But, I have tried all types of shenanigans to set dataframe elementWidth/elementHeight properties to no avail.

Does anyone know how I can force the export to match the extent I set exactly?

Best Answer

This may narrow your issue search and it does not do any harm. Include:

arcpy.RefreshActiveView()
arcpy.RefreshTOC()

just before

df.extent = newExtent

and see if it works

Related Question