-
Notifications
You must be signed in to change notification settings - Fork 160
/
server.js
150 lines (107 loc) · 3.64 KB
/
server.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
var fs = require('fs'),
express = require('express'),
app = express(),
polyline = require('polyline'),
sqlite3 = require("sqlite3");
var port = process.env.PORT || 3000; // set our port
var db = new sqlite3.Database('db');
var summary = [];
var featureCollection = {
type:"FeatureCollection",
features:[]
};
var router = express.Router();
app.use(express.static(__dirname + '/public'));
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
router.get('/trips', function(req, res, next) {
var getMedallion = "select medallion from `trips` order by random() limit 1";
db.serialize(function() {
db.each(getMedallion, function(err, result) { //pick a medallion at random
console.log("Getting trips for Medallion " + result.medallion);
if (err) { console.log(err); }
getRows(result.medallion,function(rows){ //get all rows for our medallion
createGeojson(rows,function(geojson){ //convert polylines to geojson
console.log("Sending Results");
res.json(geojson); //send the response
});
});
});
});
});
app.use('/', router);
app.listen(port);
console.log('Listening on port ' + port);
//functions
function getRows(medallion,callback) {
var statement = "select * from 'trips' where medallion = '" + medallion + "'";
summary = [];
db.serialize(function() {
db.each(statement, function(err, result) {
if (err) { console.log(err); }
summary.push(result);
},function(){
console.log(summary.length + " rows found for this medallion");
callback(summary);
});
});
}
function createGeojson(rawData,callback){
featureCollection.features = [];
for(var i=0;i<rawData.length;i++) {
coordinates = rawData[i].trippolyline;
coordinates2 = rawData[i].nextpolyline;
coordinates = polyline.decode(coordinates);
coordinates2 = polyline.decode(coordinates2);
var feature = {
type:"Feature",
properties:{},
geometry:{
type:"LineString",
coordinates:[]
}
}
var feature2 = {
type:"Feature",
properties:{},
geometry:{
type:"LineString",
coordinates:[]
}
}
feature.properties.medallion = rawData[i].medallion;
feature.properties.passengers = rawData[i].passengers;
feature.properties.fare = rawData[i].fare;
feature.properties.paymenttype = rawData[i].paymenttype;
feature.properties.surcharge = rawData[i].surcharge;
feature.properties.mtatax = rawData[i].mtatax;
feature.properties.tip = rawData[i].tip;
feature.properties.tolls = rawData[i].tolls;
feature.properties.total = rawData[i].total;
feature.properties.pickuptime = rawData[i].pickuptime;
feature.properties.dropofftime = rawData[i].dropofftime;
feature.properties.nextpickuptime = rawData[i].nextpickuptime;
feature.properties.key = rawData[i].key;
feature.properties.hasfare = true;
feature2.properties.pickuptime = rawData[i].dropofftime;
feature2.properties.dropofftime = rawData[i].nextpickuptime;
feature2.properties.key = rawData[i].key;
feature2.properties.hasfare = false;
for(var j=0;j<coordinates.length;j++){
var coord = [coordinates[j][1],coordinates[j][0]]
//create a feature
feature.geometry.coordinates.push(coord);
};
for(var j=0;j<coordinates2.length;j++){
var coord = [coordinates2[j][1],coordinates2[j][0]]
//create a feature
feature2.geometry.coordinates.push(coord);
};
featureCollection.features.push(feature);
featureCollection.features.push(feature2);
}
callback(featureCollection);
}