diff options
author | Alexander Sulfrian <alexander@sulfrian.net> | 2011-01-29 14:00:40 +0100 |
---|---|---|
committer | Alexander Sulfrian <alexander@sulfrian.net> | 2011-01-29 14:00:40 +0100 |
commit | d857e652d28ab6988b60d3d72e3b4432d0125eaf (patch) | |
tree | 5ad69e6d738102bcd7fdd19034ea5efed8c36b84 /src/nodejs/main.js | |
parent | a9a2daeebf8ef351b3efe74549026ce7619873ff (diff) | |
parent | db5acf7cda7855bb22660f993cc0c25dd27bf631 (diff) | |
download | osm-xapi-d857e652d28ab6988b60d3d72e3b4432d0125eaf.tar.gz osm-xapi-d857e652d28ab6988b60d3d72e3b4432d0125eaf.tar.xz osm-xapi-d857e652d28ab6988b60d3d72e3b4432d0125eaf.zip |
Merge remote branch 'upstream/master'
Conflicts:
src/nodejs/main.js
Diffstat (limited to '')
-rw-r--r-- | src/nodejs/main.js | 240 |
1 files changed, 124 insertions, 116 deletions
diff --git a/src/nodejs/main.js b/src/nodejs/main.js index a7ff07b..80bd051 100644 --- a/src/nodejs/main.js +++ b/src/nodejs/main.js @@ -7,6 +7,7 @@ var opts = require('opts'); var osmRes = require('./response'); var log4js = require('log4js')(); var log = log4js.getLogger('global'); +var parser = require('./parse'); var config; // #################### MAY be put to different module later @@ -65,11 +66,11 @@ function rowToWay(row){ 'changeset' : row.changeset_id }; if(row.tags != '{}') { - node.tags = []; + way.tags = []; // FIXME: something doesnt work at all temp = row.tags.replace("{","").replace("}","").split(","); for(var x=0;x<temp.length;x=x+2){ - node.tags.push({ + way.tags.push({ 'k' : temp[x], 'v' : temp[x+1] }); @@ -80,120 +81,107 @@ function rowToWay(row){ // #################### MAY be put to different module later +var options = [ + { short : 'c', + long : 'config', + description : 'Select configuration file', + value : true + } +]; -// #################### my little clutch replacments - - -var urlToXpathObj = function urlToXpathObj(url){ - - // FIXME: more validaiton - // filter stars in keys - // filter no enough arguments +// +// { +// object = node/way/relation/* , +// bbox = { left : 1.0 , right : 1.0 , top : 1.0, bottom : 1.0 } +// tag = { key : [ ], value [ ] } +// } + + +function buildMainQuery(reqJso){ + + var id = 1; + var replacements = Array(); + + var selectMap = { + 'node' : 'id,user_id,tstamp,version,changeset_id,hstore_to_array(tags) as tags, ' + + 'X(geom) as lat, Y(geom) as lon', + 'way' : 'id,tstamp,version,changeset_id,nodes,user_id,hstore_to_array(tags) as tags ', + 'relation' : '' //FIXME: plz + } + + // FIXME: help me i am not side effect free + function buildTagsQuery(map){ + + tagQueries = new Array(); + + for(tagkey in map){ + tagQueries.push("tags @> hstore($" + id++ + ",$" + id++ + ")"); + replacements.push(tagkey); + replacements.push(map[tagkey]); + } - var parseKeyList = function(string){ - result = /(.+)(:?\|(.+))/.exec(string); - result.shift(); - return result; + return tagQueries.join(" OR \n"); } - var parseBboxList = function(string){ - - result = /(.+)(:?,(.+)){3}/.exec(string); - - if(result.length != 4){ - throw "error"; + // FIXME: help me i am not side effect free + function buildBbox(object,bbox){ + + var colName = { + node : 'geom', + way : 'linestring', + relation : '' // FIXME: whats my name } - result.shift(); + bboxQueryStr = colName[object] + ' && st_setsrid(st_makebox2d( ' + + ' st_setsrid(st_makepoint($' + id++ + ', $' + id++ + '), 4326), ' + + ' st_setsrid(st_makepoint($' + id++ + ', $' + id++ + '), 4326) ' + + ' ), 4326) '; + + for( direction in bbox ) { + replacements.push(bbox[direction]); + } - return { - 'left' : result[0], - 'bottom' : result[1], - 'right' : result[2], - 'top' : result[3] - }; + return bboxQueryStr; } - var xp = {}; - - result = /\/(*|node|way|relation)(:?\[(.*)=(.*)\])*/.exec(url); - - xp.object=result[1]; - - for(i=2;i<=result.length();i++){ - if(result[i]==="bbox"){ - xp.bbox = parseBboxValues(result[i+1]); - } else { - xp.tag ={}; - xp.tag.keys = parseKeyList(result[i]); - xp.tag.values = parseKeyList(result[i+1]); + function explodeTags(keys,values){ + var map = {}; + for(key in keys) { + for(value in values) { + map[keys[key]]=values[value]; // FIXME: das ist scheiße + } } - i++; + return map; } -} - - - - - - - -// ################## end my little clutch replacments - - - + + query = "SELECT " + selectMap[reqJso.object] + " FROM " + reqJso.object + "s"; + + whereClauses = Array(); + if(reqJso.bbox != undefined){ + whereClauses.push(buildBbox(reqJso.object,reqJso.bbox)); + } + + // FIXME: rename tag to tags key to keys value to values + if(reqJso.tag != undefined){ + tags = explodeTags(reqJso.tag.key,reqJso.tag.value); + whereClauses.push(buildTagsQuery(tags)); + } + if(whereClauses.length > 0) { + query += ' WHERE (' + whereClauses.join(' AND ') + ')'; + } -var options = [ - { short : 'c', - long : 'config', - description : 'Select configuration file', - value : true - } -]; + query += ';' -function createWayBboxQuery(key, value, left, bottom, right, top) { return { - text: 'SELECT id,tstamp,version,changeset_id,nodes,user_id,hstore_to_array(tags) as tags ' + - 'FROM ways ' + - 'WHERE ( ' + - ' tags @> hstore($1, $2) AND ' + - ' linestring && st_setsrid(st_makebox2d( ' + - ' st_setsrid(st_makepoint($3, $4), 4326), ' + - ' st_setsrid(st_makepoint($5, $6), 4326) ' + - ' ), 4326) ' + - ')', - values: [key, value, left, bottom, right, top], - name: 'way bbox query' - }; + text:query, + values:replacements, + name: query + }; } -function createNodeBboxQuery(key, value, left, bottom, right, top) { - return { - text: '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($1, $2) AND ' + - ' geom && st_setsrid(st_makebox2d( ' + - ' st_setsrid(st_makepoint($3, $4), 4326), ' + - ' st_setsrid(st_makepoint($5, $6), 4326) ' + - ' ), 4326) ' + - ')', - values: [key, value, left, bottom, right, top], - name: 'node bbox query' - }; -} -function createNodesForWayQuery(nodes) { - return { - text: '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($1))', - values: [nodes], - name: 'nodes for way' - }; -} function dbConnect(res, callback) { pg.connect(config.connectionString, function(err, client) { @@ -251,7 +239,7 @@ function wayBboxHandler(req, res, key, value, left, bottom, right, top) { dbConnect(res, function(client) { var count = 0; var success = false; - //console.log(createWayBboxQuery(key, value, left, bottom, right, top)); + 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) { @@ -284,26 +272,46 @@ function wayBboxHandler(req, res, key, value, 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) { +function myFunction(req,res){ -} + res.writeHead(200); + + var reqObj = parser.urlToXpathObj(req.url); + + var queryDict = buildMainQuery(reqObj); + + var resXml = osmRes.mkXmlRes(res); + -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], -]); + console.log(JSON.stringify(queryDict)); + console.log("??????????????????????????????"); + console.log(JSON.stringify(reqObj)); + dbConnect(resXml, function(client) { + var success = false; + var query = client.query(queryDict); + + query.on('error', function(err) { + res.endWith500(); + }); + + query.on('end', function() { + console.log(" EEEND "); + res.atEnd(); + }); + + query.on('row', function(row) { + //console.log(JSON.stringify(row)); + if(reqObj.object == "node") { + var pojo = rowToNode(row); + res.putNode(pojo); + } + else if(reqObj.object == "way") { + var pojo = rowToWay(row); + res.putWay(pojo);} + }); + }); +} function getConfig(configPath, callback) { if( configPath[0] != '/'){ @@ -323,7 +331,7 @@ function init(newConfig) { log.setLevel(config.logLevel); log.info("server starting..."); log.info("loaded config from " + configPath); - http.createServer(myRoutes).listen(config.port, config.host); + http.createServer(myFunction).listen(config.port, config.host); log.info("Started server at " + config.host + ":" + config.port ); } |