Select to view content in your preferred language

Adjusting Units of a scale bar with arcpy

742
1
Jump to solution
11-20-2023 12:56 PM
StevenTouzel
Occasional Contributor

I'm using ArcGIS Pro 3.2 and I'm trying to use arcpy.mp and the layout CIM to update a scale bar's units. 

This is what I have so far, but the units somehow get erased from the layout cim json instead of updated:

 

 

 

import arcpy
aprx = arcpy.mp.ArcGISProject("CURRENT")
vicinityLayout = aprx.listLayouts("Fig1_Vicinity")[0]
# Update scale bar units to miles
scaleBarName = 'Scale Bar'
vicinityLayoutCIM = vicinityLayout.getDefinition("V3") # Get the layout CIM
## Find the Scale Bar - updating the units for the
jsonText = "{\"uwkid\":9093}" 
for elm in vicinityLayoutCIM.elements:  
	if elm.name == "Scale Bar":
		elm.unitLabel = "Miles"
		elm.numberFormat.roundingValue = 0
		elm.units = jsonText
vicinityLayout.setDefinition(vicinityLayoutCIM)

 

 

 

0 Kudos
1 Solution

Accepted Solutions
StevenTouzel
Occasional Contributor

Found the solution. I needed to convert the unit string to json. Not just make it look like json. This worked!

 

 

import arcpy, json
aprx = arcpy.mp.ArcGISProject("CURRENT")
vicinityLayout = aprx.listLayouts("Fig1_Vicinity")[0]
# Update scale bar units to miles
scaleBarName = 'Scale Bar'
vicinityLayoutCIM = vicinityLayout.getDefinition("V3") # Get the layout CIM
## Find the Scale Bar
jsonText = "{\"uwkid\":9093}" # need to convert this to json to make the unit update
scalebarUnit = json.loads(jsonText)
for elm in vicinityLayoutCIM.elements:  
	if elm.name == "Scale Bar":
		elm.unitLabel = "Miles"
		elm.numberFormat.roundingValue = 0
		elm.units = scalebarUnit
vicinityLayout.setDefinition(vicinityLayoutCIM)

 

View solution in original post

1 Reply
StevenTouzel
Occasional Contributor

Found the solution. I needed to convert the unit string to json. Not just make it look like json. This worked!

 

 

import arcpy, json
aprx = arcpy.mp.ArcGISProject("CURRENT")
vicinityLayout = aprx.listLayouts("Fig1_Vicinity")[0]
# Update scale bar units to miles
scaleBarName = 'Scale Bar'
vicinityLayoutCIM = vicinityLayout.getDefinition("V3") # Get the layout CIM
## Find the Scale Bar
jsonText = "{\"uwkid\":9093}" # need to convert this to json to make the unit update
scalebarUnit = json.loads(jsonText)
for elm in vicinityLayoutCIM.elements:  
	if elm.name == "Scale Bar":
		elm.unitLabel = "Miles"
		elm.numberFormat.roundingValue = 0
		elm.units = scalebarUnit
vicinityLayout.setDefinition(vicinityLayoutCIM)