How to Convert DMS (sexagesimal) to Decimal Co-ordinates

Posted on 2008-11-03
Last Modified: 2013-12-24
I'm trying to get geo-locations (latitudes and longitudes) of photos into a database in a decimal format from EXIF gps data. However, the EXIF fields 'gps latitude' and 'gps longitude' are in a DMS (Degrees, minutes seconds) format. The value of the EXIF field 'gps latitude ref' is either 'N' (north) if the point is north of the equator, or 'S' if it is south of the equator, and 'gps longitude ref' is either 'W' if the point is to the west of the meridian line at Greenwich, or 'E' if it is to the east. The Coldfusion dump of the data looks (for example) like this: 52"15'57.600002 N 3"34'31.799927 W, as in:

<cfimage action="read" source="IMG_6384.jpg" name="myImage" />    
<cfset latitude = ImageGetEXIFTag(myImage,"gps latitude") />
<cfset longitude = ImageGetEXIFTag(myImage,"gps longitude") />
<cfset latref = ImageGetEXIFTag(myImage,"gps latitude ref") />
<cfset lonref = ImageGetEXIFTag(myImage,"gps longitude ref") />

<cfoutput>#latitude# #latref#</cfoutput><br>
<cfoutput>#longitude# #lonref#</cfoutput>

Can anyone come up with a way of converting the DMS values to a decimal number? I think I'm far from being the only person who would find a solution to this useful. I've attached an image file with the required EXIF data.
Question by:gohols
    LVL 36

    Expert Comment

    there's some JS here which could be adapted or cfscript-ed
    LVL 27

    Expert Comment

    it looks like the image you attached does not have any EXIF data in it, so i am going on your description of what the EXIF data looks like:

    [code blow uses your LATITUDE and LONGITUDE variables and assumes they are in DD"MM'SS.SSSS format]

    <cfset degrees_lat = (listlast(latitude, chr(34)&chr(34))/60 + listgetat(latitude, 2, chr(34)&chr(39)))/60 + listfirst(latitude, chr(34)&chr(39))>

    <cfset degrees_lng = (listlast(longitude, chr(34)&chr(34))/60 + listgetat(longitude, 2, chr(34)&chr(39)))/60 + listfirst(longitude, chr(34)&chr(39))>


    Author Comment

    Just checked and it looks like EE has stripped out the EXIF and IPTC data. The file with these intact is at

    I tried the solution from azadisaryev and it's probably essentially ok, but it throws an error:

    " The value 15'57.600002 cannot be converted to a number. "
    LVL 27

    Accepted Solution

    sorry, a typo in my code:
    i have chr(34)&chr(34), but it should be chr(34)&chr(39)

    so, correct code is:

    <cfset degrees_lat = (listlast(latitude, chr(34)&chr(39))/60 + listgetat(latitude, 2, chr(34)&chr(39)))/60 + listfirst(latitude, chr(34)&chr(39))>

    <cfset degrees_lng = (listlast(longitude, chr(34)&chr(39))/60 + listgetat(longitude, 2, chr(34)&chr(39)))/60 + listfirst(longitude, chr(34)&chr(39))>

    fiy, in case you were wondering, chr(34) is an ascii code for " (double quote) and chr(39) for ' (single quote)


    Author Comment

    Thanks, Azadisaryev - that works a treat.

    Anyone using this should bear in mind that if the value of the IPTC tag 'gps latitude ref' is S, or if the value of 'gps longitude ref' is E, the decimal version of the co-ordinate will be a minus value so you will need to append the '-' character to the start of the number.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Title # Comments Views Activity
    nginx reverse proxy 6 71
    URL redirect 4 43
    Forbidden errors 5 95
    IPA - can it be run on a web server? 3 98
    This is a guide to setting up a new WHM/cPanel Server to be used for web hosting accounts. It is intended for web hosting company administrators and dedicated server owners. For under $99 per month (considering normal rate of Big Data Cetnters like …
    Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now