1

I'm trying to convert coordinates to DMS, I try to use field calculator, but I still have no result.

I also try this script:

(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'"

Any proposition?

4
  • What happened when you ran the script that you have presented?
    – PolyGeo
    Commented Dec 6, 2019 at 0:57
  • I receive an empty field !!
    – Ch.abdel
    Commented Dec 6, 2019 at 20:59
  • When you use the || symbol to join strings, if any of the strings are NULL, the entire output is empty. Try using the concat() function instead of concatenating with || and see if that helps.
    – csk
    Commented Dec 6, 2019 at 21:30
  • Please use the edit button beneath your question to revise it with any requested clarifications.
    – PolyGeo
    Commented Dec 6, 2019 at 23:01

3 Answers 3

2

You should use the to_dms expression.

to_dms($x, 'x', 3)

If your geometry is not in EPSG:4326, reproject it on the fly, like this:

to_dms(x(transform($geometry,'EPSG:2154','EPSG:4326')), 'x', 3)

Source : http://changelog.qgis.org/en/qgis/version/3.4-LTR/#map-expressions

2
  • using to_dms in Map Canvas give me an error, e.g. to_dms(x_min(map_get( item_variables( 'Map 1'),'map_extent')),'x',3) but using x() is okay ...
    – Kampau Ocu
    Commented Jun 21, 2022 at 11:22
  • Yes, the canvas does not have the variable 'Map 1'. There is already variables @map_extent, $x, $y etc.
    – etrimaille
    Commented Jun 22, 2022 at 5:54
0

Open the attribute table and start editing, then open the field calculator; Create a new text field of length 16, named ‘lon_dms’, and enter the code below in the ‘Expression’ box; Calculate, then repeat for ‘lat_dms’ but change each $x to $y; Save

(CASE WHEN $x < 0 THEN 'N' ELSE 'S' 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) || '"'

[I got this code from www.underdiverwaterman.com, but the link/website is currently down]

1
  • I receive an empty field !!
    – Ch.abdel
    Commented Dec 6, 2019 at 20:59
0

I substituted the concat(string1,string2,string3) concatenation method for the || character. When concatenating a complicated expression like this, concat() is better because it converts NULL values to empty strings. When using ||, if the expression has any mistakes that evaluate to NULL, the whole output is empty, which is very difficult to troubleshoot. I did a bit of additional cleaning up and fixed a few mistakes (see my notes below). It's not perfect yet, but it should give you a starting point for more troubleshooting.

concat(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')

Notes:

The biggest issue was the '\'' term. I assume the backslash is supposed to escape the single quotation mark, but it wasn't working that way. Instead it was printing a backslash as though the expression was '\' and then everything after the third single quotation mark was converted into a string.

I couldn't figure out a way to get the single quotation mark into the output, so I gave up and substituted the tick mark: `

If it's important that you have a single quotation mark in there, try playing around with the char(code) function. It should take a unicode code number and convert it into that symbol. I tried a few different unicode values that should convert to an apostrophe or single quote, and they gave me boxes or square corner brackets. Maybe QGIS uses a different version of unicode than my Google search results, or maybe the preview is just broken.

Another issue was the final term, '" E'". I used the same workaround as for the minutes, and just substituted a double tick mark. If you want some single or double quotes in the final output, see my notes above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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