Sunday, 12 February 2017

Achieving Super or Subscript graticule labels in QGIS Composer windows?


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 .


Is there any other way to achieve this effect?



Answer



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.


EDIT - Note on fonts


Note that the unicode characters generated by the tool aren't present in every font. For example, using the following label expression:



 '1234567890'  || '\n'  ||  subscr_num(  '1234567890') ||  '\n'  || supscr_num(  '1234567890') 

Generates the following using Arial:


Arial exmaple


But using Times New Roman all the digits are present:


Times New Roman example


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...