diff options
author | Alexander Sulfrian <alexander@sulfrian.net> | 2011-01-13 13:58:15 +0100 |
---|---|---|
committer | Alexander Sulfrian <alexander@sulfrian.net> | 2011-01-13 13:58:15 +0100 |
commit | 0f1ae045f0a561be8c42f2fcf41ca25d2b1751af (patch) | |
tree | 63791b2261c14abf4ea5c1f00492b643a10ecd12 /src/nodejs | |
parent | 280179806ad4426480032769f5296d3875044530 (diff) | |
parent | 7d9e6e0d1bd45bedc030a287330c50cde7769f56 (diff) | |
download | osm-xapi-0f1ae045f0a561be8c42f2fcf41ca25d2b1751af.tar.gz osm-xapi-0f1ae045f0a561be8c42f2fcf41ca25d2b1751af.tar.xz osm-xapi-0f1ae045f0a561be8c42f2fcf41ca25d2b1751af.zip |
Merge remote branch 'upstream/master'
Conflicts:
src/nodejs/start.js
Diffstat (limited to 'src/nodejs')
-rw-r--r-- | src/nodejs/no1.js | 85 | ||||
-rw-r--r-- | src/nodejs/no2.js | 106 | ||||
-rw-r--r-- | src/nodejs/start.js | 27 |
3 files changed, 202 insertions, 16 deletions
diff --git a/src/nodejs/no1.js b/src/nodejs/no1.js new file mode 100644 index 0000000..1a250be --- /dev/null +++ b/src/nodejs/no1.js @@ -0,0 +1,85 @@ + +var clutch = require('clutch'); +var pg = require('pg') + +var connectionString = "pg://user:password@host/database"; + + +function createNodeBboxQuery(key, value, left, bottom, right, top) { + return "SELECT * from nodes WHERE (tags @> '\"" + key + + "\"=>\"" + value + "\"'" + + " AND POINT(geom) @ polygon(box('(" + left + + "," + bottom +")'::point,'(" + + + right + "," + top + ")'::point)));"; +} + + + +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) { + pg.connect(connectionString, function(err,client) { + + if (err) { + console.log(err); + } + else { + console.log(createNodeBboxQuery(key, value, left, bottom, right, top)); + /*client.query(createNodeBboxQuery(key, value, left, bottom, right, top), function(err,result) { + + if (err) { + console.log(err); + } + else { + console.log(result.rows); + for(row in result.rows.length) { + console.log(row); + } + } + });*/ + } + + + + }); + + + + 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) { +} + +function relationWorldHandler(req, res, key, value) { + + res.writeHead(200, {'Content-Type': 'text/plain'}); + res.end(' key:' +key +' value:'+value+'!\n'); +} +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/way\\[(\\w+)=(\\w+)\\]$',wayWorldHandler], + ['GET /api/way\\[(\\w+)=(\\w+)\\]\\[bbox=(\\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'); +http.createServer(myRoutes).listen(8080, 'localhost'); diff --git a/src/nodejs/no2.js b/src/nodejs/no2.js new file mode 100644 index 0000000..233b5e1 --- /dev/null +++ b/src/nodejs/no2.js @@ -0,0 +1,106 @@ +var users = [ + { name: 'tj' }, + { name: 'tim' } +]; + +function user(app) { + app.resource('/.:format?', { + 'get' : function(req, res, next) { + switch (req.params.format) { + case 'json': + var body = JSON.stringify(users); + res.writeHead(200, { + 'Content-Type': 'application/json', + 'Content-Length': body.length + }); + res.end(body); + break; + default: + var body = '<ul>' + + users.map(function(user) { + return '<li>' + user.name + '</li>'; + }).join('\n') + + '</ul>'; + res.writeHead(200, { + 'Content-Type': 'text/html', + 'Content-Length': body.length + }); + res.end(body); + } + } + }); + + app.resource('/:id.:format', { + 'get' : function(req, res, next) { + var user = users[req.params.id]; + if (user && req.params.format === 'json') { + user = JSON.stringify(user); + res.writeHead(200, { + 'Content-Type': 'application/json', + 'Content-Length': user.length + }); + res.end(user); + } + else { + // When true is passed, provide control + // back to middleware, skipping route + // match attemps + next(true); + } + } + }) + + app.resource('/\\[:id/:op?', { + 'get' : function(req, res) { + var body = users[req.params.id] + ? users[req.params.id].name + : 'User ' + req.params.id + ' does not exist'; + body = (req.params.op || 'view') + 'ing ' + body; + res.writeHead(200, { + 'Content-Type': 'text/html', + 'Content-Length': body.length + }); + res.end(body, 'utf8'); + } + }) +} + + +function main(app) { + app.resource('/', { + 'get' : function(req, res) { + var examples = [ + '/users', + '/users.json', + '/users/0 (or /users/0/view)', + '/users/0/edit', + '/users/0.json' + ]; + var body = 'Visit one of the following: <ul>' + + examples.map(function(str) { + return '<li>' + str + '</li>' + }).join('\n') + + '</ul>'; + res.writeHead(200, { + 'Content-Type': 'text/html', + 'Content-Length': body.length + }); + res.end(body, 'utf8'); + } + }); +} + + +var connect = require('connect'); +var resource = require('resource-router'); + +var server = connect.createServer( + connect.logger({ buffer: true }), + connect.cache(), + connect.gzip() + ); + +server.use('/users', resource(user)); +server.use(resource(main)); +server.listen(3000); +console.log('Connect server listening on port 3000'); diff --git a/src/nodejs/start.js b/src/nodejs/start.js index c664cfe..52c0748 100644 --- a/src/nodejs/start.js +++ b/src/nodejs/start.js @@ -34,24 +34,19 @@ function getDataBaseResult(tag,bbox,res) { function createQuery(tag,bbox){ // FIXME: validate - // var table = tag[0] + "#" + tag[1]; - // var filter = ""; - - // // input validation - // for(i=0;i<bbox.length;i++){ - // bbox[i] = parseFloat(bbox[i]); - // } - - // if(bbox){ - // filter = "WHERE longitude > " + bbox[0] + " AND longitude < " + bbox[1] + - // " AND latitude > " + bbox[2] + " AND latitude < " + bbox[3]; - // } + var table = tag[0] + "#" + tag[1]; + var filter = ""; + // input validation + for(i=0;i<bbox.length;i++){ + bbox[i] = parseFloat(bbox[i]); + } - return "SELECT id, tags, linestring \ - FROM ways \ - WHERE (tags @> '\"amenity\"=>\"pub\"' AND \ - lseg(linestring) @ box('(13.0882097323,52.3418234221)'::point,'(13.7606105539,52.6697240587)'::point));" + if(bbox){ + filter = "WHERE longitude > " + bbox[0] + " AND longitude < " + bbox[1] + + " AND latitude > " + bbox[2] + " AND latitude < " + bbox[3]; + } + return "SELECT * FROM \"" + table + "\" " + filter + ";"; } |