QGIS Grids – Custom Format for a Grid with Degrees, Minutes, Seconds Using Suffix from Another Language

grids-graticulesinternationalizationqgisqgis-2qgis-3

I am designing a map in French where West is "Ouest", and thus the map should read 45°23′67′′O instead of 45°23′67′′W. I am using a version of QGIS in English (update: in the French version of QGIS, the default option is O). I am currently using QGIS 2.18.11, but intend to switch to 3.0 soon.

1) Is there a way to change the longitude and latitude suffix to another language or just set a different letter?

2) Using the custom option, how can I get the minutes and the seconds? I found an example here that gets me a decimal degrees version with my own suffix, but I would like to keep degrees, minutes, seconds format. If @grid_number is degrees… what is the equivalent for minutes and seconds?

abs(format_number( @grid_number ,2)) 
|| ' °' 
|| CASE 
    WHEN  @grid_axis = 'x' 
       THEN 
         IF (@grid_number > 0, 'E' , 'O')
       ELSE
           IF (@grid_number > 0, 'N' , 'S')
       END

Best Answer

Transforming decimal degrees to degrees-minutes-seconds requires some intermediate variables, so you would need to create a custom function to achieve what you want.

The way to do the conversion is taken from this post, and is enhanced with the proper formatting and french orientation letter.

Using the numbers from the screen shot at the bottom:

  1. select a custom format
  2. click the epsilon beside it
  3. select the function editor tab
  4. create a new file, name it dd_to_dms_FR
  5. paste the code below in the right pane of the function editor
  6. click load

then go back to the Expression tab and call the new function dd_to_dms_FR( @grid_number,@grid_axis )

"""
Define new functions using @qgsfunction. feature and parent must always be the
last args. Use args=-1 to pass a list of values as arguments
"""

from qgis.core import *
from qgis.gui import *

@qgsfunction(args='auto', group='Custom')
def dd_to_dms_FR(grid_number,grid_axis, feature, parent):
    degree_sign= u'\N{DEGREE SIGN}'
    is_positive = grid_number >= 0

    if grid_axis == 'x' :
      letter = 'E' if is_positive else 'O'
    else :
      letter = 'N' if is_positive else 'S'

    dd = abs(grid_number)
    minutes,seconds = divmod(dd*3600,60)
    degrees,minutes = divmod(minutes,60)
    #Uncomment below to get negative values
    #degrees = degrees if is_positive else -degrees
    output =  "%s %s %s' %s '' %s"  %(int(degrees),degree_sign ,int(minutes),int(seconds),letter)
    #To pad with zero the minutes/seconds (ex: 45° 01' 02"), use the following line
    #output =  "%s %s %02d' %02d '' %s"  %(int(degrees),degree_sign ,int(minutes),int(seconds),letter)
    return output

enter image description here

Related Question