Select to view content in your preferred language

Set a map view scale

88
7
8 hours ago
Sfortunatamente
Regular Contributor

Hi, it should be pretty simple, I know but I'm stuck. I have one layer, I grab the extent of it and set the defaultCamera extent based on that value. This is my "pan to layer". Now I need to zoom in to a, let's say, 1:1000 scale. How can I do that? 

0 Kudos
7 Replies
Marshal
Frequent Contributor

Set the scale property of camera.

https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/camera-class.htm

aprx = arcpy.mp.ArcGISProject('CURRENT')
layout = aprx.listLayouts()[0]
map_frame = layout.listElements('MAPFRAME_ELEMENT')[0]
map_frame.camera.scale = 1000

 

0 Kudos
Sfortunatamente
Regular Contributor

Thanks. I still do not get it. I do not have any layouts, just a simple map frame. I get an empty list:

lyt = aprx.listLayouts()

 

0 Kudos
Marshal
Frequent Contributor

If you are not using a map frame in a layout then you must use aprx.activeView > MapView.camera.scale.  But it is only intended for use while ArcGIS Pro is open (e.g. script tools, python window, etc.).

https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/mapview-class.htm

That said, why would you need to set the scale on a map if you aren't exporting it?  If you are exporting it, why wouldn't you use a layout?

0 Kudos
Sfortunatamente
Regular Contributor

I need to set up the extent and scale for publishing the map so anytime I add the map service to a web map the content is already in place. Is there a better approach for that? 

I will execute the py script from the command line later on without opening ArcGIS Pro

0 Kudos
Marshal
Frequent Contributor

You are publishing the layers in the map as a map service?  The scale and extent of the map used for publishing has no impact on the resulting map service.

Sfortunatamente
Regular Contributor

You're right. Thanks!

TonyAlmeida
MVP Regular Contributor

Set the extent to the layer
mf.camera.setExtent(mf.getLayerExtent(lyr, True, True))

aprx = arcpy.mp.ArcGISProject("CURRENT")

# Get map and layer
m = aprx.listMaps("Layers1")[0]
lyr = m.listLayers("CCSO.CCSO.CCAP")[0]

# Get layout elements
lyt = aprx.listLayouts("Layout1")[0]
mf = lyt.listElements("MAPFRAME_ELEMENT", "Map Frame")[0]

# First set the extent to the layer
mf.camera.setExtent(mf.getLayerExtent(lyr, True, True))

# Then set the target scale (1:1000 in your case)
target_scale = 1000  # Changed to 1000 as per your question
mf.camera.scale = target_scale

# Update the map's default camera if needed
m.defaultCamera = mf.camera

 

0 Kudos