How to use the @turf/invariant.getCoord function in @turf/invariant

To help you get started, we’ve selected a few @turf/invariant 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 Turfjs / turf / packages / turf-rhumb-distance / index.ts View on Github external
function rhumbDistance(from: Coord, to: Coord, options: {
    units?: Units,
} = {}): number {
    const origin = getCoord(from);
    const destination = getCoord(to);

    // compensate the crossing of the 180th meridian (https://macwright.org/2016/09/26/the-180th-meridian.html)
    // solution from https://github.com/mapbox/mapbox-gl-js/issues/3250#issuecomment-294887678
    destination[0] += (destination[0] - origin[0] > 180) ? -360 : (origin[0] - destination[0] > 180) ? 360 : 0;
    const distanceInMeters = calculateRhumbDistance(origin, destination);
    const distance = convertLength(distanceInMeters, "meters", options.units);
    return distance;
}
github Turfjs / turf / packages / turf-ellipse / index.js View on Github external
options = options || {};
    var steps = options.steps || 64;
    var units = options.units || 'kilometers';
    var angle = options.angle || 0;
    var pivot = options.pivot || center;
    var properties = options.properties || center.properties || {};

    // validation
    if (!center) throw new Error('center is required');
    if (!xSemiAxis) throw new Error('xSemiAxis is required');
    if (!ySemiAxis) throw new Error('ySemiAxis is required');
    if (!isObject(options)) throw new Error('options must be an object');
    if (!isNumber(steps)) throw new Error('steps must be a number');
    if (!isNumber(angle)) throw new Error('angle must be a number');

    var centerCoords = getCoord(center);
    if (units === 'degrees') {
        var angleRad = degreesToRadians(angle);
    } else {
        xSemiAxis = rhumbDestination(center, xSemiAxis, 90, {units: units});
        ySemiAxis = rhumbDestination(center, ySemiAxis, 0, {units: units});
        xSemiAxis = getCoord(xSemiAxis)[0] - centerCoords[0];
        ySemiAxis = getCoord(ySemiAxis)[1] - centerCoords[1];
    }

    var coordinates = [];
    for (var i = 0; i < steps; i += 1) {
        var stepAngle = i * -360 / steps;
        var x = ((xSemiAxis * ySemiAxis) / Math.sqrt(Math.pow(ySemiAxis, 2) + (Math.pow(xSemiAxis, 2) * Math.pow(getTanDeg(stepAngle), 2))));
        var y = ((xSemiAxis * ySemiAxis) / Math.sqrt(Math.pow(xSemiAxis, 2) + (Math.pow(ySemiAxis, 2) / Math.pow(getTanDeg(stepAngle), 2))));

        if (stepAngle < -90 && stepAngle >= -270) x = -x;
github thuanmb / react-mapbox-gl-cluster / src / lib / common / utils / cluster.js View on Github external
return totalBounds;
      }

      if (!totalBounds) {
        totalBounds = new LngLatBounds(coordinates[0], coordinates[0]);
      }

      totalBounds = coordinates.reduce(function(bounds, coord) {
        return bounds.extend(coord);
      }, totalBounds);
    });

    return totalBounds;
  }

  const coordinates = getCoord(data);
  return createBoundsFromCoordinates(coordinates, totalBounds);
};
github Turfjs / turf / packages / turf-directional-mean / index.ts View on Github external
const r: number = angle * Math.PI / 180;
        const sin: number = Math.sin(r);
        const cos: number = Math.cos(r);
        beginX = averageX - lenOfLine / 2 * cos;
        beginY = averageY - lenOfLine / 2 * sin;
        endX = averageX + lenOfLine / 2 * cos;
        endY = averageY + lenOfLine / 2 * sin;
        return [
            [beginX, beginY],
            [endX, endY],
        ];
    } else {
        const end = destination(point(centroidOfLine), lenOfLine / 2, angle, { units: "meters" });
        const begin = destination(point(centroidOfLine), -lenOfLine / 2, angle, { units: "meters" });
        return [
            getCoord(begin), getCoord(end),
        ];
    }
}
github FreemapSlovakia / freemap-v3-react / src / logic / elevationChartLogic.js View on Github external
function resolveElevationProfilePointsViaApi(
  trackGeojson,
  dispatch,
  cancelled$,
  storeDispatch,
  done,
) {
  const totalDistanceInKm = turfLength(trackGeojson);
  const delta = Math.min(0.1, totalDistanceInKm / (window.innerWidth / 2));
  const elevationProfilePoints = [];
  for (let dist = 0; dist <= totalDistanceInKm; dist += delta) {
    const [lon, lat] = getCoord(turfAlong(trackGeojson, dist));
    elevationProfilePoints.push({ lat, lon, distance: dist * 1000 });
  }

  const pid = Math.random();
  dispatch(startProgress(pid));
  const source = axios.CancelToken.source();
  cancelled$.subscribe(() => {
    source.cancel();
  });
  axios
    .post(
      `${process.env.API_URL}/geotools/elevation`,
      elevationProfilePoints.map(({ lat, lon }) => [lat, lon]),
      {
        validateStatus: status => status === 200,
        cancelToken: source.token,
github sharedstreets / sharedstreets-js / src / geometry.ts View on Github external
export function geometry(start: Location, end: Location, bearing: number): Geometry {
  const id = 'NxPFkg4CrzHeFhwV7Uiq7K'
  const coords = [getCoord(start), getCoord(end)]
  const properties = {
    id,
    fromIntersectionId: '5gRJyF2MT5BBErTyEesQLC',
    toIntersectionId: 'N38a21UGykpnqxwez7NGS3',
    forwardReferenceId: '2Vw2XzW4cs7r32RLhQnqwA',
    backReferenceId: 'VXKSEokmvBJ81XHYhUronG',
    roadClass: getRoadClass(3),
  }
  return lineString(coords, properties, {id})
}
github Turfjs / turf / packages / turf-spatial-weight / index.ts View on Github external
export function pNormDistance(feature1: Feature, feature2: Feature, p: number = 2): number {
  let coordinate1 = getCoord(feature1);
  let coordinate2 = getCoord(feature2);
  let xDiff = coordinate1[0] - coordinate2[0];
  let yDiff = coordinate1[1] - coordinate2[1];
  if (p == 1) {
    return Math.abs(xDiff) + Math.abs(yDiff);
  }
  return Math.pow((Math.pow(xDiff, p) + Math.pow(yDiff, p)), 1 / p);
}
github sharedstreets / sharedstreets-js / src / intersection.ts View on Github external
export function intersection(pt: Location): Intersection {
  const coord = getCoord(pt)
  const id = '5gRJyF2MT5BBErTyEesQLC'
  const properties = {
    id,
    osmNodeId: 42460951,
    outboundSegmentIds: ['6mjqqv7YNsp4541DmrrRbV', 'jwwKcUvHuCw6GJJAT3mDQ', '2Vw2XzW4cs7r32RLhQnqwA'],
    inboundSegmentIds: ['VmSkhzGKoEc767w98x35La', 'VXKSEokmvBJ81XHYhUronG', 'B7RPzs3hb1cSXqYcAKmUhE'],
  }

  return point(coord, properties, {id})
}
github Turfjs / turf / packages / turf-kernel_density / index.js View on Github external
_sum = featureReduce(output, (prev, current) => {
                    let
                        w = isWeighted ? (current.properties[weight] || 0) : 1,
                        c = getCoord(current).map((a, i) => Math.pow(w * a - m[i], 2));
                    return prev.map((a, i) => a + c[i]);
                }, [0, 0]),
                degDist = Math.sqrt((_sum[0] + _sum[1]) / pointsCount);

@turf/invariant

Lightweight utility for input validation and data extraction in Turf.js. Ensures GeoJSON inputs are in the correct format and extracts specific components like coordinates or geometries.

MIT
Latest version published 20 days ago

Package Health Score

93 / 100
Full package analysis