5

In QGIS 2.14.2 I have a series of polylines of which I want the start and endpoint to be labeled with its coordinates in WGS84 (EPSG 4326) like this: 51*15'23.40"N, 5*17'56.58"E

I thought I figured a workaround by:

  1. Creating new point shapefile and creating points manually at start and endpoint of each polyline.
  2. Open field calculator and create a virtual field "X" containing $x and another containing $Y (Decimals)
  3. Label them by X||'\n'||Y

However the field calculator gives me an output like this X:5.26344964931 Y:51.20551633106

How do I display them in DMS format?

5

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.

6

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
2

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"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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