aboutsummaryrefslogtreecommitdiffstats
path: root/src/nodejs/xmlGenerator.js
blob: e982e18df8688e8440ae5b1dd5f6d4ec5b87e25a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
var builder = require('xmlbuilder');
//set up logger
var log4js = require('log4js')(); //note the need to call the function
//log4js.addAppender(log4js.fileAppender('osm-xapi.log'), 'cheese');

var log = log4js.getLogger('xmlGenerator');
log.setLevel('ALL');


exports.createNode = function (row) {
    log.debug(row);
    var node = builder.begin('node')
        .att('id', row.id)
        .att('timestamp', toISO8601(row.tstamp))
        .att('version', row.version)
        .att('changeset', row.changeset_id)
        .att('lat', row.lat)
        .att('lon', row.lon);
    if(row.tags != '{}') {
        var temp = row.tags.replace("{","").replace("}","").split(",");
        for(var x=0;x<temp.length;x=x+2)
            node.ele('tag')
                .att('k',escape(temp[x]))
                .att('v',escape(temp[x+1]));
    }
    return builder.toString({ pretty: true });
}

exports.createWay = function (row) {
    var way = builder.begin('way')
        .att('id', row.id)
        .att('timestamp', toISO8601(row.tstamp))
        .att('version', row.version)
        .att('changeset', row.changeset_id);
    if(row.tags != '{}') {
        var temp = row.tags.replace("{","").replace("}","").split(",");
        for(var x=0;x<temp.length;x=x+2)
            way.ele('tag')
                .att('k',escape(temp[x]))
                .att('v',escape(temp[x+1]));
    }

    var temp = row.nodes.replace("{","").replace("}","").split(",");
    for(var x=0;x<temp.length;x++) {
        way.ele('nd')
            .att('ref',temp[x]);
    }
    return builder.toString({pretty:'true'});
}

function toISO8601(date) {
    //2007-03-31T00:09:22+01:00
    var pad_two = function(n) {
        return (n < 10 ? '0' : '') + n;
    };

    return [
        date.getUTCFullYear(),
        '-',
        pad_two(date.getUTCMonth() + 1),
        '-',
        pad_two(date.getUTCDate()),
        'T',
        pad_two(date.getUTCHours()),
        ':',
        pad_two(date.getUTCMinutes()),
        ':',
        pad_two(date.getUTCSeconds()),
        '+01:00'	//FIX ME
            ].join('');
}