Skip to content

Commit

Permalink
fix(editor): handle travel time calc for patterns without shapes
Browse files Browse the repository at this point in the history
fixes #107
  • Loading branch information
landonreed committed May 3, 2018
1 parent d5bb692 commit 7b0d970
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
30 changes: 20 additions & 10 deletions lib/editor/components/pattern/CalculateDefaultTimesForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React, {Component, PropTypes} from 'react'
import { Button, FormGroup, InputGroup, Form, FormControl, ControlLabel } from 'react-bootstrap'

import MinuteSecondInput from '../MinuteSecondInput'
import {straightLineDistancesBetweenStopAnchors} from '../../util/map'

const DEFAULT_SPEED = 20 // km/hr

Expand All @@ -19,23 +20,32 @@ export default class CalculateDefaultTimesForm extends Component {
}

_calculateDefaultTimes = () => {
const {activePattern, saveActiveEntity, updateActiveEntity} = this.props
const {activePattern, controlPoints, saveActiveEntity, updateActiveEntity} = this.props
const {dwellTime, speed} = this.state
const patternStops = [...activePattern.patternStops]
const convertedSpeed = speed * 1000 / 60 / 60 // km/hr -> m/s
const straightLineDistances = straightLineDistancesBetweenStopAnchors(controlPoints)
for (var i = 0; i < patternStops.length; i++) {
let interStopDistance = 0
patternStops[i].defaultDwellTime = dwellTime
if (i > 0) {
// Only set travel time for stops after the first stop (time to travel to
// first stop should be zero).
const interStopDistance = patternStops[i].shapeDistTraveled - patternStops[i - 1].shapeDistTraveled
patternStops[i].defaultTravelTime = Math.floor(interStopDistance / convertedSpeed)
console.log(interStopDistance, patternStops[i].defaultTravelTime)
} else {
// Set first stop travel time to zero (in case it was previously some
// other value).
patternStops[i].defaultTravelTime = 0
// Only update travel time for stops after the first stop because the
// time to travel to the first stop (from the first stop) should be zero.
if (activePattern.shape) {
// If there is a pattern shape, use the shape distance traveled values
interStopDistance = patternStops[i].shapeDistTraveled - patternStops[i - 1].shapeDistTraveled
} else {
// Otherwise, use straight line distances between stops (there are
// only n - 1 distances between n stops, hence the i - 1 for the index).
interStopDistance = straightLineDistances[i - 1]
}
}
// Never permit the calculation of negative stop distance values (if that
// were somehow to occur).
patternStops[i].defaultTravelTime = interStopDistance > 0
? Math.floor(interStopDistance / convertedSpeed)
: 0
// console.log(interStopDistance, patternStops[i].defaultTravelTime)
}
updateActiveEntity(activePattern, 'trippattern', {patternStops})
saveActiveEntity('trippattern')
Expand Down
18 changes: 18 additions & 0 deletions lib/editor/util/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -754,3 +754,21 @@ export async function street (from: LatLng, to: LatLng): Promise<any | null> {
return json
}
}

/**
* Generate straight line inter-stop distances (in meters) from control points.
* @param {[type]} controlPoints [description]
* @return {[type]} [description]
*/
export function straightLineDistancesBetweenStopAnchors (
controlPoints: Array<ControlPoint>
): Array<number> {
// Ensure that only stop control points are considered.
const stopControlPoints = controlPoints.filter(cp => cp.pointType === POINT_TYPE.STOP)
const interStopDistances = []
for (var i = 1; i < stopControlPoints.length; i++) {
const dist = distance(controlPoints[i].point, controlPoints[i - 1].point, {units: 'meters'})
interStopDistances.push(dist)
}
return interStopDistances
}

0 comments on commit 7b0d970

Please sign in to comment.