How to use @turf/line-slice - 8 common examples

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 conveyal / analysis-ui / lib / components / report / reroute.js View on Github external
let nStopsRemoved = toStopIndex - fromStopIndex
  if (modification.fromStop && modification.toStop) nStopsRemoved-- // -1 because it's an exclusive interval on both sides, don't include from and to stops

  let nStopsAdded = stops.length

  // the endpoints are included, subtract them off where they overlap with existing stops
  if (modification.fromStop) nStopsAdded--
  if (modification.toStop) nStopsAdded--

  // NB using indices here so we get an object even if fromStop or toStop is null
  // stops in pattern are in fact objects but they only have stop ID.
  const fromStop = feed.stopsById[pattern.stops[fromStopIndex].stop_id]
  const toStop = feed.stopsById[pattern.stops[toStopIndex].stop_id]

  const geometry = lineSlice(
    point([fromStop.stop_lon, fromStop.stop_lat]),
    point([toStop.stop_lon, toStop.stop_lat]),
    {
      type: 'Feature',
      geometry: pattern.geometry,
      properties: {}
    }
  )

  const removedLengthThisPattern = turfLength(geometry)

  return (
    <table></table>
github developmentseed / skynet-scrub / app / scripts / components / map / index.js View on Github external
splitLine: function (e) {
    const { draw } = this;
    const ids = draw.getFeatureIdsAt(e.point);
    if (!ids.length) { return; }
    const line = draw.get(ids[0]);
    const cursorAt = point([e.lngLat.lng, e.lngLat.lat]);

    // delete the existing line, and add two additional lines.
    draw.delete(line.id);
    const newIds = [];
    newIds.push(draw.add(lineSlice(point(firstCoord(line)), cursorAt, line)));
    newIds.push(draw.add(lineSlice(cursorAt, point(lastCoord(line)), line)));

    this.splitMode({ featureIds: newIds });
    const newLines = newIds.map(id => draw.get(id));

    // Mark the new lines as edited
    newLines.forEach(this.markAsEdited);
    const actions = newLines.map(createRedo).concat(createUndo(line));
    this.props.dispatch(updateSelection(actions));
  },
github conveyal / analysis-ui / lib / components / modifications-map / hop-layer.js View on Github external
const makeGeometryIfNecessary = () => {
          // TODO: SIDE EFFECTS!!
          if (startHopIndex >= 0) {
            // get geometry
            const fromStopId = pattern.stops[startHopIndex].stop_id
            const fromStop = feed.stopsById[fromStopId]
            // get feature at end of hop
            const toStopId = pattern.stops[prevHopIndex + 1].stop_id
            const toStop = feed.stopsById[toStopId]
            const geometry = lineSlice(
              point([fromStop.stop_lon, fromStop.stop_lat]),
              point([toStop.stop_lon, toStop.stop_lat]),
              {
                type: 'Feature',
                properties: {},
                geometry: pattern.geometry
              }
            )

            selectedHopGeometries.push(geometry)
          }
        }
github conveyal / analysis-ui / lib / components / modifications-map / reroute-layer.js View on Github external
// make sure to find a toStopIndex _after_ the fromStopIndex (helps with loop routes also)
      const toStopIndex = modification.toStop != null
        ? pattern.stops.findIndex(
          (s, i) => i > fromStopIndex && s.stop_id === modification.toStop
        )
        : pattern.stops.length - 1

      const modificationAppliesToThisPattern =
        fromStopIndex !== -1 && toStopIndex !== -1
      if (modificationAppliesToThisPattern) {
        // NB using indices here so we get an object even if fromStop or toStop
        // is null stops in pattern are in fact objects but they only have stop ID.
        const fromStop = feed.stopsById[pattern.stops[fromStopIndex].stop_id]
        const toStop = feed.stopsById[pattern.stops[toStopIndex].stop_id]

        return lineSlice(
          point([fromStop.stop_lon, fromStop.stop_lat]),
          point([toStop.stop_lon, toStop.stop_lat]),
          {
            type: 'Feature',
            geometry: pattern.geometry,
            properties: {}
          }
        )
      }
    })
    .filter(segment => !!segment)
github developmentseed / skynet-scrub / app / scripts / components / map / index.js View on Github external
splitLine: function (e) {
    const { draw } = this;
    const ids = draw.getFeatureIdsAt(e.point);
    if (!ids.length) { return; }
    const line = draw.get(ids[0]);
    const cursorAt = point([e.lngLat.lng, e.lngLat.lat]);

    // delete the existing line, and add two additional lines.
    draw.delete(line.id);
    const newIds = [];
    newIds.push(draw.add(lineSlice(point(firstCoord(line)), cursorAt, line)));
    newIds.push(draw.add(lineSlice(cursorAt, point(lastCoord(line)), line)));

    this.splitMode({ featureIds: newIds });
    const newLines = newIds.map(id => draw.get(id));

    // Mark the new lines as edited
    newLines.forEach(this.markAsEdited);
    const actions = newLines.map(createRedo).concat(createUndo(line));
    this.props.dispatch(updateSelection(actions));
  },
github FreemapSlovakia / freemap-v3-react / src / components / TrackViewerResult.tsx View on Github external
computeInfoDistanceKm = (
    infoLat: number,
    infoLon: number,
    geojsonLineString,
  ) => {
    const p1 = point(geojsonLineString.geometry.coordinates[0]);
    const p2 = point([infoLon, infoLat]);
    return turfLength(turfLineSlice(p1, p2, geojsonLineString));
  };
github noncomputable / AgentMaps / src / routing.js View on Github external
}
	if (!intersection_indices.some(intersection_index =&gt; 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 &lt;= 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 &lt; start_to_path_end ?	street_path_unordered :	street_path_unordered.reverse();
	let street_path_lat_lngs = street_path.map(coords =&gt; { 
		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

90 / 100
Full package analysis

Popular @turf/line-slice functions