2

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?

  • When I was using arcpy to export geotifs I noticed a warping of the results which was fixed by adding a line setting ar (ar = df.extent.height / df.extent.width) and then using it in the export like this: arcpy.mapping.ExportToTIFF(mxd,r"C:\Project\cruise2013_" + str(ddp.pageRow.Name) + ".tif",df,9306,9306*ar,300,True) – johns Feb 19 '14 at 18:25
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.