UNPKG

bdq

Version:

Biodiversity Data Quality Mechanisms

52 lines (47 loc) 1.93 kB
/* <b>Mechanism:</b> Convert a non-parsed coordinate in string from DMS format to Decimal format. */ exports.DMSCoordinateToDecimal = function (coordinate) { var p = exports.DMSCoordinateParser(coordinate); return exports.ParsedDMSCoordinateToDecimal(p.degrees, p.minutes, p.seconds, p.direction); } /* <b>Mechanism:</b> Convert a parsed coordinate from DMS format to Decimal format. */ exports.ParsedDMSCoordinateToDecimal = function (degrees, minutes, seconds, direction) { var dd = parseFloat(degrees) + parseFloat(minutes)/60 + parseFloat(seconds)/(60*60); if (direction == "S" || direction == "W") { dd = dd * -1; } // Don't do anything for N or E return dd; } /* <b>Mechanism:</b> Parse a string coordinate in Degrees, Minutes, Seconds and Direction format to an Object with degrees, minutes, seconds and direction spllited. <b>Specification:</b> To ensure that invalid characters will not influence in the parser, it selects only numbers until a non-number to Degree, the same with the next characters for Minutes and Seconds and select a value S, W, N or E to the Direction. */ exports.DMSCoordinateParser= function (coordinate){ coordinate = coordinate.trim(); var r = new Object(); r.degrees = ""; r.minutes = ""; r.seconds = ""; var directionPosition = coordinate.toUpperCase().search(/S|W|N|E/); r.direction = coordinate.substring(directionPosition,directionPosition+1); var i = 0; var d = 0; while(d<3){ var s = coordinate.substring(i,coordinate.length).search(/[0-9]|\./); if(s==0){ //Number r.degrees += d==0? coordinate.substring(i,i+1):""; r.minutes += d==1? coordinate.substring(i,i+1):""; r.seconds += d==2? coordinate.substring(i,i+1):""; i++; }else{ // Non-Number i++; d++; } } return r; }