diff options
author | Mark Engel <mark.c.engel@gmail.com> | 2011-01-21 17:45:16 +0100 |
---|---|---|
committer | Mark Engel <mark.c.engel@gmail.com> | 2011-01-21 17:45:16 +0100 |
commit | eee248c50cf60c1c24b090d5c58fba6bd9711d20 (patch) | |
tree | 00b1df1df7b3c314ce567ae3f387784601d9d638 /src | |
parent | c7cee89a94511c7314265193aa469dc1b4266dd5 (diff) | |
download | osm-xapi-eee248c50cf60c1c24b090d5c58fba6bd9711d20.tar.gz osm-xapi-eee248c50cf60c1c24b090d5c58fba6bd9711d20.tar.xz osm-xapi-eee248c50cf60c1c24b090d5c58fba6bd9711d20.zip |
creation of xml data is now handled by xmlGenerator
Diffstat (limited to 'src')
-rw-r--r-- | src/nodejs/no1.js | 134 | ||||
-rw-r--r-- | src/nodejs/xmlGenerator.js | 71 |
2 files changed, 105 insertions, 100 deletions
diff --git a/src/nodejs/no1.js b/src/nodejs/no1.js index d63a8bb..9258e15 100644 --- a/src/nodejs/no1.js +++ b/src/nodejs/no1.js @@ -1,6 +1,6 @@ var clutch = require('clutch'); var pg = require('pg'); -var builder = require('xmlbuilder'); +var xmlGenerator = require('./xmlGenerator.js'); // load config var config = require('./config.json'); @@ -20,37 +20,11 @@ var connectionString = config['connectionString']; var log4js = require('log4js')(); //note the need to call the function //log4js.addAppender(log4js.fileAppender('osm-xapi.log'), 'cheese'); -var logger = log4js.getLogger('global'); -logger.setLevel('ALL'); - - var log = log4js.getLogger('global'); log.setLevel('ALL'); log.info("server starting..."); -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(''); -} - function createWayBboxQuery(key, value, left, bottom, right, top) { return "SELECT id,tstamp,version,changeset_id, nodes, user_id, hstore_to_array(tags) as tags FROM ways WHERE (tags @> hstore('" + key + "','" + value + "') AND linestring && st_setsrid(st_makebox2d(st_setsrid(st_makepoint(" + left + "," + bottom + "),4326), st_setsrid(st_makepoint(" + right + "," + top + "),4326)),4326));"; @@ -97,58 +71,17 @@ function nodeBboxHandler(req, res, key, value, left, bottom, right, top) { } }); - query.on('row', function(row) { - if(!success) { - success = true; - res.writeHead(200, {'Content-Type': 'text/plain'}); - res.write("<xml>"); - } - res.write(createXmlNode(row)); + query.on('row', function(row) { + if(!success) { + success = true; + res.writeHead(200, {'Content-Type': 'text/plain'}); + res.write("<xml>"); + } + res.write(xmlGenerator.createNode(row)); + }); }); }); } - -function createXmlNode(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 }); -} - -function createXmlWay(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 wayWorldHandler(req, res, key, value) { res.writeHead(200, {'Content-Type': 'text/plain'}); } @@ -200,30 +133,31 @@ function wayBboxHandler(req, res, key, value, left, bottom, right, top) { } }); - query.on('row', function(row) { - if(!success) { - success = true; - res.writeHead(200, {'Content-Type': 'text/plain'}); - res.write("<xml>"); - } - //console.log(row); - if(row.nodes != '{}') { - count++; - var subquery = client.query(createNodesForWayQuery(row.nodes)); - subquery.on('error',function(err) {}); - subquery.on('end', function() { - count--; - if(count==0) - res.write("</xml>"); - res.end(); - }); - subquery.on('row', function(row) { - res.write(createXmlNode(row)); - }); - - //console.log(createNodesForWayQuery(row.nodes)); - } - res.write(createXmlWay(row)); + query.on('row', function(row) { + if(!success) { + success = true; + res.writeHead(200, {'Content-Type': 'text/plain'}); + res.write("<xml>"); + } + //console.log(row); + if(row.nodes != '{}') { + count++; + var subquery = client.query(createNodesForWayQuery(row.nodes)); + subquery.on('error',function(err) {}); + subquery.on('end', function() { + count--; + if(count==0) + res.write("</xml>"); + res.end(); + }); + subquery.on('row', function(row) { + res.write(xmlGenerator.createNode(row)); + }); + + //console.log(createNodesForWayQuery(row.nodes)); + } + res.write(xmlGenerator.createWay(row)); + }); }); }); } diff --git a/src/nodejs/xmlGenerator.js b/src/nodejs/xmlGenerator.js new file mode 100644 index 0000000..b2cf2cb --- /dev/null +++ b/src/nodejs/xmlGenerator.js @@ -0,0 +1,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(''); +}
\ No newline at end of file |