2

I'm trying to convert DMS coordinate written as DD-MM-SS.SSS (ex. 17-42-35.244) in decimal, someone not human suggested me:

to_real(left("Field",2)) + 
to_real(mid("Field",4,2))/60 + 
to_real(right("Field",5))/3600

But QGIS says Expression is invalid.

If I consider only converting Degrees/Minutes/Seconds to_real(left("Field",2)) without any other mathematical operation, I obtain some results.

What is wrong?

2
  • hi, thanks for suggestion. I know about to_decimal but in my qgis it does not exist. Thanks again
    – Eppesuiga
    Mar 26 at 18:08
  • See updated answer. You seem to have a version older than 3.16. Time to update.
    – Babel
    Mar 26 at 19:36

1 Answer 1

7

Use to_decimal() function, introduced in QGIS 3.16:

to_decimal ("Field")

Example: if Field value is '17-42-35.244', the output is 17.70979


For QGIS versions older than 3.16, change your expression accordingly:

  1. Function mid() does not exist. Use function substr() instead.
  2. In the last line, as parameter of the right() function, you must include 6, not 5 characters. The dot counts as a character, too. With 5, you just have 5.244 instead of 35.244 as input.

So the modified expression looks like:

to_real(left("Field",2)) + 
to_real(substring("Field",4,2))/60 + 
to_real(right("Field",6))/3600

Your Answer

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

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