-2

Possible Duplicate:
Regex/Javascript to transform Degrees Decimal Minutes to Decimal Degrees

I have some javascript code that converts Decimal Degree Minutes to Decimal Degrees. But something is slightly screwy and am hoping someone here will know how to fix it.

How to I get this to consistently work when a user removes the ° or has extra spaces?

With the ° character it works great:

input "77° 50.51086497"
output "77.8418477495" 

input "-113° 40.54687527"
output "-113.6757812545"

However without the ° it sometimes breaks:

NOT WORKING

input "77 50.51086497"
ouput "775.0085144161667" 

WORKING

input "-113 40.54687527"
output "-113.6757812545" 

Same thing with extra spaces:

NOT WORKING

input "77   ` ` ` ` ` ` ` `50.51086497" 
output "775.0085144161667" 

WORKING

input "-113   ` ` ` ` ` ` ` `40.54687527 "
output "-113.6757812545" 

Here is my code: Please see the JSFIDDLE TO TEST

function ddmToDeg(ddm) { 
    if (!ddm) { 
        return Number.NaN; 
    } 
    var neg= ddm.match(/(^\s?-)|(\s?[SW]\s?$)/)!=null? -1.0 : 1.0; 
    ddm= ddm.replace(/(^\s?-)|(\s?[NSEW]\s?)$/,''); 
    ddm= ddm.replace(/\s/g,''); 
    var parts=ddm.match(/(\d{1,3})[.,°d]?(\d{0,2}(?:\.\d+)?)[']?/); 
        //dms.match(/(\d{1,3})[.,°d]?(\d{0,2})[']?(\d{0,2})[.,]?(\d{0,})(?:["]|[']{2})?/);
    if (parts==null) { 
        return Number.NaN; 
    } 
    // parts: 
    // 0 : degree 
    // 1 : degree 
    // 2 : minutes 


    var d= (parts[1]?         parts[1]  : '0.0')*1.0; 
    var m= (parts[2]?         parts[2]  : '0.0')*1.0; 
    var dec= (d + (m/60.0))*neg; 
    return dec; 
}
Community
  • 1
  • 1
capdragon
  • 14,565
  • 24
  • 107
  • 153
  • Consider re-wording this question to make it more generally applicable to others. Just solving your very specific question doesn't help anybody else. I bet if you reworded this to be a more general question about detecting presence of a token with regex you would quickly get a lot of good answers. – Marplesoft May 09 '11 at 18:16

1 Answers1

1

I answered this on the original question: Regex/Javascript to transform Degrees Decimal Minutes to Decimal Degrees

Solution posted to your fiddle: http://jsfiddle.net/NJDp4/6/

dmsToDeg: function(dms) { 
        if (!dms) { 
            return Number.NaN; 
        } 
        var neg= dms.match(/(^\s?-)|(\s?[SW]\s?$)/)!=null? -1.0 : 1.0; 
        dms= dms.replace(/(^\s?-)|(\s?[NSEW]\s?)$/,''); 
    var parts=dms.match(/(\d{1,3})[.,°d ]?\s*(\d{0,2}(?:\.\d+)?)[']?/); 
        if (parts==null) { 
            return Number.NaN; 
        } 
        // parts: 
        // 0 : degree 
        // 1 : degree 
        // 2 : minutes 


        var d= (parts[1]?         parts[1]  : '0.0')*1.0; 
        var m= (parts[2]?         parts[2]  : '0.0')*1.0; 
        var dec= (d + (m/60.0))*neg; 
        return dec; 
    }

The reason this was working for "-113 40.54687527" but not for "77 50.51086497" is that the code was ripping out spaces and the sign (leaving "11340.54687527" and "7750.51086497") and then grabbing the first 3 digits to use as degrees (so "113" and "775"). I've modified this so that it no longer strips out spaces before grabbing the two numbers.

Community
  • 1
  • 1
Prestaul
  • 83,552
  • 10
  • 84
  • 84
  • Not exactly. This question is an extension of the original, but the question here was asked in comments on the original and was answered on the original question as well (probably after this was posted). It could almost certainly be closed as an (unintentional) duplicate. – Prestaul May 09 '11 at 18:23
  • Well, I might say that's ok. As Marplesoft points out, it could use a better question title. – Jared Farrish May 09 '11 at 18:25