From 36a09ea6caf9b606fcd918ead56f8691f2ba8ac8 Mon Sep 17 00:00:00 2001
From: slomo <steve.harrison@gmx.net>
Date: Fri, 21 Jan 2011 20:58:22 +0100
Subject: jslint says: ok

---
 src/nodejs/main.js         | 103 +++++++++++++++++++++++----------------------
 src/nodejs/xmlGenerator.js |  53 ++++++++++++-----------
 2 files changed, 81 insertions(+), 75 deletions(-)

(limited to 'src/nodejs')

diff --git a/src/nodejs/main.js b/src/nodejs/main.js
index 17a5235..b825e75 100644
--- a/src/nodejs/main.js
+++ b/src/nodejs/main.js
@@ -7,15 +7,6 @@ var config = require('./config.json');
 var log4js = require('log4js')(); 
 var log = log4js.getLogger('global');
 
-var options = [
-      { short       : 'c'
-      , long        : 'config'
-      , description : 'Select configuration file'
-      , value       : true
-      , callback    : function(value) { loadCustomConfig(value); }
-      }
-];
-
 function loadCustomConfig(path) {
         log.info("reading custom config: " + path);
         if( path[0] != '/'){
@@ -25,17 +16,27 @@ function loadCustomConfig(path) {
         config = require(path);
 }
 
+var options = [
+      { short       : 'c' ,
+        long        : 'config' ,
+        description : 'Select configuration file' ,
+        value       : true ,
+        callback    : function(value) { loadCustomConfig(value); }
+      }
+];
+
+
 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) \
-               )',
+        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'
     };
@@ -43,15 +44,15 @@ function createWayBboxQuery(key, value, left, bottom, right, top) {
 
 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) \
-               )',
+        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'
     };
@@ -59,21 +60,36 @@ function createNodeBboxQuery(key, value, left, bottom, right, top) {
 
 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))',
+        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) {
+        if(err) {
+            log.error('message');
+            console.log(config.connectionString);
+            console.log(err);
+            res.writeHead(404,{});
+            res.end();
+        } else {
+            log.info("db connection was successfull");
+            callback(client);
+        }
+    });
+}
+
 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) {
-    db_connect(res, function(client) {
+    dbConnect(res, function(client) {
         var success = false;
         var query = client.query(createNodeBboxQuery(key, value, left, bottom, right, top));
 
@@ -117,23 +133,9 @@ function connectionError(err, res) {
     log.fatal("connectionError not implemented");
 }
 
-function db_connect(res, callback) {
-    pg.connect(config['connectionString'], function(err, client) {
-        if(err) {
-            log.error('message');
-            console.log(config['connectionString']);
-            console.log(err);
-            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) {
+    dbConnect(res, function(client) {
         var count = 0;
         var success = false;
         //console.log(createWayBboxQuery(key, value, left, bottom, right, top));
@@ -147,7 +149,7 @@ function wayBboxHandler(req, res, key, value, left, bottom, right, top) {
 
         query.on('end', function() {
             if(success) {
-                if(count == 0) {
+                if(count === 0) {
                     res.write("</xml>");
                     res.end();
                 }
@@ -174,9 +176,10 @@ function wayBboxHandler(req, res, key, value, left, bottom, right, top) {
                 subquery.on('error',function(err) {});
                 subquery.on('end', function() {
                     count--;
-                    if(count==0)
+                    if(count === 0){
                         res.write("</xml>");
-                    res.end();
+                        res.end();
+                    }
                 });
                 subquery.on('row', function(row) {
                     res.write(xmlGenerator.createNode(row));
@@ -206,7 +209,7 @@ myRoutes = clutch.route404([
     //['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+)\\]$',relationWorldHandler]
     //['GET /api/relation\\[(\\w+)=(\\w+)\\](\\[bbox=(\\d),(\\d),(\\d),(\\d)\\])$',relationBboxHandler],
 ]);
 
diff --git a/src/nodejs/xmlGenerator.js b/src/nodejs/xmlGenerator.js
index 17143a8..08ea833 100644
--- a/src/nodejs/xmlGenerator.js
+++ b/src/nodejs/xmlGenerator.js
@@ -8,6 +8,27 @@ var log4js = require('log4js')(); //note the need to call the function
 var log = log4js.getLogger('xmlGenerator');
 log.setLevel(config.logLevel);
 
+function toISO8601(date) {
+    //2007-03-31T00:09:22+01:00
+    var pad_two = function(n) {
+        return (n < 10 ? '0' : '') + n;
+    };
+
+    return [
+        date.getUTCFullYear(),
+        '-',
+        pad_two(date.getUTCMonth() + 1),
+        '-',
+        pad_two(date.getUTCDate()),
+        'T',
+        pad_two(date.getUTCHours()),
+        ':',
+        pad_two(date.getUTCMinutes()),
+        ':',
+        pad_two(date.getUTCSeconds()),
+        '+01:00'	//FIX ME
+            ].join('');
+}
 
 exports.createNode = function (row) {
     log.debug(row);
@@ -20,52 +41,34 @@ exports.createNode = function (row) {
         .att('lon', row.lon);
     if(row.tags != '{}') {
         var temp = row.tags.replace("{","").replace("}","").split(",");
-        for(var x=0;x<temp.length;x=x+2)
+        for(var x=0;x<temp.length;x=x+2){
             node.ele('tag')
                 .att('k',escape(temp[x]))
                 .att('v',escape(temp[x+1]));
+        }
     }
     return builder.toString({ pretty: true });
 };
 
 exports.createWay = function (row) {
+    var temp;
     var way = builder.begin('way')
         .att('id', row.id)
         .att('timestamp', toISO8601(row.tstamp))
         .att('version', row.version)
         .att('changeset', row.changeset_id);
     if(row.tags != '{}') {
-        var temp = row.tags.replace("{","").replace("}","").split(",");
-        for(var x=0;x<temp.length;x=x+2)
+        temp = row.tags.replace("{","").replace("}","").split(",");
+        for(var x=0;x<temp.length;x=x+2){
             way.ele('tag')
                 .att('k',escape(temp[x]))
                 .att('v',escape(temp[x+1]));
+        }
     }
-    var temp = row.nodes.replace("{","").replace("}","").split(",");
+    temp = row.nodes.replace("{","").replace("}","").split(",");
     for(var i=0;i<temp.length;i++) {
         way.ele('nd').att('ref',temp[i]);
     }
     return builder.toString({pretty:'true'});
 };
 
-function toISO8601(date) {
-    //2007-03-31T00:09:22+01:00
-    var pad_two = function(n) {
-        return (n < 10 ? '0' : '') + n;
-    };
-
-    return [
-        date.getUTCFullYear(),
-        '-',
-        pad_two(date.getUTCMonth() + 1),
-        '-',
-        pad_two(date.getUTCDate()),
-        'T',
-        pad_two(date.getUTCHours()),
-        ':',
-        pad_two(date.getUTCMinutes()),
-        ':',
-        pad_two(date.getUTCSeconds()),
-        '+01:00'	//FIX ME
-            ].join('');
-}
-- 
cgit v1.2.3