Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
flibbertigibbet committed Nov 18, 2016
2 parents a4ec201 + 39c23e3 commit 7886e31
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 42 deletions.
2 changes: 1 addition & 1 deletion python/cac_tripplanner/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
<script src="{% static 'scripts/vendor/typeahead.bundle.js' %}"></script>
<script src="{% static 'scripts/vendor/leaflet.awesome-markers.js' %}"></script>
<script src="{% static 'scripts/vendor/turf-helpers.js' %}"></script>
<script src="{% static 'scripts/vendor/turf-nearest.js' %}"></script>
<script src="{% static 'scripts/vendor/turf-point-on-line.js' %}"></script>
<script src="{% static 'scripts/vendor/moment.js' %}"></script>
<script src="{% static 'scripts/vendor/moment-duration-format.js' %}"></script>
<script src="{% static 'scripts/vendor/bootstrap-datetimepicker.min.js' %}"></script>
Expand Down
46 changes: 17 additions & 29 deletions src/app/scripts/cac/map/cac-map-control.js
Original file line number Diff line number Diff line change
Expand Up @@ -581,44 +581,32 @@ CAC.Map.Control = (function ($, Handlebars, cartodb, L, turf, _) {
itinerary.geojson.bringToFront();
}

// Figure out where in the sequence of waypoints the click to create a new one falls.
// Returns the index in the waypoint array at which to insert the new waypoint.
function getNewWaypointIndex(itinerary, startDragPoint) {
var waypoints = itinerary.waypoints;

if (!waypoints || !waypoints.length) {
return 0;
}

var originPoint = turf.point([itinerary.from.lon, itinerary.from.lat], {index: -1});
var destPoint = turf.point([itinerary.to.lon, itinerary.to.lat], {index: waypoints.length});
// Combine the whole itinerary into a single linestring
var combinedRoute = turf.lineString(_.flatMap(itinerary.geojson.toGeoJSON().features,
'geometry.coordinates'));
// Figure out how far the new point is along that linestring. Index is fine since we
// just need to do comparisons.
var newPointDist = turf.pointOnLine(combinedRoute, startDragPoint).properties.index;

var allFeatures = _.concat([originPoint], waypoints, [destPoint]);

var turfPoint = turf.point(startDragPoint);
var nearest = turf.nearest(turfPoint, turf.featureCollection(allFeatures));

var nearestIndex = nearest.properties.index;

// drop the nearest point to repeat search, in order to find next nearest
var remainingFeatures = _.concat(_.slice(allFeatures, 0, nearestIndex),
_.slice(allFeatures, nearestIndex + 1));

var nextNearest = turf.nearest(turfPoint, turf.featureCollection(remainingFeatures));

var nextNearestIndex = nextNearest.properties.index;

// determine the sequence ordering of the two nearest points, so the new point can
// be added between them
var smallerIndex = Math.min(nearestIndex, nextNearestIndex);
var largerIndex = Math.max(nearestIndex, nextNearestIndex);
var newIndex = smallerIndex + 1;

// If the nearest two points aren't in sequence and the larger indexed one is closer,
// put the new point right before that one instead of right after the 2nd nearest
if (largerIndex - smallerIndex !== 1 && smallerIndex !== nearestIndex) {
newIndex = largerIndex - 1;
// Find the first waypoint that's after the new point
var insertBefore = _.find(waypoints, function (pt) {
return turf.pointOnLine(combinedRoute, pt).properties.index > newPointDist;
});
// And return its index or, if no later waypoint was found, add the new one to the end
if (insertBefore) {
return insertBefore.properties.index;
} else {
return waypoints.length;
}

return newIndex;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/app/scripts/cac/routing/cac-routing-itinerary.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ CAC.Routing.Itinerary = (function ($, cartodb, L, _, moment, Geocoder, Utils) {
// merge it back into a single step
var lastStep = _.clone(_.last(thisLeg.steps));
var nextStep = _.first(nextLeg.steps);
if (nextStep.relativeDirection === 'CONTINUE' &&
if (nextStep && nextStep.relativeDirection === 'CONTINUE' &&
lastStep.streetName === nextStep.streetName) {
lastStep.distance += nextStep.distance;
lastStep.elevation = lastStep.elevation.concat(nextStep.elevation);
Expand Down
21 changes: 11 additions & 10 deletions src/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,31 +73,32 @@ gulp.task('collectstatic', function () {
});

// turf module needs to be run through browserify to pack it with its dependencies
var turfDistanceRoot = './node_modules/@turf/nearest/node_modules/@turf/distance';
var turfRoot = './node_modules/@turf/point-on-line';

var buildTurfHelpers = function() {
return browserify(turfDistanceRoot + '/node_modules/@turf/helpers', {
return browserify(turfRoot + '/node_modules/@turf/helpers', {
standalone: 'turf',
expose: ['helpers']
})
.require(turfDistanceRoot + '/node_modules/@turf/helpers',
.require(turfRoot + '/node_modules/@turf/helpers',
{expose: 'turf-helpers'})
.bundle()
.pipe(vinylSourceStream('turf-helpers.js'));
};

var buildTurfPointOnLine = function() {
return browserify('./node_modules/@turf/nearest', {
standalone: 'turf.nearest',
exclude: [turfDistanceRoot + '/node_modules/@turf/helpers']
return browserify('./node_modules/@turf/point-on-line', {
standalone: 'turf.pointOnLine',
exclude: [turfRoot + '/node_modules/@turf/helpers']
})
.transform(aliasify, {aliases: {
'turf-helpers': turfDistanceRoot + '/node_modules/@turf/helpers',
'turf-invariant': turfDistanceRoot + '/node_modules/@turf/invariant',
'turf-distance': turfDistanceRoot
'turf-helpers': turfRoot + '/node_modules/@turf/helpers',
'turf-distance': turfRoot + '/node_modules/@turf/distance',
'turf-bearing': turfRoot + '/node_modules/@turf/bearing',
'turf-destination': turfRoot + '/node_modules/@turf/destination'
}})
.bundle()
.pipe(vinylSourceStream('turf-nearest.js'));
.pipe(vinylSourceStream('turf-point-on-line.js'));
};

// combine streams from turf and the other vendor dependencies
Expand Down
2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"node": ">=0.12.0"
},
"dependencies": {
"@turf/nearest": "~3.5.2"
"@turf/point-on-line": "~3.5.3"
},
"devDependencies": {
"aliasify": "^2.0.0",
Expand Down

0 comments on commit 7886e31

Please sign in to comment.