How to use the @turf/bearing function in @turf/bearing

To help you get started, we’ve selected a few @turf/bearing 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 / utils / markers.js View on Github external
export function getBearingAndCoordinatesAlongLine(
  coordinates,
  spacingMeters = DIRECTION_MARKER_SPACING_METERS
) {
  if (coordinates.length < 2) return []

  const markers = [
    {
      bearing: bearing(point(coordinates[0]), point(coordinates[1])),
      lon: coordinates[0][0],
      lat: coordinates[0][1]
    }
  ]

  for (
    let segIdx = 1, distanceToLastMarker = 0, totalDistance = 0;
    segIdx < coordinates.length;
    segIdx++
  ) {
    const segStart = coordinates[segIdx - 1]
    const segEnd = coordinates[segIdx]
    const distanceThisSegment = distance(point(segStart), point(segEnd)) * 1000

    // while not if, may need multiple markers on a single long segment
    while (
github uber / nebula.gl / modules / core / src / lib / editable-feature-collection.js View on Github external
geometry: {
          type: 'LineString',
          coordinates: [this._clickSequence[0], groundCoords]
        }
      });
    } else if (this._clickSequence.length === 2) {
      const lineString: LineString = {
        type: 'LineString',
        coordinates: this._clickSequence
      };

      const [p1, p2] = this._clickSequence;
      const pt = point(groundCoords);
      const options = { units: 'miles' };
      const ddistance = pointToLineDistance(pt, lineString, options);
      const lineBearing = bearing(p1, p2);

      // Check if current point is to the left or right of line
      // Line from A=(x1,y1) to B=(x2,y2) a point P=(x,y)
      // then (x−x1)(y2−y1)−(y−y1)(x2−x1)
      const isPointToLeftOfLine =
        (groundCoords[0] - p1[0]) * (p2[1] - p1[1]) - (groundCoords[1] - p1[1]) * (p2[0] - p1[0]);

      // Bearing to draw perpendicular to the line string
      const orthogonalBearing = isPointToLeftOfLine < 0 ? lineBearing - 90 : lineBearing - 270;

      // Get coordinates for the point p3 and p4 which are perpendicular to the lineString
      // Add the distance as the current position moves away from the lineString
      const p3 = destination(p2, ddistance, orthogonalBearing, options);
      const p4 = destination(p1, ddistance, orthogonalBearing, options);

      this._setTentativeFeature({
github uber / nebula.gl / modules / layers / src / mode-handlers / draw-90degree-polygon-handler.js View on Github external
getIntermediatePoint(coordinates: Position[]) {
    let pt;
    if (coordinates.length > 4) {
      const [p1, p2] = [...coordinates];
      const angle1 = bearing(p1, p2);
      const p3 = coordinates[coordinates.length - 3];
      const p4 = coordinates[coordinates.length - 4];
      const angle2 = bearing(p3, p4);

      const angles = { first: [], second: [] };
      // calculate 3 right angle points for first and last points in lineString
      [1, 2, 3].forEach(factor => {
        const newAngle1 = angle1 + factor * 90;
        // convert angles to 0 to -180 for anti-clock and 0 to 180 for clock wise
        angles.first.push(newAngle1 > 180 ? newAngle1 - 360 : newAngle1);
        const newAngle2 = angle2 + factor * 90;
        angles.second.push(newAngle2 > 180 ? newAngle2 - 360 : newAngle2);
      });

      const distance = turfDistance(point(p1), point(p3));
      // Draw imaginary right angle lines for both first and last points in lineString
github uber / nebula.gl / modules / layers / src / mode-handlers / draw-90degree-polygon-handler.js View on Github external
getIntermediatePoint(coordinates: Position[]) {
    let pt;
    if (coordinates.length > 4) {
      const [p1, p2] = [...coordinates];
      const angle1 = bearing(p1, p2);
      const p3 = coordinates[coordinates.length - 3];
      const p4 = coordinates[coordinates.length - 4];
      const angle2 = bearing(p3, p4);

      const angles = { first: [], second: [] };
      // calculate 3 right angle points for first and last points in lineString
      [1, 2, 3].forEach(factor => {
        const newAngle1 = angle1 + factor * 90;
        // convert angles to 0 to -180 for anti-clock and 0 to 180 for clock wise
        angles.first.push(newAngle1 > 180 ? newAngle1 - 360 : newAngle1);
        const newAngle2 = angle2 + factor * 90;
        angles.second.push(newAngle2 > 180 ? newAngle2 - 360 : newAngle2);
      });

      const distance = turfDistance(point(p1), point(p3));
      // Draw imaginary right angle lines for both first and last points in lineString
      // If there is intersection point for any 2 lines, will be the 90 degree point.
      [0, 1, 2].forEach(indexFirst => {
        const line1 = lineString([
github uber / streetscape.gl / test / apps / measure / src / tape-measure-layer.js View on Github external
function getAngle(origin, p1, p2) {
  const b1 = bearing(origin, p1);
  const b2 = bearing(origin, p2);
  let angle = b1 - b2;
  if (angle < -180) {
    angle += 360;
  }
  if (angle > 180) {
    angle -= 360;
  }
  return angle;
}
github uber / nebula.gl / modules / layers / src / mode-handlers / translate-handler.js View on Github external
getTranslateAction(
    startDragPoint: Position,
    currentPoint: Position,
    editType: string
  ): ?EditAction {
    if (!this._geometryBeforeTranslate) {
      return null;
    }
    const p1 = point(startDragPoint);
    const p2 = point(currentPoint);

    const distanceMoved = turfDistance(p1, p2);
    const direction = turfBearing(p1, p2);

    const movedFeatures = turfTransformTranslate(
      this._geometryBeforeTranslate,
      distanceMoved,
      direction
    );

    let updatedData = this.getImmutableFeatureCollection();

    const selectedIndexes = this.getSelectedFeatureIndexes();
    for (let i = 0; i < selectedIndexes.length; i++) {
      const selectedIndex = selectedIndexes[i];
      const movedFeature = movedFeatures.features[i];
      updatedData = updatedData.replaceGeometry(selectedIndex, movedFeature.geometry);
    }
github Turfjs / turf / packages / turf-directional-mean / index.ts View on Github external
const beginPoint: number[] = coordinates[0];
    const endPoint: number[] = coordinates[coordinates.length - 1];
    if (isPlanar) {
        const [x0, y0]: number[] = beginPoint;
        const [x1, y1]: number[] = endPoint;
        const dx: number = x1 - x0;
        const dy: number = y1 - y0;
        const h = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
        if (h < 0.000000001) {
            return [NaN, NaN];
        }
        const sin1 = dy / h;
        const cos1 = dx / h;
        return [sin1, cos1];
    } else {
        const angle = bearingToCartesian(bearing(beginPoint, endPoint));
        const radian = angle * Math.PI / 180;
        return [Math.sin(radian), Math.cos(radian)];
    }

}
github uber / nebula.gl / modules / layers / src / mode-handlers / extrude-handler.js View on Github external
getBearing(p1: any, p2: any) {
    const angle = bearing(p1, p2);
    if (angle < 0) {
      return Math.floor(360 + angle);
    }
    return Math.floor(angle);
  }

@turf/bearing

turf bearing module

MIT
Latest version published 3 months ago

Package Health Score

98 / 100
Full package analysis

Popular @turf/bearing functions