From 94168b354b72ab45ffd9ae1911d038f546fcafdb Mon Sep 17 00:00:00 2001 From: slomo Date: Sat, 29 Jan 2011 01:12:30 +0100 Subject: added build request --- src/nodejs/main.js | 174 ++++++++++++++++++++++++----------------------------- 1 file changed, 79 insertions(+), 95 deletions(-) (limited to 'src/nodejs/main.js') diff --git a/src/nodejs/main.js b/src/nodejs/main.js index ce4518d..2656f3d 100644 --- a/src/nodejs/main.js +++ b/src/nodejs/main.js @@ -80,119 +80,103 @@ 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 = 0; + 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 @> hastore($" + 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 ) { + replacments.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[key]=value; + } } - i++; + return map; } -} - - - - - - - -// ################## end my little clutch replacments - - - + + query = "SELECT " + selectMap[object] + " FROM " + object + "s"; + + whereClauses = Array(); + if(reqJson.bbox != undefined){ + whereClauses.push(buildBbox(reqJson.bbox)); + } + + if(reqJson.tags != undefined){ + tags = explodeTags(tags.keys,tags.values); + whereClauses.push(buildTagsQuery(tags)); + } + if(whereClauses.length > 0) { + whereClauses.join(' AND '); + query += ' WHERE (' + whereClauses + ')'; + } -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' - }; + return 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) { -- cgit v1.2.3 From 831a90dd5fe9c200a4930ee4a23af3c3cfecff5b Mon Sep 17 00:00:00 2001 From: slomo Date: Sat, 29 Jan 2011 03:00:36 +0100 Subject: integrated query create function --- src/nodejs/main.js | 99 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 62 insertions(+), 37 deletions(-) (limited to 'src/nodejs/main.js') diff --git a/src/nodejs/main.js b/src/nodejs/main.js index 2656f3d..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 hastore($" + id++ + ",$" + id++ + ")"); + tagQueries.push("tags @> hstore($" + id++ + ",$" + id++ + ")"); replacements.push(tagkey); replacements.push(map[tagkey]); } @@ -124,7 +125,7 @@ function buildMainQuery(reqJso){ // FIXME: help me i am not side effect free function buildBbox(object,bbox){ - + var colName = { node : 'geom', way : 'linestring', @@ -132,12 +133,12 @@ function buildMainQuery(reqJso){ } bboxQueryStr = colName[object] + ' && st_setsrid(st_makebox2d( ' + - ' st_setsrid(st_makepoint($' + id++ + ', $' + id++ + '), 4326), ' + - ' st_setsrid(st_makepoint($' + id++ + ', $' + id++ + '), 4326) ' + - ' ), 4326) '; + ' st_setsrid(st_makepoint($' + id++ + ', $' + id++ + '), 4326), ' + + ' st_setsrid(st_makepoint($' + id++ + ', $' + id++ + '), 4326) ' + + ' ), 4326) '; for( direction in bbox ) { - replacments.push(bbox[direction]); + replacements.push(bbox[direction]); } return bboxQueryStr; @@ -147,33 +148,37 @@ function buildMainQuery(reqJso){ var map = {}; for(key in keys) { for(value in values) { - map[key]=value; + map[keys[key]]=values[value]; // FIXME: das ist scheiße } } return map; } - query = "SELECT " + selectMap[object] + " FROM " + object + "s"; + query = "SELECT " + selectMap[reqJso.object] + " FROM " + reqJso.object + "s"; whereClauses = Array(); - if(reqJson.bbox != undefined){ - whereClauses.push(buildBbox(reqJson.bbox)); + if(reqJso.bbox != undefined){ + whereClauses.push(buildBbox(reqJso.object,reqJso.bbox)); } - if(reqJson.tags != undefined){ - tags = explodeTags(tags.keys,tags.values); + // 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) { - whereClauses.join(' AND '); - query += ' WHERE (' + whereClauses + ')'; + query += ' WHERE (' + whereClauses.join(' AND ') + ')'; } query += ';' - return query; + return { + text:query, + values:replacements, + name: query + }; } @@ -234,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) { @@ -267,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); -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], -]); + var reqObj = parser.urlToXpathObj(req.url); + + var queryDict = buildMainQuery(reqObj); + + var resXml = osmRes.mkXmlRes(res); + + + 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] != '/'){ @@ -306,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 ); } -- cgit v1.2.3