7

I have a scalebar with a element-width of 70 mm. It has three segments and their width is set to be between 50 and 60 mm. Scalebar units are set to [m]. This works fine when it comes to automated adaption to different scales.

But when my scales get smaller (1:30,000, 1:50,000 and similar) the labels of the scalebar change from hundreds of meters to thousands, which reduces readability.

Is there a way to automatically change the scalebar units e.g. from [m] to [km] when crossing a certain threshold of scale?

1 Answer 1

10

I couldn't find a manual solution. But It is easily performed using PyQGIS.

threshold = 50000 # 1:50.000

# get layout and items
manager = QgsProject.instance().layoutManager()
layout = manager.layoutByName("layout_name")
mapItem = layout.itemById("map_name")
scalebar = layout.itemById("scalebar_name")

def map_extent_changed():    
    if mapItem.scale()>= threshold:        
        scalebar.setUnits(1) # 1: kilometer
        scalebar.setUnitLabel("km")
    else:
        scalebar.setUnits(0) # 0: meter
        scalebar.setUnitLabel("m")
    
mapItem.extentChanged.connect(map_extent_changed)

# to be sure 
# if isinstance(mapItem, QgsLayoutItemMap):
#    mapItem.extentChanged.connect(map_extent_changed)


enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.