Skip to content

Commit

Permalink
all routes appear to render correctly and terminate at the destination
Browse files Browse the repository at this point in the history
  • Loading branch information
rps committed Nov 15, 2013
1 parent 220da6c commit 5941364
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 99 deletions.
43 changes: 32 additions & 11 deletions client/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,18 @@ lm.App.prototype.getStopPredictions = function(stopObj){
if(child.children[0].name !== 'message'){
var minutes = child.children[0].children[0].attr.minutes; // Child.children.children is the soonest <prediction minutes dirTag>
var dirTag = child.children[0].children[0].attr.dirTag;
userOrDest = stopObj[name+':'+dirTag].dest.stopTag === stop ? 'dest' : 'user';
lat = stopObj[name+':'+dirTag][userOrDest].lonlat[1];
lon = stopObj[name+':'+dirTag][userOrDest].lonlat[0];
color = stopObj[name+':'+dirTag][userOrDest].color;
oppositeColor = stopObj[name+':'+dirTag][userOrDest].oppositeColor;
stopLongName = stopObj[name+':'+dirTag][userOrDest].stopName; // TODO: use or delete
// self.lastStopObjArray.push({ lat: lat, lon: lon, minutes: minutes, route: name, userOrDest: userOrDest, color: color, oppositeColor: oppositeColor });
routesCovered[name+':'+dirTag] = routesCovered[name+':'+dirTag] || [];
routesCovered[name+':'+dirTag].push({ dirTitle: directionTitle, lat: lat, lon: lon, minutes: minutes, route: name, userOrDest: userOrDest, color: color, oppositeColor: oppositeColor });
// Protection against dirTags we do not have
if(stopObj[name+':'+dirTag]){
userOrDest = stopObj[name+':'+dirTag].dest.stopTag === stop ? 'dest' : 'user';
lat = stopObj[name+':'+dirTag][userOrDest].lonlat[1];
lon = stopObj[name+':'+dirTag][userOrDest].lonlat[0];
color = stopObj[name+':'+dirTag][userOrDest].color;
oppositeColor = stopObj[name+':'+dirTag][userOrDest].oppositeColor;
stopLongName = stopObj[name+':'+dirTag][userOrDest].stopName; // TODO: use or delete
// self.lastStopObjArray.push({ lat: lat, lon: lon, minutes: minutes, route: name, userOrDest: userOrDest, color: color, oppositeColor: oppositeColor });
routesCovered[name+':'+dirTag] = routesCovered[name+':'+dirTag] || [];
routesCovered[name+':'+dirTag].push({ dirTitle: directionTitle, lat: lat, lon: lon, minutes: minutes, route: name, userOrDest: userOrDest, color: color, oppositeColor: oppositeColor });
}
}
} else if(child.name === 'predictions' && child.attr.dirTitleBecauseNoPredictions){
// self.lastStopObjArray.push({ lat: lat, lon: lon, minutes: '?', route: name, userOrDest: userOrDest, color: color, oppositeColor: oppositeColor });
Expand Down Expand Up @@ -158,9 +161,17 @@ lm.App.prototype.getStopPredictions = function(stopObj){
lm.config.direction = {};
for(var routeAndDirTag in routesCovered){
if(routesCovered[routeAndDirTag].length === 2){
self.lastStopObjArray.push(routesCovered[routeAndDirTag][0]);
self.lastStopObjArray.push(routesCovered[routeAndDirTag][1]);
console.log(routesCovered[routeAndDirTag]);
console.log(routesCovered[routeAndDirTag].length);
var temp = routesCovered[routeAndDirTag][0];
temp.routeAndDirTag = routeAndDirTag;
self.lastStopObjArray.push(temp);
temp = routesCovered[routeAndDirTag][1];
temp.routeAndDirTag = routeAndDirTag;
self.lastStopObjArray.push(temp);
lm.config.direction[routeAndDirTag] = true;
} else {
delete routesCovered[routeAndDirTag];
}
}
console.log('lm config',lm.config.direction);
Expand Down Expand Up @@ -321,12 +332,22 @@ lm.App.prototype.addThings = function(type, enableTransitions){

timeleft.text(function(d){
if(d.userOrDest === 'user'){
if(d.minutes === '?'){
return '???';
} else {
return d.minutes+' min';
}
} else {
return 'end';
}
});

timeleft.style('fill',function(d){
if(d.minutes === '?'){
return 'red';
}
});

svg.append('text')
.attr('x',3)
.attr('y',10)
Expand Down
35 changes: 17 additions & 18 deletions client/src/Map.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,22 +172,21 @@ lm.Map.prototype.getRouteObjFromServer = function(routeObj){
// Routify takes an array of map objects and renders them.
lm.Map.prototype.routify = function(err, res){
if(err) throw err;
var stopData,
allRoutes,
var stopArr, // all stops with the routeAndDirTag
coord,
stopRead,
allRoutes = {},
endPairs = {}, // obj of route objs containing dest/user lat and lon
self = this,
endpointArray = lm.app.lastStopObjArray; // TODO: !IMPORTANT - verify that this only stores for routes with BOTH user and dest.
endpointArray = lm.app.lastStopObjArray; // Only routeStops we have predictions for

for(var j = 0; j<endpointArray.length; j++){
endPairs[endpointArray[j].route] = endPairs[endpointArray[j].route] || {};
endPairs[endpointArray[j].route][endpointArray[j].userOrDest] = endpointArray[j];
endPairs[endpointArray[j].routeAndDirTag] = endPairs[endpointArray[j].routeAndDirTag] || {};
endPairs[endpointArray[j].routeAndDirTag][endpointArray[j].userOrDest] = endpointArray[j];
}

try {
stopData = JSON.parse(res.responseText);
allRoutes = {};
stopArr = JSON.parse(res.responseText);
this.routesNotRendered = false;
} catch(error) {
console.error(error);
Expand All @@ -204,30 +203,30 @@ lm.Map.prototype.routify = function(err, res){



for(var i = 0; i<stopData.length; i++){
if(!allRoutes[stopData[i].routename]){
for(var i = 0; i<stopArr.length; i++){
if(!allRoutes[stopArr[i].routeAndDirTag]){
stopRead = false;
allRoutes[stopData[i].routename] = {stops:[],color:stopData[i].color};
allRoutes[stopArr[i].routeAndDirTag] = {stops:[],color:stopArr[i].color};
}
if(!stopRead){
coord = new google.maps.LatLng(stopData[i].lonlat[1],stopData[i].lonlat[0]);
allRoutes[stopData[i].routename].stops.push(coord);
coord = new google.maps.LatLng(stopArr[i].lonlat[1],stopArr[i].lonlat[0]);
allRoutes[stopArr[i].routeAndDirTag].stops.push(coord);
} else {
// console.log('break ',stopData[i].lonlat[1], endPairs[stopData[i].routename].dest.lat, stopData[i].lonlat[0], endPairs[stopData[i].routename].dest.lon);
// console.log('break ',stopArr[i].lonlat[1], endPairs[stopArr[i].routeAndDirTag].dest.lat, stopArr[i].lonlat[0], endPairs[stopArr[i].routeAndDirTag].dest.lon);
}
if(stopData[i].lonlat[0] === endPairs[stopData[i].routename].dest.lon &&
stopData[i].lonlat[1] === endPairs[stopData[i].routename].dest.lat
if(stopArr[i].lonlat[0] === endPairs[stopArr[i].routeAndDirTag].dest.lon &&
stopArr[i].lonlat[1] === endPairs[stopArr[i].routeAndDirTag].dest.lat
){
stopRead = true;
stopRead = true; // Less than ideal
}
}

for(var route in allRoutes){
for(var routeAndDirTag in allRoutes){
// console.log('route: ',route,allRoutes[route].stops);
// for(var s = 0; s<allRoutes[route].stops.length; s++){
// console.log(allRoutes[route].stops[s].pb);
// }
createPolyline(allRoutes[route].stops, allRoutes[route].color);
createPolyline(allRoutes[routeAndDirTag].stops, allRoutes[routeAndDirTag].color);
}

// var coord;
Expand Down
117 changes: 47 additions & 70 deletions server/routeCompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ var makeRouteObj = function(tag, title, color, oppositeColor){
};
};

// Handles a single <route> containing <stop>s and <direction>s lists of stops
var parseRoutes = function(dbInfo, child, index, array){
if(child.name === 'route'){
dbInfo.counter++;
Expand All @@ -80,6 +81,8 @@ var parseRoutes = function(dbInfo, child, index, array){
}
};

// 8X stops are being stored in the wrong order!!!!

var parseRouteStops = function(dbInfo, routeObj, grandChild, index, array){
if(grandChild.name === 'stop' && grandChild.attr.lon && grandChild.attr.lat){ // some lon/lat are undefined from nextmuni
routeObj.stops.push({
Expand All @@ -88,13 +91,14 @@ var parseRouteStops = function(dbInfo, routeObj, grandChild, index, array){
lonlat: [Number(grandChild.attr.lon), Number(grandChild.attr.lat)] // lonlat order required for mongo 2d index
});
} else if(grandChild.name === 'direction'){
routeObj[grandChild.attr.tag] = [];
routeObj[grandChild.attr.tag] = []; // Sets the dirTag as a key on the routeObj
routeObj.directionPairs[grandChild.attr.tag] = grandChild.attr.title; // Save direction tag and readable title
var collectStopTagsNow = collectStopTags.bind(undefined, routeObj, grandChild.attr.tag);
grandChild.eachChild(collectStopTagsNow);
}
};

// Working correctly
var collectStopTags = function(routeObj, direction, stop, index){
routeObj[direction].push(stop.attr.tag);
};
Expand All @@ -108,11 +112,15 @@ var mongoReformat = function(routeObj){
mongoRouteObj.oppositeColor = routeObj.oppositeColor;
mongoRouteObj.directionPairs = routeObj.directionPairs;
for(var dirTag in routeObj.directionPairs){
console.log(dirTag);
mongoRouteObj[dirTag] = [];
for(var i = 0; i<routeObj[dirTag].length; i++){
for(var j = 0; j<routeObj.stops.length; j++){
if(routeObj.stops[j].stopTag === routeObj[dirTag][i]){
mongoRouteObj[dirTag].push(routeObj.stops[j]);
// if(dirTag === '8X'){
// console.log(mongoRoute);
// }
}
}
}
Expand Down Expand Up @@ -141,67 +149,16 @@ exports.listAllRoutes = function(cb, originalres){
});
};

var globalStopObj = {
requestDirections: []
}; // TODO NO GLOBAL!!!
exports.findStopsOnRoutes = function(request, response){

var db = connect('routesdb','busstops2');

var getRouteStopsFromDB = function(db, routeAndDirTagArray, direction){
console.log('stoparr: ',stopArray);
console.log('direction: ',direction);
db.busstops2.find({routeAndDirTag: {$in: routeAndDirTagArray}},{_id:0}).toArray(function(err,res){
db.busstops2.find({routeAndDirTag: {$in: Object.keys(request)}},{_id:0}).toArray(function(err,res){
if(err) console.log("This is an error: ",err);
globalStopObj.save(res);
response.end(JSON.stringify(res));
});
};

// Doesn't send back directions. Is this important?

// Can now query for route+dirTag
exports.findStopsOnRoutes = function(request, response){
console.log('thereq',request);
globalStopObj.dbInfo = connect('routesdb','busstops2');
globalStopObj.counter = 0;
globalStopObj.res = response;
globalStopObj.toSendBack = [];
globalStopObj.lastDir = '';

// No need to use the counter anymore, since we query both IB and OB at once
globalStopObj.trigger = function(){
if(this.counter > 0){
this.counter--;
this.lastDir = Object.keys(this.requestDirections[this.counter])[0];
console.log('reqdircount',this.requestDirections[this.counter]);

// TODO second arg should be routeAndDirTag
getRouteStopsFromDB(this.dbInfo, this.requestDirections[this.counter][this.lastDir], this.lastDir);
} else {
// console.log('tosendback',this.toSendBack);
var sendback = this.toSendBack;
this.counter = 0;
this.toSendBack = [];
this.lastDir = '';
this.requestDirections = [];
this.res.end(JSON.stringify(sendback));
}
};
globalStopObj.save = function(results){
this.toSendBack = this.toSendBack.concat(results);
this.trigger();
};
var temp = {};
for(var dir in request){
temp[dir] = Object.keys(request[dir]);
console.log('temp',temp);
globalStopObj.requestDirections.push(temp);
temp = {};
}
globalStopObj.counter = globalStopObj.requestDirections.length;
globalStopObj.trigger.bind(globalStopObj);
globalStopObj.save.bind(globalStopObj);
globalStopObj.trigger();

};

// dual function to save user coord early when possible
exports.findRoutesNear = findRoutesNear = function(coordinates, cb, routeArray){
var dbInfo = connect('routesdb','busstops2');
Expand All @@ -219,14 +176,15 @@ exports.findRoutesNear = findRoutesNear = function(coordinates, cb, routeArray){
},
spherical: true,
num: num,
maxDistance: 400,
maxDistance: 800, // meters
query: query
},
function(err, res){
console.log('Total nearby routes: ', res.results.length);
var routesObj = {};
var resultsArr = res.results;
for(var i = 0; i<resultsArr.length; i++){
console.log(resultsArr[i].obj.routeAndDirTag);
// Since geo results are sorted by proximity, only save the closest route+dirTag object
if(!routesObj[resultsArr[i].obj.routename+':'+resultsArr[i].obj.dirTag]){
routesObj[resultsArr[i].obj.routename+':'+resultsArr[i].obj.dirTag] = resultsArr[i].obj;
Expand Down Expand Up @@ -402,7 +360,7 @@ var createStopsCollection = function(){
routeTicker: 0,
allRoutes: [],
counter: 0,
trigger: function(){
triggerNewRoute: function(){
if(this.routeTicker < this.allRoutes.length){
routeFlattener(this.allRoutes[this.routeTicker],this);
this.routeTicker++;
Expand All @@ -418,10 +376,33 @@ var createStopsCollection = function(){
globalMind.routesdb = mongoClient.db('routesdb'); // TODO: use connect function
globalMind.busroutes2 = globalMind.routesdb.collection('busroutes2'); // TODO: use connect function
globalMind.busstops2 = globalMind.routesdb.collection('busstops2');// TODO: use connect function
globalMind.syncronousInsert = function(){
var self = this;
if(this.tempCountTicker < this.tempCount){
globalMind.busstops2.insert(this.decompiled[this.tempCountTicker], function(err, res){
if(err) console.log("This is an error: ",err);
self.counter--;
if(self.counter === 0){ // triggers the 'else' in triggerNewRoute
self.triggerNewRoute();
} else {
self.tempCountTicker++;
console.log('tempcount: ', self.tempCountTicker);
self.syncronousInsert();
}
});
} else {
this.tempCount = 0;
this.decompiled = [];
this.tempCountTicker = 0;
this.triggerNewRoute();
}

};
globalMind.syncronousInsert.bind(globalMind);
globalMind.busroutes2.find({}).toArray(function(err,res){
if(!err){
globalMind.allRoutes = res;
globalMind.trigger();
globalMind.triggerNewRoute();
}
});
};
Expand All @@ -430,6 +411,7 @@ var createStopsCollection = function(){
var routeFlattener = function(aRoute, globalMind){
var decompiledRoute = [];
var tempStop = {};
// if(aRoute.routename === '8X') console.log('aRoute: ',aRoute);
for(var dirTag in aRoute.directionPairs){
for(var i = 0; i<aRoute[dirTag].length; i++){
tempStop = aRoute[dirTag][i];
Expand All @@ -445,15 +427,10 @@ var routeFlattener = function(aRoute, globalMind){
tempStop = {};
}
}
for(var j = 0; j<decompiledRoute.length; j++){
globalMind.busstops2.insert(decompiledRoute[j], function(err, res){
if(err) console.log("This is an error: ",err);
globalMind.counter--;
console.log(globalMind.counter);
if(globalMind.counter === 0){
globalMind.trigger();
}
});
}
// if(aRoute.routename === '8X') console.log('decomp: ',decompiledRoute);
globalMind.tempCount = decompiledRoute.length;
globalMind.decompiled = decompiledRoute;
globalMind.tempCountTicker = 0;
globalMind.syncronousInsert();
};

0 comments on commit 5941364

Please sign in to comment.