1

I am trying to automate the export of a map to png over a given domain (SW and NE coordinates) in ArcPy (ArcMap version 10.7.1). I've searched a bit but fail at setting the correct automatized scale (depending on the image size i give the scale can be way too high or way too small). I am clearly missing something in the process.

Does someone have any suggestion or hint ?

Currently, my script is the following :

import arcpy
mxd = arcpy.mapping.MapDocument(input_path)
df = arcpy.mapping.ListDataFrames(mxd)[0]

#Domain X and Y coordinates (domain limits given as floats in meters)
XW = 
XE = 
YS = 
YN = 

#output image size in pixels
x_pix = 4000.0
y_pix = 3000.0
#output image size in cm
x_cm = x_pix/37.7952755906
y_cm = y_pix/37.7952755906

extent = arcpy.Extent(XW,YS,XE,YN)
df.extent = newExtent
df.panToExtent(extent)
df.zoomToSelectedFeatures()

#compute appropriate scale
#convert domain width and height to cm (factor 100) and divide by image size in cm
x_scale = 100.0*(XE-XW)/x_cm
y_scale = 100.0*(YN-YS)/y_cm
#select scale to ensure that the desired domain is within the image
df.scale = max(x_scale,y_scale)

#export
arcpy.RefreshActiveView()
arcpy.mapping.ExportToPNG(mxd,output_path,df,x_pix,y_pix,world_file=True)

Here are two examples for the same domain :

1- desired domain to export

2- result for 3000 by 2250 pixels

3- result for 8000 by 6000 pixels

enter image description here

enter image description here

enter image description here

4
  • 1
    It might be helpful to explain a bit more about your process. What are x_cm and y_cm? Why are you multiplying the x_scale by 100? What version of arcmap are you using? Also, an example image showing what is wrong and illustrating the desired output would help.
    – nrp1000
    Mar 2, 2023 at 14:31
  • Thanks for your feedback. I added more details as well as examples hoping it makes my problem more comprehensible.
    – thom_hlh
    Mar 2, 2023 at 16:10
  • I think you should be multiplying instead of dividing to calculate x_cm and y_cm
    – nrp1000
    Mar 2, 2023 at 16:49
  • What i calculate here is the dimensions of the output image so multiplying by 37 would mean that one pixel has a size of 37 cm. From what i found one pixel is about 0.26 mm so 1/37 seems correct.
    – thom_hlh
    Mar 3, 2023 at 9:03

1 Answer 1

0

You are able to set the extent of the data frame explicitly using:

df = arcpy.mapping.ListDataFrames(mxd)[0]
newExtent = df.extent
newExtent.XMin, newExtent.YMin = XW, YS
newExtent.XMax, newExtent.YMax = XE, YN
df.extent = newExtent

Details can be found here: https://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-mapping/dataframe-class.htm

Approaching it this way should mean that you don’t have to worry about setting the scale. Does that accomplish what you are looking for?

2
  • The two setting methods seem to be equivalent as i get exactly the same coordinates when reading df.extent. However, I think you're right when telling me i don't have to worry about the scale. I will try to find a new way to get the exact extent (currently there is a margin added to the frame).
    – thom_hlh
    Mar 3, 2023 at 9:52
  • I haven't been able to confirm that this works but per the answer here, try running arcpy.RefreshActiveView() and arcpy.RefreshTOC() right before setting df.extent = newExtent, gis.stackexchange.com/questions/86486/…
    – nrp1000
    Jun 28, 2023 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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