Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I would like to add a scale bar to my composition using the python console and then export it to a raster image.
I am following the MapRendering tutorial with the "output using map composer " approach however,the Scale Bar item example do not have many features.

I would like

  • a scale bar with 2 ticks, one at the beginning, one at the end
  • units in km
  • eventually round it to the nearest km
  • place it on the upper right of my figure.
  • set a transparent background

Just like in the desktop QGIS version. What are the options ? So far I have found

item = QgsComposerScaleBar(c)
item.setStyle("Line Ticks Up") # optionally modify the style
item.setComposerMap(composerMap)
c.addItem(item)

Will the scale bar be "at scale" ? Thank you for your help

I cannnot find anything from that page ScalBar class

share|improve this question
    
Did you find any more tips on getting the size and positioning of table items right? I'm trying to get an attribute table on my map with no luck. –  user25976 Jul 10 at 2:02

3 Answers 3

up vote 3 down vote accepted

"Will the scale bar be "at scale" ?"

Your scale bar's scale will be relative to an existing composer map item that is assigned to it: QgsComposerScaleBar.setComposerMap(mymap_item_obj_here). So you will need to create the composer map item first, so it's scale can be referenced by the scale bar.

"I cannnot find anything from that page ScalBar class"

I found almost all the necessary functions to create the custom scale bar you want on that page.

Once the composer map reference is set, all other QgsComposerScaleBar attributes can be set using the class's public member functions, or those functions inherited by the scale bar's base class, QgsComposerItem, like setItemPosition().

Base class

Base class inheritance diagram

I guessing here, but for setting the opacity for the background to zero, you will probably want to set the QBrush used in QgsComposerScaleBar.setBrush(QBrush) to have a QColor with an alpha of 0.


EDIT (response to your further questions/clarifications):

Regarding the scale back groundI have been trying the following :

brush = ScaleBar.brush() 
brush.setColor(QColor(255, 0, 0, 0))
QgsComposerScaleBar.setBrush(brush)

I was incorrect in assuming the QBrush in question belonged to the encompassing frame. The brush is actually the color for the scale bar's font. The frame and its background are part of the inherited QgsComposerItem class. To effectively work with its methods of a similar name (setBrush() in this example), you need to access it via Python's super() function (using your ScaleBar object here):

scalebaritem = super(QgsComposerItem, ScaleBar)
scalebaritem.setBrush(QColor(0,0,0,0))

This will reference the super class of the object, and set it to transparent black, much like setting parameters in the General options section for a composer item.


I could not change the font size !

ScaleBar.font().toString()
PyQt4.QtCore.QString(u'Ubuntu,12,-1,5,50,0,0,0,0,0')
>>> ScaleBar.font().setPointSize(8)
>>> ScaleBar.font().pointSize()
12

You need to create a QFont, set its size, then set the scale bar's font to it. You can begin by pulling in the QFont from the scale bar.

f = ScaleBar.font()
f.setPointSize(8)
ScaleBar.setFont(f)
share|improve this answer
    
Thank you very much dakcarto, I could never find the super trick by myself ! You've saved my week –  Renaud Jul 7 '12 at 10:37

Thank you very much, I would really appreciate a little bit of more help. I could manage the units from meters (map units) to km

I have modify setNumMapUnitsPerScaleBarUnit() parameter and changed the label units :

ScaleBar = QgsComposerScaleBar(c)
ScaleBar.setComposerMap(composerMap)
ScaleBar.setStyle('Line Ticks Middle') # optionally modify the style
ScaleBar.setFrame(False)
ScaleBar.setUnitLabeling(QString("km"))
ScaleBar.setNumMapUnitsPerScaleBarUnit(1000)

Regarding the scale back groundI have been trying the following :

brush = ScaleBar.brush() 
brush.setColor(QColor(255, 0, 0, 0))
QgsComposerScaleBar.setBrush(brush) 

But nothing seems to change on my output. I finish my code with :

ScaleBar.setItemPosition(50,40)
ScaleBar.applyDefaultSize()
#ScaleBar.update()
c.addItem(ScaleBar)

The background color is still white. What is a QBrush by the way ?

I could not change the font size !

ScaleBar.font().toString()
PyQt4.QtCore.QString(u'Ubuntu,12,-1,5,50,0,0,0,0,0')
>>> ScaleBar.font().setPointSize(8)
>>> ScaleBar.font().pointSize()
12

Thank you

share|improve this answer

just do ScaleBar.setBackgroundEnabled(0) in Qgis 2.4

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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