Skip to content

Commit

Permalink
Merge pull request #110 from sarahz916/sarahdev
Browse files Browse the repository at this point in the history
Fixed generated routes with species
  • Loading branch information
sarahz916 authored Aug 17, 2020
2 parents bdfcd16 + 470a0a7 commit cb57cd6
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 91 deletions.
56 changes: 0 additions & 56 deletions src/main/java/com/google/sps/servlets/GenRouteServlet.java

This file was deleted.

34 changes: 26 additions & 8 deletions src/main/java/com/google/sps/servlets/RouteStoreServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,19 @@ private ArrayList<StoredRoute> getUnorderedStoredRoutes(){
ArrayList<StoredRoute> routes = new ArrayList<>();
for (Entity entity : results.asIterable()) {
long id = entity.getKey().getId();
Text textText = (Text) entity.getProperty("text");
Text waypointsJsonText = (Text) entity.getProperty("actual-route");
String text = textText.getValue();
String waypointsJson = waypointsJsonText.getValue();
Object textObject = entity.getProperty("text");
Object waypointsJsonObject = entity.getProperty("actual-route");
String text;
String waypointsJson;
try{
Text textText = (Text) textObject;
Text waypointsJsonText = (Text) waypointsJsonObject;
text = textText.getValue();
waypointsJson = waypointsJsonText.getValue();
} catch(ClassCastException e){ //some old routes in datastore are stored as strings
text = (String) textObject;
waypointsJson = (String) waypointsJsonObject;
}
if(routes.size() <= NUM_RESULTS){ //only want to return NUM_RESULTS routes
if (waypointsJson != null){
StoredRoute route = new StoredRoute(id, text, waypointsJson);
Expand All @@ -101,10 +110,19 @@ private ArrayList<StoredRoute> getOrderedStoredRoutes(GeoPt midpoint){
ArrayList<StoredRoute> routes = new ArrayList<>();
for (Entity entity : results.asIterable()) {
long id = entity.getKey().getId();
Text textText = (Text) entity.getProperty("text");
Text waypointsJsonText = (Text) entity.getProperty("actual-route");
String text = textText.getValue();
String waypointsJson = waypointsJsonText.getValue();
Object textObject = entity.getProperty("text");
Object waypointsJsonObject = entity.getProperty("actual-route");
String text;
String waypointsJson;
try{
Text textText = (Text) textObject;
Text waypointsJsonText = (Text) waypointsJsonObject;
text = textText.getValue();
waypointsJson = waypointsJsonText.getValue();
} catch(ClassCastException e){ //some old routes in datastore are stored as strings
text = (String) textObject;
waypointsJson = (String) waypointsJsonObject;
}
GeoPt center;
try{
center = (GeoPt) entity.getProperty("center-of-mass");
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/google/sps/servlets/StartEndServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public void doPost(HttpServletRequest request, HttpServletResponse response) thr
// Check if this is a loop or one-way route and get the end accordingly.
if (request.getParameterMap().containsKey("endloc-input")) {
end = request.getParameter("endloc-input");
sessionDataStore.storeProperty("StartEnd", "radius", DEFAULT_COORDINATE_STRING);
} else {
end = start;
Double radiusInMiles = getRadius(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ public class WaypointQueryServlet extends HttpServlet {
private static final String NOUN_SINGULAR_OR_MASS = "NN";
private static final String NOUN_PLURAL = "NNS";
private static final String PRONOUN = "PRP";
private static final String START = "start";
private static final String END = "end";
private static final String MIDPOINT = "midpoint";
public static Autocorrect corrector;

// @Override
Expand Down Expand Up @@ -101,9 +104,9 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String input = request.getParameter("text-input");
SessionDataStore sessionDataStore = new SessionDataStore(request);
Coordinate midpoint = sessionDataStore.getPoint("midpoint");
Coordinate start = sessionDataStore.getPoint("start");
Coordinate end = sessionDataStore.getPoint("end");
Coordinate midpoint = sessionDataStore.getPoint(MIDPOINT);
Coordinate start = sessionDataStore.getPoint(START);
Coordinate end = sessionDataStore.getPoint(END);
Double loopRadius = sessionDataStore.getLoopRadius();
ArrayList<List<Coordinate>> waypoints = new ArrayList<List<Coordinate>>();
int statusCode = HttpServletResponse.SC_OK;
Expand Down
43 changes: 21 additions & 22 deletions src/main/webapp/gen-route-script-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
//BUG: what to do if route text is the same?
//Have it work without start and end location?

const DIFF = 0.002;

window.onload = async function setup() {
event.preventDefault();
let startAddr = await getStartAddr();
Expand Down Expand Up @@ -133,7 +135,7 @@ async function createMapWithWaypoints(route, mapID, legendID, urlID) {
function calcRoute(directionsService, directionsRenderer, start, end, waypoints, legendID) {
var waypointsWithLabels = waypoints;
let waypointsData = [];
waypoints.forEach((pts, label) => pts.forEach(pt => waypointsData.push({ location: pt })));
waypoints.forEach(pt => waypointsData.push({ location: pt.latlng }));
let request = {
origin: start,
destination: end,
Expand Down Expand Up @@ -174,9 +176,10 @@ async function createWaypointLegend(route, waypointsWithLabels, legendID) {
let pt = route.legs[i].end_location;
totalDistance += route.legs[i].distance.value;
totalDuration += route.legs[i].duration.value;
let label = getLabelFromLatLng(pt, waypointsWithLabels);
let label = getInfoFromLatLng(pt, waypointsWithLabels, 'label');
let species = getInfoFromLatLng(pt, waypointsWithLabels, 'species');
marker = String.fromCharCode(marker.charCodeAt(0) + 1);
addNewLegendElem(legend, `${marker}: ${label}`);
addNewLegendElem(legend, `${marker}: ${label} (${species})`);
}
let end = route.legs[route.legs.length - 1].end_location;
totalDistance += route.legs[route.legs.length - 1].distance.value;
Expand Down Expand Up @@ -230,18 +233,19 @@ function convertHoursToMinutes(time) {
* Given a Google Maps LatLng object and JSON containing waypoint coords with labels,
* return the label matching the given LatLng object.
*/
function getLabelFromLatLng(pt, waypointsWithLabels) {
for (let [label, waypoints] of waypointsWithLabels.entries()) {
function getInfoFromLatLng(pt, waypointsWithLabels, infoRequested) {
//for (let [label, waypoints] of waypointsWithLabels.entries()) {
for (let waypoint of waypointsWithLabels) {
// Calculate the difference between the lat/long of the points and
// check if its within a certain range.
for (let waypoint of waypoints) {
let latDiff = Math.abs(waypoint.lat() - pt.lat());
let lngDiff = Math.abs(waypoint.lng() - pt.lng());
const range = 0.001;
if (latDiff < range && lngDiff < range) {
return label;
}
//for (let waypoint of waypoints) {
let latDiff = Math.abs(waypoint.latlng.lat() - pt.lat());
let lngDiff = Math.abs(waypoint.latlng.lng() - pt.lng());
const range = 0.001;
if (latDiff < DIFF && lngDiff < DIFF) {
return waypoint[`${infoRequested}`];
}
//}
}
return '';
}
Expand Down Expand Up @@ -273,8 +277,8 @@ function initMap(center, id) {
function generateURL(start, end, waypoints, urlID){
let globalURL = 'https://www.google.com/maps/dir/?api=1';
globalURL = globalURL + '&origin=' + start + '&destination=' + end;
globalURL += '&waypoints='
waypoints.forEach((pts, label) => pts.forEach(pt => globalURL += pt + '|'));
globalURL += '&waypoints=';
waypoints.forEach(pt => globalURL += pt.latlng + '|');
globalURL = globalURL + '&travelmode=walking';
const URLcontainer = document.getElementById(urlID);
globalURL = globalURL.split(" ").join("") //need to get rid of white space for link to work
Expand All @@ -297,17 +301,12 @@ async function writeToAssociatedText(){
* Convert waypoints in JSON form returned by servlet to Google Maps LatLng objects.
*/
function convertWaypointstoLatLng(waypoints) {
let latlngWaypoints = new Map();
let latLngWaypoints = [];
for (let pt of waypoints) {
let waypoint = new google.maps.LatLng(pt.y, pt.x);
// If the given label doesn't exist in the map, add it.
if (!latlngWaypoints.has(pt.label)) {
latlngWaypoints.set(pt.label, [waypoint]);
} else {
latlngWaypoints.get(pt.label).push(waypoint);
}
latLngWaypoints.push({ latlng: waypoint, label: pt.label, species: pt.species });
}
return latlngWaypoints;
return latLngWaypoints;
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/main/webapp/route-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ async function createMapWithWaypoints() {
let waypoints = convertWaypointstoLatLng(res);
let start = await getStartCoord();
let end = await getEndCoord();

let map = initMap(start, 'route-map');
let directionsService = new google.maps.DirectionsService();
let directionsRenderer = new google.maps.DirectionsRenderer({
Expand Down Expand Up @@ -521,7 +520,7 @@ function generateURL(start, end, waypoints){
let globalURL = 'https://www.google.com/maps/dir/?api=1';
globalURL = globalURL + '&origin=' + start + '&destination=' + end;
globalURL += '&waypoints=';
waypoints.forEach(pt => globalURL += pt.latlng + '|')
waypoints.forEach(pt => globalURL += pt.latlng + '|');
globalURL = globalURL + '&travelmode=walking';
const URLcontainer = document.getElementById('globalURL');
globalURL = globalURL.split(" ").join("") //need to get rid of white space for link to work
Expand Down

0 comments on commit cb57cd6

Please sign in to comment.