forked from codeforamerica/route_view
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
74 lines (57 loc) · 2.02 KB
/
app.js
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
var request = require("request");
var express = require("express");
var app = express.createServer();
var fs = require("fs");
app.use(express.bodyParser());
//load cameras
var camfile = fs.readFileSync("data/cameras.json");
var cameras = JSON.parse(camfile);
// This is a server side implementation of the search for cameras along a route.
// Currently Route View does this client side
app.all('/api/cameras', function(req, resp){
var wktline = null;
wktline = geoJSONToWKT(req.param("route"));
if(wktline){
var routeLine = new geonode.Geometry(wktline);
routeLine.srid = 4326;
var buffered = routeLine.buffer(0.00025);
var alongRoute = [];
for(var i=0; i< cameras.length; i++){
var cam = cameras[i];
try{
console.log("contains ",i, cameras.length, cam.point.toWkt());
console.log((buffered.contains(cam.point)));
if(buffered.contains(cam.point)){
alongRoute.push({description:cam.description,
imageURL: cam.cameraImageURL,
geometry:cam.geometry});
}
}catch(e){
console.log(e);
}
}
resp.send({cameras:alongRoute});
}
});
var geoJSONToWKT = function(geojson){
var wkt = geojson.type.toUpperCase()+"(";
var points = [];
for(var i=0; i<geojson.coordinates.length;i++){
points.push(geojson.coordinates[i].join(" "))
}
wkt+=points.join(",")+")";
return wkt;
}
// This proxy the request to a camera in order to add the referer it expects
app.all('/camera', function(req, resp){
var url = "http://goakamai.org/"+req.param("url", "");
req.headers.referer = "http://goakamai.org/Home.aspx";
var x = request(url)
req.pipe(x)
x.pipe(resp)
});
app.use('/', express.static(__dirname + '/'));
var port = process.env.PORT || 3005;
app.listen(port, function() {
console.log("Listening on " + port);
});