Display coordinates of points in DMS format in print composer
Since QGIS 3.4, there is now a builtin expression to convert a coordinate into DMS or DM format.
to_dms( $x, 'x', 3) -> 62°31′7.951″
to_dms( $x, 'x', 3, 'suffix') -> 62°31′7.951″E
to_dm( $x, 'x', 5, 'suffix') -> 62°31.13252′E
You're in the right direction. What you need to do is use the QGIS expression builder to concatenate a string with the degrees, minutes, seconds, and any symbols you want in between.
This example calculates the $x value, and will work for positive or negative values.
(CASE WHEN $x < 0 THEN '-' ELSE '' END) || floor (abs($x)) || '° ' || floor(((abs($x)) - floor (abs($x))) * 60) ||'\'' || substr( (tostring((((abs($x)) - floor (abs($x))) * 60) - floor(((abs($x)) - floor (abs($x))) * 60)) * 60),1,5) || '"'
You can change the symbol signs, like °, ' and '' to fit any style you need.
Do note though that the resulting field has to be a string.
Thank you HDunn!
I tweaked the code a little so it also displays North/E oriëntation and has no spaces in the coördinate line:
For field (text string) X:
(CASE WHEN $x < 0 THEN '-' ELSE '' END) || floor (abs($x)) || '°' || floor(((abs($x)) - floor (abs($x))) * 60) ||'\'' || substr( (tostring((((abs($x)) - floor (abs($x))) * 60) - floor(((abs($x)) - floor (abs($x))) * 60)) * 60),1,5) || '" E'
For Y:
(CASE WHEN $y < 0 THEN '-' ELSE '' END) || floor (abs($y)) || '°' || floor(((abs($y)) - floor (abs($y))) * 60) ||'\'' || substr( (tostring((((abs($y)) - floor (abs($y))) * 60) - floor(((abs($y)) - floor (abs($y))) * 60)) * 60),1,5) || '" N'
Then to label use: "Y" || '\n' || "X"