aboutsummaryrefslogtreecommitdiffstats
path: root/src/nodejs/no1.js
blob: 1a250be9782260783b1a2d3cbd35b4781739955f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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');