-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…ahmed-alkheerow/issue69
- Loading branch information
Showing
19 changed files
with
4,146 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"projects": { | ||
"default": "mapsproject-228715" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
firebase-debug.log* | ||
firebase-debug.*.log* | ||
|
||
# Firebase cache | ||
.firebase/ | ||
|
||
# Firebase config | ||
|
||
# Uncomment this if you'd like others to create their own Firebase project. | ||
# For a team working on the same Firebase project(s), it is recommended to leave | ||
# it commented so all members can deploy to the same project(s) in .firebaserc. | ||
# .firebaserc | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
*.pid.lock | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# nyc test coverage | ||
.nyc_output | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# Bower dependency directory (https://bower.io/) | ||
bower_components | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (http://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directories | ||
node_modules/ | ||
|
||
# Optional npm cache directory | ||
.npm | ||
|
||
# Optional eslint cache | ||
.eslintcache | ||
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
# Output of 'npm pack' | ||
*.tgz | ||
|
||
# Yarn Integrity file | ||
.yarn-integrity | ||
|
||
# dotenv environment variables file | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
const functions = require('firebase-functions'); | ||
const admin = require('firebase-admin'); | ||
const cors = require('cors')({ origin: true }); | ||
const firebaseConfig = { | ||
apiKey: 'AIzaSyC5oUqe1Y0-ZGy_tkTG7jwWx98-LGiVMRs', | ||
authDomain: 'mapsproject-228715.firebaseapp.com', | ||
databaseURL: 'https://mapsproject-228715.firebaseio.com', | ||
projectId: 'mapsproject-228715', | ||
storageBucket: 'mapsproject-228715.appspot.com', | ||
messagingSenderId: '1052677426743', | ||
appId: '1:1052677426743:web:9625bece8d88ca9474f9b2', | ||
measurementId: 'G-BSTYSQ6D7Q', | ||
}; | ||
|
||
admin.initializeApp(firebaseConfig); | ||
const turf = require('@turf/turf'); | ||
|
||
// // Create and Deploy Your First Cloud Functions | ||
// // https://firebase.google.com/docs/functions/write-firebase-functions | ||
// | ||
exports.getRoutes = functions.https.onRequest(async (request, response) => { | ||
cors(request, response, async () => { | ||
// Only POST | ||
if (request.method === 'POST') { | ||
const { origin, destination } = request.body; | ||
const routes = await getRoutes(origin, destination); | ||
response.send(routes); | ||
} else { | ||
response.send('Unknown Method'); | ||
} | ||
}); | ||
}); | ||
|
||
async function getRoutes(origin, destination) { | ||
const originP = turf.point(origin); | ||
const destP = turf.point(destination); | ||
|
||
const distanceThreshold = 0.5; //kilometers | ||
|
||
const options = { units: 'kilometers' }; | ||
|
||
const routes = []; | ||
const routesSnapshot = await admin.firestore().collection('routes').get(); | ||
routesSnapshot.forEach((doc) => { | ||
const route = { id: doc.id, ...doc.data() }; | ||
if (typeof route.path === 'string') route.path = JSON.parse(route.path); | ||
const path = route.path; | ||
const originDistance = turf.pointToLineDistance(originP, path, options); | ||
const destDistance = turf.pointToLineDistance(destP, path, options); | ||
const getInPoint = turf.nearestPointOnLine(path, originP, options); | ||
const getOutPoint = turf.nearestPointOnLine(path, destP, options); | ||
getInPoint.properties.type = 'start'; | ||
getOutPoint.properties.type = 'end'; | ||
|
||
if ( | ||
originDistance <= distanceThreshold && | ||
destDistance <= distanceThreshold | ||
) { | ||
routes.push({ | ||
name: route.name, | ||
availability: route.availability, | ||
id: route.id, | ||
path: turf.featureCollection([route.path, getInPoint, getOutPoint]), | ||
originDistance, | ||
destDistance, | ||
totalDistance: originDistance + destDistance, | ||
units: options.units, | ||
}); | ||
} | ||
}); | ||
|
||
for (const route of routes) { | ||
const { id } = route; | ||
const buses = await getBuses(id); | ||
route.buses = buses; | ||
} | ||
|
||
routes.sort((a, b) => a.totalDistance - b.totalDistance); | ||
routes[0].fastest = true; | ||
|
||
return routes; | ||
} | ||
|
||
async function getBuses(routeId) { | ||
const buses = []; | ||
const busesSnapshot = await admin | ||
.firestore() | ||
.collection('busses') | ||
.where('route_id', '==', routeId) | ||
.get(); | ||
busesSnapshot.forEach((doc) => { | ||
const bus = { id: doc.id, ...doc.data() }; | ||
buses.push(bus); | ||
}); | ||
|
||
return buses; | ||
} |
Oops, something went wrong.