diff options
author | Philipp Borgers <borgers@mi.fu-berlin.de> | 2011-01-13 19:32:17 +0100 |
---|---|---|
committer | Philipp Borgers <borgers@mi.fu-berlin.de> | 2011-01-13 19:32:17 +0100 |
commit | de2719e8d5841abce7933ea20878de79203193dd (patch) | |
tree | ec1700467a07746be53be3eac22f8d1fc768be6e | |
parent | f9c8e0df93c69ca40e404dbc658f89c6cfbeb7fa (diff) | |
download | osm-xapi-de2719e8d5841abce7933ea20878de79203193dd.tar.gz osm-xapi-de2719e8d5841abce7933ea20878de79203193dd.tar.xz osm-xapi-de2719e8d5841abce7933ea20878de79203193dd.zip |
fight against regex succesful, fixed database request, added xmlbuilder, return xml
-rw-r--r-- | src/nodejs/no1.js | 61 |
1 files changed, 49 insertions, 12 deletions
diff --git a/src/nodejs/no1.js b/src/nodejs/no1.js index 1a250be..8eb86d7 100644 --- a/src/nodejs/no1.js +++ b/src/nodejs/no1.js @@ -1,16 +1,22 @@ var clutch = require('clutch'); var pg = require('pg') +var builder = require('xmlbuilder') var connectionString = "pg://user:password@host/database"; function createNodeBboxQuery(key, value, left, bottom, right, top) { - return "SELECT * from nodes WHERE (tags @> '\"" + key + /* + * return "SELECT * from nodes WHERE (tags @> '\"" + key + "\"=>\"" + value + "\"'" + " AND POINT(geom) @ polygon(box('(" + left + "," + bottom +")'::point,'(" + + right + "," + top + ")'::point)));"; + */ + + return "SELECT id,tstamp,version,changeset_id, 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));"; } @@ -25,37 +31,67 @@ function nodeBboxHandler(req, res, key, value, left, bottom, right, top) { if (err) { console.log(err); + res.writeHead(404,{}); + res.end('\n'); } else { console.log(createNodeBboxQuery(key, value, left, bottom, right, top)); - /*client.query(createNodeBboxQuery(key, value, left, bottom, right, top), function(err,result) { + client.query(createNodeBboxQuery(key, value, left, bottom, right, top), function(err,result) { if (err) { console.log(err); + res.writeHead(404,{}); + res.end('\n'); } else { console.log(result.rows); - for(row in result.rows.length) { - console.log(row); + + res.writeHead(200, {'Content-Type': 'text/plain'}); + //res.write("lala"); + res.write("<xml>"); + for(var i=0; i<result.rows.length;i++) { + /*//res.write(result.rows[i].id); + res.write("<node id='" + result.rows[i].id + "'" + + " timestamp='" + result.rows[i].tstamp + "'" + + + " version='" + result.rows[i].version + "'" + + //+ " changeset='" + result.rows[i].changeset_id + "'" + + ">"); + res.write("</node>"); + //console.log(result.rows[i].id); + */ + var node = builder.begin('node') + .att('id', result.rows[i].id) + .att('timetamp', result.rows[i].tstamp) + .att('version', result.rows[i].version) + .att('changeset', result.rows[i].changeset_id) + .att('lat', result.rows[i].lat) + .att('lon', result.rows[i].lon); + + + + res.write(builder.toString()); + } + res.write("</xml>"); + res.end(); } - });*/ + }); } }); + + //console.log(createNodeBboxQuery(key, value, left, bottom, right, top)); - - - res.writeHead(200, {'Content-Type': 'text/plain'}); - res.end( 'bbox: '+ left + bottom + right + top + ' key:' +key +' value:'+value+'!\n'); + //res.writeHead(200, {'Content-Type': 'text/plain'}); + //res.end( 'bbox: '+ left + bottom + right + top + ' key:' +key +' value:'+value+'!\n'); } function wayWorldHandler(req, res, key, value) { res.writeHead(200, {'Content-Type': 'text/plain'}); - res.end(' key:' +key +' value:'+value+'!\n'); + } function wayBboxHandler(req, res, key, value, bbox, left, bottom, right, top) { } @@ -72,8 +108,9 @@ 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+)\\]$',nodeBboxHandler], + //['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)\\]$',wayBboxHandler], ['GET /api/relation\\[(\\w+)=(\\w+)\\]$',relationWorldHandler], |