Layout Element Dimensions in Arcpy

637
4
Jump to solution
09-05-2023 12:24 PM
Labels (1)
JohnButcher
New Contributor II

I have a question regarding layout element dimensions (in this case a MAP_FRAME element) and how decimal places are represented when extracting this information using arcpy.

I'm using Pro 2.9.5

Here is a screenshot showing the dimensions of my map frame:

JohnButcher_0-1693940643053.png

Here's a simplified snippet of code:

import arcpy

prj = arcpy.mp.ArcGISProject("CURRENT")
lyt = mxd.listLayouts()[0]

frame = layout.listElements("MAPFRAME_ELEMENT","Map Frame")[0]
print(frame.elementWidth)
print(frame.elementHeight)
print(frame.elementPositionX)
print(frame.elementPositionY)
3.31
2.37
0.25
7.5

As you can see the results are truncating at 2 decimal places. This is tripping me up as I try to place layout elements with precision.

I've tried a few things like "round()" and importing the decimal module but haven't had any success.

Any sense of what's happening here and how I might be able to return values that reflect the precise dimensions of the element?

Appreciate the help.

0 Kudos
1 Solution

Accepted Solutions
JohnButcher
New Contributor II

I logged a defect with ESRI and received the following response:



 I have spoken with some colleagues and it appears that we cannot increase the significant figures used in the output when using ArcPy. I have logged an enhancement to allow for this so that we can get consistent results whether we are working with Python or in the UI. Below is some more information on the defect. 

Enhancement Number: ENH-000161348

Synopsis: Allow for the ability to increase the precision of the element height, width, and XY position when using ArcPy.

View solution in original post

0 Kudos
4 Replies
DanPatterson
MVP Esteemed Contributor

See if it is just your default format settings by being specific about your format

frame_elementWidth = 3.3125  # a test variable

print("{:6.6f}".format(frame_elementWidth))
3.312500

print("{:6.4f}".format(frame_elementWidth))
3.3125

print("{:6.2f}".format(frame_elementWidth))
  3.31

... sort of retired...
0 Kudos
JohnButcher
New Contributor II

Hi Dan, thanks a lot for the reply.

Using your recommended format options on both the frame element and a test variable the following values are returned.

frame = layout.listElements("MAPFRAME_ELEMENT","Map Frame")[0]
print("{:6.6f}".format(frame.elementWidth))
3.310000
print("{:6.4f}".format(frame.elementWidth))
3.3100
print("{:6.2f}".format(frame.elementWidth))
3.31

frame_elementWidth = 3.3125 # test variable
print("{:6.6f}".format(frame_elementWidth))
3.312500
print("{:6.4f}".format(frame_elementWidth))
3.3125
print("{:6.2f}".format(frame_elementWidth))
3.31

You mentioned default format settings. Is that something I can adjust?

0 Kudos
DanPatterson
MVP Esteemed Contributor

It is hard to say whether it is your IDE or that is the way it outputs the result.  If it is the latter, you should forward this as an issue with tech support, since the help states that a double is returned,.... not a truncated double

As for in general...

I know you can in scripts using numpy, but in plain python, you have to be overt in your decisions... see

7. Input and Output — Python 3.11.5 documentation

for other options.

If you use numpy therre is a setprintoptions method. which enables you to handle boolean, float, list and array formatting.

 

ft = {"bool": lambda x: repr(x.astype(np.int32)),
      "float_kind": '{: 6.2f}'.format}
np.set_printoptions(
    edgeitems=10, linewidth=120, precision=3, suppress=True, threshold=200,
    formatter=ft
)

 

Check your python IDE... there may be a setting option there


... sort of retired...
0 Kudos
JohnButcher
New Contributor II

I logged a defect with ESRI and received the following response:



 I have spoken with some colleagues and it appears that we cannot increase the significant figures used in the output when using ArcPy. I have logged an enhancement to allow for this so that we can get consistent results whether we are working with Python or in the UI. Below is some more information on the defect. 

Enhancement Number: ENH-000161348

Synopsis: Allow for the ability to increase the precision of the element height, width, and XY position when using ArcPy.

0 Kudos