diff options
-rw-r--r-- | src/nodejs/no1.js | 225 | ||||
-rw-r--r-- | src/nodejs/xmlGenerator.js | 10 |
2 files changed, 117 insertions, 118 deletions
diff --git a/src/nodejs/no1.js b/src/nodejs/no1.js index 66c16c5..e9e57ad 100644 --- a/src/nodejs/no1.js +++ b/src/nodejs/no1.js @@ -4,16 +4,15 @@ var xmlGenerator = require('./xmlGenerator.js'); // load config var config = require('./config.json'); -process.argv.forEach( - function (val,index, array){ +process.argv.forEach(function (val,index, array){ if(val=="-c"){ - path = array[index+1]; - if( path[0] != '/'){ - path = __dirname + '/' + path; - } - config = require(path); + path = array[index+1]; + if( path[0] != '/'){ + path = __dirname + '/' + path; } - }); + config = require(path); + } + }); var connectionString = config['connectionString']; //set up logger @@ -27,12 +26,12 @@ log.info("server starting..."); 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));"; + 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));"; + left + "," + bottom + "),4326), st_setsrid(st_makepoint(" + right + "," + top + "),4326)),4326));"; } function createNodesForWayQuery(nodes) { @@ -47,39 +46,39 @@ function nodeWorldHandler(req, res, key, value) { function nodeBboxHandler(req, res, key, value, left, bottom, right, top) { log.error("nodeBboxHandler"); db_connect(res, function(client) { - log.info(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) { - log.error(err); - res.writeHead(404,{}); - res.end('\n'); + log.info(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) { + log.error(err); + res.writeHead(404,{}); + res.end('\n'); + }); + + query.on('end', function() { + //console.log("end event\n"); + if(success) { + res.write("</xml>"); + 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("<xml>"); + } + res.write(xmlGenerator.createNode(row)); + }); }); - - query.on('end', function() { - //console.log("end event\n"); - if(success) { - res.write("</xml>"); - 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("<xml>"); - } - res.write(xmlGenerator.createNode(row)); - }); -}); } function wayWorldHandler(req, res, key, value) { @@ -93,71 +92,71 @@ function connectionError(err, res) { function db_connect(res, callback) { pg.connect(connectionString, function(err, client) { - if(err) { - log.error('message'); - res.writeHead(404,{}); - res.end(); - } else { - log.info("db connection was successfull"); - callback(client); - } - }); + if(err) { + log.error('message'); + res.writeHead(404,{}); + res.end(); + } else { + log.info("db connection was successfull"); + callback(client); + } + }); } function wayBboxHandler(req, res, key, value, left, bottom, right, top) { db_connect(res, function(client) { - var count = 0; - var success = false; - //console.log(createWayBboxQuery(key, value, left, bottom, right, top)); - var query = client.query(createWayBboxQuery(key, value, left, bottom, right, top)); - - query.on('error', function(err) { - log.error(err); - res.writeHead(404,{}); - res.end(); - }); - - query.on('end', function() { - if(success) { - if(count == 0) { - res.write("</xml>"); - res.end(); - } - //res.write("</xml>"); - //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("<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)); - }); + var count = 0; + var success = false; + //console.log(createWayBboxQuery(key, value, left, bottom, right, top)); + var query = client.query(createWayBboxQuery(key, value, left, bottom, right, top)); + + query.on('error', function(err) { + log.error(err); + res.writeHead(404,{}); + res.end(); + }); + + query.on('end', function() { + if(success) { + if(count == 0) { + res.write("</xml>"); + res.end(); + } + //res.write("</xml>"); + //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("<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)); + }); }); } @@ -171,16 +170,16 @@ function relationBboxHandler(req, res, key, value, left, bottom, right, top) { } myRoutes = clutch.route404([ - //['GET /api/(\\w+)(\\[bbox=(\\d,\\d,\\d,\\d)\\])*\\[(\\w+)=(\\w+)\\]$', helloSomeone], - ['GET /api/node\\[(\\w+)=(\\w+)\\]$',nodeWorldHandler], - //['GET /api/node\\[(\\w+)=(\\w+)\\]\\[bbox=(\\d+(\\.\\d+)?),(\\d+),(\\d+),(\\d+)\\]$',nodeBboxHandler], - ['GET /api/node\\[(\\w+)=(\\w+)\\]\\[bbox=(\\d+(?:\\.\\d+)?),(\\d+(?:\\.\\d+)?),(\\d+(?:\\.\\d+)?),(\\d+(?:\\.\\d+)?)\\]$',nodeBboxHandler], - //['GET /api/node\\[(\\w+)=(\\w+)\\]\\[bbox=(\\d+\\.\\d+),(\\d+),(\\d+),(\\d+)\\]$',nodeBboxHandler], - ['GET /api/way\\[(\\w+)=(\\w+)\\]$',wayWorldHandler], - ['GET /api/way\\[(\\w+)=(\\w+)\\]\\[bbox=(\\d+(?:\\.\\d+)?),(\\d+(?:\\.\\d+)?),(\\d+(?:\\.\\d+)?),(\\d+(?:\\.\\d+)?)\\]$',wayBboxHandler], - ['GET /api/relation\\[(\\w+)=(\\w+)\\]$',relationWorldHandler], - //['GET /api/relation\\[(\\w+)=(\\w+)\\](\\[bbox=(\\d),(\\d),(\\d),(\\d)\\])$',relationBboxHandler], -]); + //['GET /api/(\\w+)(\\[bbox=(\\d,\\d,\\d,\\d)\\])*\\[(\\w+)=(\\w+)\\]$', helloSomeone], + ['GET /api/node\\[(\\w+)=(\\w+)\\]$',nodeWorldHandler], + //['GET /api/node\\[(\\w+)=(\\w+)\\]\\[bbox=(\\d+(\\.\\d+)?),(\\d+),(\\d+),(\\d+)\\]$',nodeBboxHandler], + ['GET /api/node\\[(\\w+)=(\\w+)\\]\\[bbox=(\\d+(?:\\.\\d+)?),(\\d+(?:\\.\\d+)?),(\\d+(?:\\.\\d+)?),(\\d+(?:\\.\\d+)?)\\]$',nodeBboxHandler], + //['GET /api/node\\[(\\w+)=(\\w+)\\]\\[bbox=(\\d+\\.\\d+),(\\d+),(\\d+),(\\d+)\\]$',nodeBboxHandler], + ['GET /api/way\\[(\\w+)=(\\w+)\\]$',wayWorldHandler], + ['GET /api/way\\[(\\w+)=(\\w+)\\]\\[bbox=(\\d+(?:\\.\\d+)?),(\\d+(?:\\.\\d+)?),(\\d+(?:\\.\\d+)?),(\\d+(?:\\.\\d+)?)\\]$',wayBboxHandler], + ['GET /api/relation\\[(\\w+)=(\\w+)\\]$',relationWorldHandler], + //['GET /api/relation\\[(\\w+)=(\\w+)\\](\\[bbox=(\\d),(\\d),(\\d),(\\d)\\])$',relationBboxHandler], + ]); var http = require('http'); diff --git a/src/nodejs/xmlGenerator.js b/src/nodejs/xmlGenerator.js index b2cf2cb..e982e18 100644 --- a/src/nodejs/xmlGenerator.js +++ b/src/nodejs/xmlGenerator.js @@ -2,7 +2,7 @@ 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'); @@ -37,13 +37,13 @@ exports.createWay = function (row) { for(var x=0;x<temp.length;x=x+2) way.ele('tag') .att('k',escape(temp[x])) - .att('v',escape(temp[x+1])); + .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]); + .att('ref',temp[x]); } return builder.toString({pretty:'true'}); } @@ -67,5 +67,5 @@ function toISO8601(date) { ':', pad_two(date.getUTCSeconds()), '+01:00' //FIX ME - ].join(''); -}
\ No newline at end of file + ].join(''); +} |