Getting and Setting map scale and scale bar

3042
1
08-10-2015 12:05 PM
WesMiller
Regular Contributor III

I put this code together to get and set the map to a known scale. I was wanting to know has anyone else done something like this? Below is the code.

def returnScale(dfscale):
    scalebar = [2,3,4,5,6,10]
    dfscale = dfscale/12
    dfscale = str(int(dfscale))
    dfscaleLen = len(dfscale)
    numcheck = int(dfscale[0])
    for each in scalebar:
        if numcheck < each:
            multi = '1'
            while dfscaleLen > 1:
                multi = multi + '0'
                dfscaleLen = dfscaleLen - 1
            scalebar = each * int(multi)
            dataframescale = scalebar * 12
            return scalebar,dataframescale
            break
mxd = arcpy.mapping.MapDocument("CURRENT") 
df = arcpy.mapping.ListDataFrames(mxd)[0]
df.scale = returnScale(df.scale)[1]
arcpy.RefreshActiveView()
0 Kudos
1 Reply
WesMiller
Regular Contributor III

For anyone who might be interested in the future the code is not elegant, but it is functional. I also converted a scale bar to a graphic and I'm updating the text elements on that scale bar.

def returnScale(dfscale):
    scalebar = [2,3,4,5,6,10]
    dfscale = dfscale/12
    dfscale = str(int(dfscale))
    dfscaleLen = len(dfscale)
    numcheck = int(dfscale[0])
    for each in scalebar:
        if numcheck < each:
            multi = '1'
            while dfscaleLen > 1:
                multi = multi + '0'
                dfscaleLen = dfscaleLen - 1
            scalebar = each * int(multi)
            dataframescale = scalebar * 12
            return scalebar,dataframescale
            break
        
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
scalebar,dataframescale = returnScale(df.scale)
scalebarDict = {"scaletextElm1":scalebar,"scaletextElm2":scalebar/2}
for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
    if elm.name in scalebarDict:
        elm.text = scalebarDict[elm.name]
df.scale = dataframescale
arcpy.RefreshActiveView() 
0 Kudos