Skip to content

Commit

Permalink
Route with 2 segments when range is used
Browse files Browse the repository at this point in the history
This updates the route planner code to add a second post-range segment when a range is applied and is less than the route distance.

This also adds styling for the post-range segment.
  • Loading branch information
michaelpnelson committed Mar 27, 2023
1 parent 713af00 commit a35daf1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 60 deletions.
87 changes: 27 additions & 60 deletions src/smk/api/route-planner.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,24 @@ include.module( 'api.route-planner-js', [ 'jquery', 'util' ], function () {

data.segments.push( turf.lineString( data.route.slice( prop.index, data.partitions[ pi ].index + 1 ), prop ) )
}
} else if (option.rangeKm > 0 && data.distance && option.rangeKm < data.distance) {
data.segments = [];
var distanceKm = 0;
var routeIndex = 0;

// Capture the route segment where the range limit is reached
for (var i = 0; i < data.route.length + 1; i += 1) {
distanceKm += turf.distance(data.route[i], data.route[i+1], {units: "kilometers"});
if (distanceKm >= option.rangeKm) {
data.rangeLimit = data.route[i];
routeIndex = i;
break;
}
}

// Add two segments - one before the range limit is reached, one after
data.segments.push(turf.lineString(data.route.slice(0, routeIndex), { index: 0 }));
data.segments.push(turf.lineString(data.route.slice(routeIndex), { index: 1, "@layer": "@segments-after-range-limit" }));
} else {
data.segments = [ turf.lineString( data.route, { index: 0 } ) ]
}
Expand All @@ -139,10 +157,15 @@ include.module( 'api.route-planner-js', [ 'jquery', 'util' ], function () {
}
} )

// segments that start/end on a node without a direction are a problem
var problems = routeAttrs.filter( function ( ra ) {
return Object.keys( ra.segs ).length > 1 && ra.dirs.length == 0
} )
var problems = [];

// When there's a range and we've created two segments, this code results in an increase in the number of directions
if (option.rangeKm === null) {
// segments that start/end on a node without a direction are a problem
problems = routeAttrs.filter( function ( ra ) {
return Object.keys( ra.segs ).length > 1 && ra.dirs.length == 0
} )
}

if ( problems.length > 0 ) {
problems.forEach( function ( p ) {
Expand All @@ -157,18 +180,6 @@ include.module( 'api.route-planner-js', [ 'jquery', 'util' ], function () {
.map( function ( ra ) { return ra.dirs } )
.filter( function ( d ) { return !!d } )
.reduce( function ( acc, v ) { return acc.concat( v ) }, [] )

// debugger
}
if (option.rangeKm > 0 && data.distance && option.rangeKm < data.distance) {
var distanceKm = 0;
for (var i = 0; i < data.route.length + 1; i += 1) {
distanceKm += turf.distance(data.route[i], data.route[i+1], {units: "kilometers"});
if (distanceKm >= option.rangeKm) {
data.rangeLimit = data.route[i];
break;
}
}
}
}

Expand All @@ -178,50 +189,6 @@ include.module( 'api.route-planner-js', [ 'jquery', 'util' ], function () {
} )

return result

// return result.catch( function () {
// return {
// distance: '10',
// timeText: '10 mins',
// route: points.map( function ( p ) { return [ p.longitude, p.latitude ] } )
// .reduce( function ( accum, v ) {
// if ( accum.length == 0 ) {
// accum.push( v )
// return accum
// }

// var prev = accum[ accum.length - 1 ]

// accum.push( interpolate( prev, v, 0.2 ) )
// accum.push( interpolate( prev, v, 0.4 ) )
// accum.push( interpolate( prev, v, 0.6 ) )
// accum.push( interpolate( prev, v, 0.8 ) )
// accum.push( v )

// return accum
// }, [] ),
// directions: points
// .map( function ( p ) {
// return { instruction: 'waypoint: ' + p.longitude + ', ' + p.latitude, point: [ p.longitude, p.latitude ] }
// } )
// .reduce( function ( accum, v ) {
// if ( accum.length == 0 ) {
// accum.push( v )
// return accum
// }

// var prev = accum[ accum.length - 1 ]

// accum.push( { instruction: 'turn left for 1km (1:00)', point: interpolate( prev.point, v.point, 0.2 ) } )
// // accum.push( { instruction: 'go straight for 2km (2:00)', point: interpolate( prev.point, v.point, 0.4 ) } )
// accum.push( { instruction: 'turn right for 3km (3:00)', point: interpolate( prev.point, v.point, 0.6 ) } )
// // accum.push( { instruction: 'go backwards for 4km (4:00)', point: interpolate( prev.point, v.point, 0.8 ) } )
// accum.push( v )

// return accum
// }, [] )
// }
// } )
}

function interpolate( p1, p2, t ) {
Expand Down
12 changes: 12 additions & 0 deletions src/smk/tool/directions/config/tool-directions-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ include.module( 'tool-directions-config', [
legend: {
line: true
}
},
{
id: "@segments-after-range-limit",
title: "Segments After Range Limit",
style: {
strokeColor: "lightblue",
strokeWidth: 4,
strokeOpacity: 0.8
},
legend: {
line: true
}
}
],

Expand Down

0 comments on commit a35daf1

Please sign in to comment.