Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// [[User:Quarl/coor_canonicalize.js]] - canonicalizes geographical coordinates

//  Example: 59° 55′ N, 10° 44′ E becomes {{coor dm|59|55|N|10|44|E|}}

// requires: wikipage.js, util.js, addlilink.js

// quarl 2006-02-01 initial version

//<pre><nowiki>

coorz = new Object();

var coorz = new autoedit(
    'coorz',
    'CoorZ', 'ca-coorz', 'Canonicalize geographic coordinates',
    'Coor canonicalization');

coorz.buildRegExp = function() {
    var sp = function(s) { return ' *' + s + ' *'; }

    var d = sp("(?:°|&deg;)");
    var m = sp("(?:′|'|&prime;|&#x2032;)");
    var s = sp('(?:″|"|&Prime;|&#x2033;)');

    var C = '(-?[0-9.]+)';

    var dms = C + d + '(?:' + C + m + '(?:' + C + s + ')?' + ')?';

    var all = '\\b' + dms + '\\s*(N|S|[Nn]orth|[Ss]outh)[ \t,]*' + dms + '\\s*(E|W|[Ee]ast|[Ww]est)' + '\\b';
    return new RegExp(all);
}

coorz.replaceRegExp = function(d, m)
{
    var latitude = [m[1], m[2], m[3]];
    var latitude_pole = m[4][0].toUpperCase();
    var longitude = [m[5], m[6], m[7]];
    var longitude_pole = m[8][0].toUpperCase();

    if (latitude[2] || longitude[2]) {
        // dms
        return ('{{coor dms|' +
                latitude[0] + '|' + latitude[1] + '|' + latitude[2] + '|' + latitude_pole + '|' +
                longitude[0] + '|' + longitude[1] + '|' + longitude[2] + '|' + longitude_pole + '|}}');
    } else if (latitude[1] || longitude[1]) {
        // dm
        return ('{{coor dm|' +
                latitude[0] + '|' + latitude[1] + '|' + latitude_pole + '|' +
                longitude[0] + '|' + longitude[1] + '|' + longitude_pole + '|}}');
    } else if (latitude[0] || longitude[0]) {
        // d
        return ('{{coor d|' +
                latitude[0] + '|' + latitude_pole + '|' +
                longitude[0] + '|' + longitude_pole + '|}}');
    } else {
        alert ("## internal error a86f430f-f362-4324-b8ce-df5f84e8f65b");
        return null;
    }
}

coorz._load = function() {
    coorz.addTab();
}

addOnloadHook(coorz._load);

//</nowiki></pre>