From b0bece3d03570a0cba9adfed8617b58006dce3a5 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Fri, 21 Jan 2011 16:22:47 +0100 Subject: fixed codelayout --- src/nodejs/no1.js | 362 +++++++++++++++++++++++++++--------------------------- 1 file changed, 180 insertions(+), 182 deletions(-) (limited to 'src/nodejs') diff --git a/src/nodejs/no1.js b/src/nodejs/no1.js index 1b71766..18b8c41 100644 --- a/src/nodejs/no1.js +++ b/src/nodejs/no1.js @@ -5,15 +5,15 @@ var builder = require('xmlbuilder'); // load config var config = require('./config.json'); process.argv.forEach( - function (val,index, array){ - if(val=="-c"){ - path = array[index+1]; - if( path[0] != '/'){ - path = __dirname + '/' + path; - } - config = require(path); + function (val,index, array){ + if(val=="-c"){ + path = array[index+1]; + if( path[0] != '/'){ + path = __dirname + '/' + path; } - }); + config = require(path); + } + }); var connectionString = config['connectionString']; //set up logger @@ -26,13 +26,11 @@ logger.setLevel('ALL'); logger.info("server starting..."); function toISO8601(date) { - //2007-03-31T00:09:22+01:00 + //2007-03-31T00:09:22+01:00 var pad_two = function(n) { return (n < 10 ? '0' : '') + n; }; - var pad_three = function(n) { - return (n < 100 ? '0' : '') + (n < 10 ? '0' : '') + n; - }; + return [ date.getUTCFullYear(), '-', @@ -48,81 +46,81 @@ function toISO8601(date) { '+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));"; + 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));"; } function createNodeBboxQuery(key, value, left, bottom, right, top) { - return "SELECT id, user_id,tstamp,version,changeset_id, hstore_to_array(tags) as tags, X(geom) as lat, Y(geom) as lon FROM nodes WHERE (tags @> hstore('" + key + "','" + value + "') AND geom && st_setsrid(st_makebox2d(st_setsrid(st_makepoint(" + - left + "," + bottom + "),4326), st_setsrid(st_makepoint(" + right + "," + top + "),4326)),4326));"; + return "SELECT id, user_id,tstamp,version,changeset_id, hstore_to_array(tags) as tags, X(geom) as lat, Y(geom) as lon FROM nodes WHERE (tags @> hstore('" + key + "','" + value + "') AND geom && st_setsrid(st_makebox2d(st_setsrid(st_makepoint(" + + left + "," + bottom + "),4326), st_setsrid(st_makepoint(" + right + "," + top + "),4326)),4326));"; } function createNodesForWayQuery(nodes) { - return "SELECT id, tstamp, version, changeset_id, hstore_to_array(tags) as tags, X(geom) as lat, Y(geom) as lon FROM nodes WHERE id = ANY('" + nodes + "');"; + return "SELECT id, tstamp, version, changeset_id, hstore_to_array(tags) as tags, X(geom) as lat, Y(geom) as lon FROM nodes WHERE id = ANY('" + nodes + "');"; } function nodeWorldHandler(req, res, key, value) { - res.writeHead(200, {'Content-Type': 'text/plain'}); res.end(' key:' +key +' value:'+value+'\n'); } + function nodeBboxHandler(req, res, key, value, left, bottom, right, top) { - console.log("nodeBboxHandler"); - db_connect(res, function(client) { - console.log(createNodeBboxQuery(key, value, left, bottom, right, top)); - var success = false; - var query = client.query(createNodeBboxQuery(key, value, left, bottom, right, top)); - - query.on('error', function(err) { - - console.log(err); - res.writeHead(404,{}); - res.end('\n'); - }); - - query.on('end', function() { - //console.log("end event\n"); - if(success) { - res.write(""); - res.end(); - } - else { - //empty response - res.writeHead(404,{}); - res.end(); - //perhaps write 404? is error also raised? - } - }); - - query.on('row', function(row) { - - if(!success) { - success = true; - res.writeHead(200, {'Content-Type': 'text/plain'}); - res.write(""); - } - - console.log(row); - - var node = builder.begin('node') - .att('id', row.id) - .att('timetamp', 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"); + res.end(); + } + else { + //empty response + res.writeHead(404,{}); + res.end(); + //perhaps write 404? is error also raised? + } + }); + + query.on('row', function(row) { + if(!success) { + success = true; + res.writeHead(200, {'Content-Type': 'text/plain'}); + res.write(""); + } + + console.log(row); + + var node = builder.begin('node') + .att('id', row.id) + .att('timetamp', 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"); - res.end(); - } - //res.write(""); - //res.end(); //problem!!! - } - else { - res.writeHead(404,{}); - res.end(); - //perhaps write 404? - } - }); - - query.on('row', function(row) { - if(!success) { - success = true; - res.writeHead(200, {'Content-Type': 'text/plain'}); - res.write(""); - } - //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(""); - res.end(); - }); - subquery.on('row', function(row) { - console.log(row); - var node = builder.begin('node') - .att('id', row.id) - .att('timetamp', 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"); + res.end(); + } + //res.write(""); + //res.end(); //problem!!! + } + else { + res.writeHead(404,{}); + res.end(); + //perhaps write 404? + } + }); + + query.on('row', function(row) { + if(!success) { + success = true; + res.writeHead(200, {'Content-Type': 'text/plain'}); + res.write(""); + } + //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(""); + res.end(); }); + subquery.on('row', function(row) { + console.log(row); + var node = builder.begin('node') + .att('id', row.id) + .att('timetamp', 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