How to use the @turf/invariant.getType 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-line-split / index.js View on Github external
function lineSplit(line, splitter) {
    if (!line) throw new Error('line is required');
    if (!splitter) throw new Error('splitter is required');

    var lineType = getType(line);
    var splitterType = getType(splitter);

    if (lineType !== 'LineString') throw new Error('line must be LineString');
    if (splitterType === 'FeatureCollection') throw new Error('splitter cannot be a FeatureCollection');
    if (splitterType === 'GeometryCollection') throw new Error('splitter cannot be a GeometryCollection');

    // remove excessive decimals from splitter
    // to avoid possible approximation issues in rbush
    var truncatedSplitter = truncate(splitter, {precision: 7});

    switch (splitterType) {
    case 'Point':
        return splitLineWithPoint(line, truncatedSplitter);
    case 'MultiPoint':
        return splitLineWithPoints(line, truncatedSplitter);
    case 'LineString':
github Turfjs / turf / packages / turf-center-of-mass / index.ts View on Github external
function centerOfMass<p>(geojson: any, options: {
    properties?: P,
} = {}): Feature {
    switch (getType(geojson)) {
    case 'Point':
        return point(getCoord(geojson), options.properties);
    case 'Polygon':
        var coords = [];
        coordEach(geojson, function (coord) {
            coords.push(coord);
        });

        // First, we neutralize the feature (set it around coordinates [0,0]) to prevent rounding errors
        // We take any point to translate all the points around 0
        var centre = centroid(geojson, {properties: options.properties});
        var translation = centre.geometry.coordinates;
        var sx = 0;
        var sy = 0;
        var sArea = 0;
        var i, pi, pj, xi, xj, yi, yj, a;</p>
github Turfjs / turf / src / concave / lib / turf-dissolve.ts View on Github external
function dissolve(geojson: FeatureCollection, options: {
    mutate?: boolean,
} = {}): Feature | null {
    // Optional parameters
    options = options || {};
    if (!isObject(options)) { throw new Error("options is invalid"); }
    const mutate = options.mutate;

    // Validation
    if (getType(geojson) !== "FeatureCollection") { throw new Error("geojson must be a FeatureCollection"); }
    if (!geojson.features.length) { throw new Error("geojson is empty"); }

    // Clone geojson to avoid side effects
    // Topojson modifies in place, so we need to deep clone first
    if (mutate === false || mutate === undefined) { geojson = clone(geojson); }

    // Assert homogenity
    const type = getHomogenousType(geojson);
    if (!type) { throw new Error("geojson must be homogenous"); }

    // Data =&gt; Typescript hack
    const data: any = geojson;

    switch (type) {
    case "LineString":
        return lineDissolve(data, options);
github Turfjs / turf / src / concave / lib / turf-line-dissolve.js View on Github external
function lineDissolve(geojson, options) {
    if (options === void 0) { options = {}; }
    // Optional parameters
    options = options || {};
    if (!helpers_1.isObject(options)) {
        throw new Error("options is invalid");
    }
    var mutate = options.mutate;
    // Validation
    if (invariant_1.getType(geojson) !== "FeatureCollection") {
        throw new Error("geojson must be a FeatureCollection");
    }
    if (!geojson.features.length) {
        throw new Error("geojson is empty");
    }
    // Clone geojson to avoid side effects
    if (mutate === false || mutate === undefined) {
        geojson = clone_1.default(geojson);
    }
    var result = [];
    var lastLine = meta_1.lineReduce(geojson, function (previousLine, currentLine) {
        // Attempt to merge this LineString with the other LineStrings, updating
        // the reference as it is merged with others and grows.
        var merged = mergeLineStrings(previousLine, currentLine);
        // Accumulate the merged LineString
        if (merged) {
github Turfjs / turf / packages / turf-transform-scale / index.js View on Github external
function scale(feature, factor, origin) {
    // Default params
    var isPoint = getType(feature) === 'Point';
    origin = defineOrigin(feature, origin);

    // Shortcut no-scaling
    if (factor === 1 || isPoint) return feature;

    // Scale each coordinate
    coordEach(feature, function (coord) {
        var originalDistance = rhumbDistance(origin, coord);
        var bearing = rhumbBearing(origin, coord);
        var newDistance = originalDistance * factor;
        var newCoord = getCoords(rhumbDestination(origin, newDistance, bearing));
        coord[0] = newCoord[0];
        coord[1] = newCoord[1];
        if (coord.length === 3) coord[2] *= factor;
    });
github Turfjs / turf / src / concave / lib / turf-line-dissolve.ts View on Github external
function lineDissolve(
    geojson: FeatureCollection,
    options: {mutate?: boolean} = {},
): Feature | null {
    // Optional parameters
    options = options || {};
    if (!isObject(options)) { throw new Error("options is invalid"); }
    const mutate = options.mutate;

    // Validation
    if (getType(geojson) !== "FeatureCollection") { throw new Error("geojson must be a FeatureCollection"); }
    if (!geojson.features.length) { throw new Error("geojson is empty"); }

    // Clone geojson to avoid side effects
    if (mutate === false || mutate === undefined) { geojson = clone(geojson); }

    const result: any[] = [];
    const lastLine = lineReduce(geojson, (previousLine: any, currentLine: any) =&gt; {
        // Attempt to merge this LineString with the other LineStrings, updating
        // the reference as it is merged with others and grows.
        const merged = mergeLineStrings(previousLine, currentLine);

        // Accumulate the merged LineString
        if (merged) { return merged;
        // Put the unmerged LineString back into the list
        } else {
            result.push(previousLine);
github Turfjs / turf / packages / turf-nearest-point-to-line / index.ts View on Github external
points: FeatureCollection | Feature | GeometryCollection,
    line: Feature | LineString,
    options: {
        units?: Units,
        properties?: Properties,
    } = {},
): Feature {
    const units = options.units;
    const properties = options.properties || {};

    // validation
    const pts = normalize(points);
    if (!pts.features.length) { throw new Error("points must contain features"); }

    if (!line) { throw new Error("line is required"); }
    if (getType(line) !== "LineString") { throw new Error("line must be a LineString"); }

    let dist = Infinity;
    let pt: any = null;

    featureEach(pts, (point) =&gt; {
        const d = pointToLineDistance(point, line, { units });
        if (d &lt; dist) {
            dist = d;
            pt = point;
        }
    });
    /**
     * Translate Properties to final Point, priorities:
     * 1. options.properties
     * 2. inherent Point properties
     * 3. dist custom properties created by NearestPointToLine
github Turfjs / turf / packages / turf-concave / lib / turf-line-dissolve.ts View on Github external
function lineDissolve(
    geojson: FeatureCollection,
    options: {mutate?: boolean} = {},
): Feature | null {
    // Optional parameters
    options = options || {};
    if (!isObject(options)) { throw new Error("options is invalid"); }
    const mutate = options.mutate;

    // Validation
    if (getType(geojson) !== "FeatureCollection") { throw new Error("geojson must be a FeatureCollection"); }
    if (!geojson.features.length) { throw new Error("geojson is empty"); }

    // Clone geojson to avoid side effects
    if (mutate === false || mutate === undefined) { geojson = clone(geojson); }

    const result: any[] = [];
    const lastLine = lineReduce(geojson, (previousLine: any, currentLine: any) =&gt; {
        // Attempt to merge this LineString with the other LineStrings, updating
        // the reference as it is merged with others and grows.
        const merged = mergeLineStrings(previousLine, currentLine);

        // Accumulate the merged LineString
        if (merged) { return merged;
        // Put the unmerged LineString back into the list
        } else {
            result.push(previousLine);

@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