I am zooming to all features of a feature class and then setting the map scale to 5000.
aprx= arcpy.mp.ArcGISProject(aprx)
l = aprx.listLayouts("Layout")[0]
mf = l.listElements('MAPFRAME_ELEMENT','Map Frame')[0]
#make feature layer or points
f2 = fc
#make feature layer
fl = arcpy.management.MakeFeatureLayer(f2,f2)
#loop through features
with arcpy.da.SearchCursor(fl, ['OBJECTID']) as cursor:
for row in cursor:
# Find OBJECTID of the row
oid = row[0]
# Select the row
where_clause = "{} = {}".format(arcpy.AddFieldDelimiters(fl, "OBJECTID"), oid)
arcpy.management.SelectLayerByAttribute(fl, "NEW_SELECTION", where_clause)
#zoom to selection
mf.zoomToAllLayers(True)
#go back to correct scale
mf.camera.scale = 5000
However, the scale is not set to 5000. Adding a print(mf.camera.scale) shows how most of the time, the scale is at 2812,4999999999 (from when I zoomed to the feature), and only a handful of times it is 4999,999999999.
The map frame properties display options are set to none. I would really like to understand why it doesn't set the scale, especially why it does so for a few features but not all?
Solved! Go to Solution.
I solved the problem by repeating the line, so it says twice mf.camera.scale = 5000. This seems like it might be a bug, I shouldn't have to repeat this, right? But for now it works 🙂
Try this 🙂 hopefully this works. This is a snippet from my code from a tool I have.
To clarify its the decimal placement that actually fixed a similar issue I was having.
Just adding the decimal point to the scale? No, unfortunately that didn't solve it.
I solved the problem by repeating the line, so it says twice mf.camera.scale = 5000. This seems like it might be a bug, I shouldn't have to repeat this, right? But for now it works 🙂