Select to view content in your preferred language

Set camera scale for map frame with Pythong

120
4
Jump to solution
Friday
IlkaIllers1
Occasional Contributor III

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?

1 Solution

Accepted Solutions
IlkaIllers1
Occasional Contributor III

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 🙂

View solution in original post

0 Kudos
4 Replies
GISWilliamEdwards
New Contributor

GISWilliamEdwards_0-1718980525940.png

Try this 🙂 hopefully this works. This is a snippet from my code from a tool I have.

0 Kudos
GISWilliamEdwards
New Contributor

To clarify its the decimal placement that actually fixed a similar issue I was having.

0 Kudos
IlkaIllers1
Occasional Contributor III

Just adding the decimal point to the scale? No, unfortunately that didn't solve it.

0 Kudos
IlkaIllers1
Occasional Contributor III

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 🙂

0 Kudos