How to use the @turf/line-slice.default function in @turf/line-slice

To help you get started, we’ve selected a few @turf/line-slice examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github noncomputable / AgentMaps / src / routing.js View on Github external
}
	if (!intersection_indices.some(intersection_index => intersection_index === street_points.length - 1)) {
		intersection_indices.push(street_points.length - 1);
	}

	//Make a graph out of segments of the street between the start, intersections, and end of the street,
	//so that the nodes are the coordinates of the start, end, and intersection points, and the edges are
	//the segments between successive nodes. Each edge is associated with the geographic distance between its nodes.
	for (let i = 0; i <= intersection_indices.length - 2; i++) {
		let node_a = street_points[intersection_indices[i]],
		node_b = street_points[intersection_indices[i + 1]],
		a_string = encodeLatLng(node_a),
		b_string = encodeLatLng(node_b),
		start_coords = L.A.pointToCoordinateArray(node_a),
		end_coords = L.A.pointToCoordinateArray(node_b),
		segment = lineSlice(start_coords, end_coords, street.toGeoJSON()),
		distance = length(segment);
		graph.addLink(a_string, b_string, {
			distance: distance,
			place: { type: "street",
				id: street_id } 
		});
	}
}
github noncomputable / AgentMaps / src / agents.js View on Github external
Agent.setTravelOnSameStreet = function(start_lat_lng, goal_lat_lng, street_feature, street_id, speed) {
	//lineSlice, regardless of the specified starting point, will give a segment with the same coordinate order 
	//as the original lineString array. So, if the goal point comes earlier in the array (e.g. it's on the far left),
	//it'll end up being the first point in the path, instead of the last, and the agent will move to it directly,
	//ignoring the street points that should come before it. It would then travel along the street from the goal point 
	//to its original point (backwards).
	//To fix this, I'm reversing the order of the coordinates in the segment if the last point in the line is closer
	//to the agent's starting point than the first point on the line (implying the last point in the array is the starting 
	//point, not the goal). 
	
	let start_coords = L.A.pointToCoordinateArray(start_lat_lng),
	goal_coords = L.A.pointToCoordinateArray(goal_lat_lng),
	street_path_unordered = L.A.reversedCoordinates(lineSlice(start_coords, goal_coords, street_feature).geometry.coordinates);
	let start_to_path_beginning = start_lat_lng.distanceTo(L.latLng(street_path_unordered[0])),
	start_to_path_end = start_lat_lng.distanceTo(L.latLng(street_path_unordered[street_path_unordered.length - 1]));
	let street_path = start_to_path_beginning < start_to_path_end ?	street_path_unordered :	street_path_unordered.reverse();
	let street_path_lat_lngs = street_path.map(coords => { 
		let lat_lng = L.latLng(coords);
		lat_lng.new_place = { type: "street", id: street_id },
		lat_lng.speed = speed;

		return lat_lng;
	});

	let first_lat = street_path_lat_lngs[0].lat,
	first_lng = street_path_lat_lngs[0].lng; 

	//Exclude the last point if it's the same as the second to last point of this proposed segment,
	//and the second of it's the same as the first.

@turf/line-slice

turf line-slice module

MIT
Latest version published 3 months ago

Package Health Score

94 / 100
Full package analysis

Popular @turf/line-slice functions