How to use @turf/clone - 10 common examples

To help you get started, we’ve selected a few @turf/clone 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 / src / concave / lib / turf-polygon-dissolve.ts View on Github external
export default function polygonDissolve(
    geojson: FeatureCollection,
    options: {mutate?: boolean} = {},
): Feature | null {
    // 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 (options.mutate === false || options.mutate === undefined) { geojson = clone(geojson); }

    const geoms: any[] = [];
    flattenEach(geojson, (feature) => {
        geoms.push(feature.geometry);
    });
    const topo: any = topology({geoms: geometryCollection(geoms).geometry});
    const merged: any = merge(topo, topo.objects.geoms.geometries);
    return merged;
}
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 => Typescript hack
    const data: any = geojson;

    switch (type) {
    case "LineString":
        return lineDissolve(data, options);
    case "Polygon":
        return polygonDissolve(data, options);
    default:
        throw new Error(type + " is not supported");
    }
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) => {
        // 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);
            return currentLine;
        }
    });
    // Append the last line
github Turfjs / turf / packages / turf-interpolate / index.js View on Github external
// calculate the distance from each input point to the grid points
        featureEach(points, function (point) {
            var gridPoint = (gridType === 'point') ? gridFeature : centroid(gridFeature);
            var d = distance(gridPoint, point, units);
            var zValue;
            // property has priority for zValue, fallbacks to 3rd coordinate from geometry
            if (property !== undefined) zValue = point.properties[property];
            if (zValue === undefined) zValue = point.geometry.coordinates[2];
            if (zValue === undefined) throw new Error('zValue is missing');
            if (d === 0) zw = zValue;
            var w = 1.0 / Math.pow(d, weight);
            sw += w;
            zw += w * zValue;
        });
        // write interpolated value for each grid point
        var newFeature = clone(gridFeature);
        newFeature.properties[property] = zw / sw;
        results.push(newFeature);
    });
    return featureCollection(results);
github Turfjs / turf / packages / turf-concave / lib / turf-polygon-dissolve.ts View on Github external
export default function polygonDissolve(
    geojson: FeatureCollection,
    options: {mutate?: boolean} = {},
): Feature | null {
    // 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 (options.mutate === false || options.mutate === undefined) { geojson = clone(geojson); }

    const geoms: any[] = [];
    flattenEach(geojson, (feature) => {
        geoms.push(feature.geometry);
    });
    const topo: any = topology({geoms: geometryCollection(geoms).geometry});
    const merged: any = merge(topo, topo.objects.geoms.geometries);
    return merged;
}
github Turfjs / turf / packages / turf-dissolve / index.js View on Github external
function dissolve(featureCollection, options) {
    // Optional parameters
    options = options || {};
    if (!isObject(options)) throw new Error('options is invalid');
    var propertyName = options.propertyName;

    // Input validation
    collectionOf(featureCollection, 'Polygon', 'dissolve');

    // Main
    var fc = clone(featureCollection);
    var features = fc.features;

    var originalIndexOfItemsRemoved = [];

    features.forEach(function (f, i) {
        f.properties.origIndexPosition = i;
    });
    var tree = rbush();
    tree.load(fc);

    for (var i in features) {
        var polygon = features[i];

        var featureChanged = false;

        tree.search(polygon).features.forEach(function (potentialMatchingFeature) {
github Turfjs / turf / packages / turf-transform-rotate / index.js View on Github external
if (!isObject(options)) throw new Error('options is invalid');
    var pivot = options.pivot;
    var mutate = options.mutate;

    // Input validation
    if (!geojson) throw new Error('geojson is required');
    if (angle === undefined || angle === null || isNaN(angle)) throw new Error('angle is required');

    // Shortcut no-rotation
    if (angle === 0) return geojson;

    // Use centroid of GeoJSON if pivot is not provided
    if (!pivot) pivot = centroid(geojson);

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

    // Rotate each coordinate
    coordEach(geojson, function (pointCoords) {
        var initialAngle = rhumbBearing(pivot, pointCoords);
        var finalAngle = initialAngle + angle;
        var distance = rhumbDistance(pivot, pointCoords);
        var newCoords = getCoords(rhumbDestination(pivot, distance, finalAngle));
        pointCoords[0] = newCoords[0];
        pointCoords[1] = newCoords[1];
    });
    return geojson;
}
github Turfjs / turf / packages / turf-nearest-point / index.ts View on Github external
function nearestPoint(targetPoint: Coord, points: FeatureCollection): NearestPoint {
    // Input validation
    if (!targetPoint) throw new Error('targetPoint is required');
    if (!points) throw new Error('points is required');

    let nearest: NearestPoint;
    let minDist: number = Infinity;
    let bestFeatureIndex: number = 0;
    featureEach(points,  (pt, featureIndex) => {
        const distanceToPoint = distance(targetPoint, pt);
        if (distanceToPoint < minDist) {
            bestFeatureIndex = featureIndex;
            minDist = distanceToPoint;
        }
    });
    nearest = clone(points.features[bestFeatureIndex]);
    nearest.properties.featureIndex = bestFeatureIndex;
    nearest.properties.distanceToPoint = minDist;
    return nearest;
}
github Turfjs / turf / packages / turf-projection / index.ts View on Github external
function convert(geojson: any, projection: string, options: {mutate?: boolean} = {}): any {
    // Optional parameters
    options = options || {};
    var mutate = options.mutate;

    // Validation
    if (!geojson) throw new Error('geojson is required');

    // Handle Position
    if (Array.isArray(geojson) && isNumber(geojson[0])) geojson = (projection === 'mercator') ? convertToMercator(geojson) : convertToWgs84(geojson);

    // Handle GeoJSON
    else {
        // Handle possible data mutation
        if (mutate !== true) geojson = clone(geojson);

        coordEach(geojson, function (coord) {
            var newCoord = (projection === 'mercator') ? convertToMercator(coord) : convertToWgs84(coord);
            coord[0] = newCoord[0];
            coord[1] = newCoord[1];
        });
    }
    return geojson;
}
github Turfjs / turf / src / concave / lib / turf-polygon-dissolve.js View on Github external
function polygonDissolve(geojson, options) {
    if (options === void 0) { options = {}; }
    // 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
    // Topojson modifies in place, so we need to deep clone first
    if (options.mutate === false || options.mutate === undefined) {
        geojson = clone_1.default(geojson);
    }
    var geoms = [];
    meta_1.flattenEach(geojson, function (feature) {
        geoms.push(feature.geometry);
    });
    var topo = topojson_1.topology({ geoms: helpers_1.geometryCollection(geoms).geometry });
    var merged = topojson_1.merge(topo, topo.objects.geoms.geometries);
    return merged;
}
exports.default = polygonDissolve;

@turf/clone

turf clone module

MIT
Latest version published 3 months ago

Package Health Score

98 / 100
Full package analysis

Popular @turf/clone functions