Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

It is quite common for paper topographic maps to label the map graticule with ordinate values in a mixture of superscript and standard digits, eg. ⁴123⁰⁰⁰ to emphasise the significant digits.

The QGIS Map composer allows graticules (termed grids) to be drawn on the map, and the ordinate labels to be customised, but does not recognise HTML tags such as <sup>.

Is there any other way to achieve this effect?

share|improve this question
up vote 5 down vote accepted

Unicode has some superscript and subscript characters in it as described on Wikipedia

Here are two custom functions that can be entered in the QGIS Python Function Editor that superscript or subscript the digits in strings passed to them:

Superscript

@qgsfunction(args='auto', group='Custom')
def supscr_num(inputText, feature, parent):
  """ Converts any digits in the input text into their Unicode superscript equivalent.
Expects a single string argument, returns a string"""
  supScr = (u'\u2070',u'\u00B9',u'\u00B2',u'\u00B3',u'\u2074',u'\u2075',u'\u2076',u'\u2077',u'\u2078',u'\u2079')
  outputText = ''

  for char in inputText:
    charPos = ord(char) - 48
    if charPos <0 or charPos > 9:
        outputText += char
    else:
        outputText += supScr[charPos]
  return outputText

Subscript

@qgsfunction(args='auto', group='Custom')
def subscr_num(inputText, feature, parent):
""" Converts any digits in the input text into their Unicode subscript equivalent.
Expects a single string argument, returns a string"""
  outputText = ''

  for char in inputText:
    charPos = ord(char) - 48
    if charPos <0 or charPos > 9:
        outputText += char
    else:
        outputText += unichr(charPos + 8320)
  return outputText

These can then be used in an expression as follows:

supscr_num('4') || "123" || supscr_num('000')

Returning the ⁴123⁰⁰⁰ in the question

A more generic example of the same expression:

supscr_num(to_string(@grid_number // 100000)) || lpad ( to_string ( (@grid_number % 100000) // 1000),3,'0') || supscr_num(lpad (to_string(@grid_number % 1000),3,'0'))

Unfortunately there is incomplete support for the latin alphabet super and subscripts in unicode, so this technique cannot be extended completely.

share|improve this answer
    
This looks really promising - but how does one get the X or Y coords within an expression in the 'custom' option of the 'Draw coordinates' section? – Simbamangu Feb 18 at 13:54
    
There are two custom variables that are available (at the top of the list) in the expression editor - grid_axis (gives you the x or y axis), and grid_number (the ordinate) – Andy Harfoot Feb 18 at 14:06
    
Oops, incredibly obvious when you look. Excellent use of custom functions! – Simbamangu Feb 18 at 14:09
    
I've added a generic example using the @grid_number variable to illustrate – Andy Harfoot Feb 18 at 14:21

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.