How to use the @turf/meta.coordEach function in @turf/meta

To help you get started, we’ve selected a few @turf/meta 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-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;

        var neutralizedPoints = coords.map(function (point) {
            return [
                point[0] - translation[0],
                point[1] - translation[1]</p>
github Turfjs / turf / packages / turf-transform-rotate / index.js View on Github external
// 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-projection / index.ts View on Github external
// 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 developmentseed / skynet-scrub / app / scripts / components / map / index.js View on Github external
const { draw, props } = this;

    const cursor = point([e.lngLat.lng, e.lngLat.lat]);
    const featureIds = draw.getFeatureIdsAt(e.point);

    // a length of 2 means we're hovering over a feature
    if (featureIds.length &gt; 1) {
      // the first item in the array seems to always be the one the user is continuing from
      const fromLineString = draw.get(featureIds[0]);
      const originalFromLineString = fromLineString;
      const toLineString = draw.get(featureIds[1]);
      const mergedLineString = { type: 'Feature', properties: { status: EDITED } };
      let nearest;
      let minDist;

      coordEach(toLineString, function (coord, i) {
        var dist = distance(cursor, point(coord));

        if (!minDist || dist &lt; minDist) {
          nearest = toLineString.geometry.coordinates[i];
          minDist = dist;
        }
      });

      // add point to front or back of fromLineString dependending on distance
      // TODO: is there a way to get something like "most recent point i've continued from"
      const fromFront = fromLineString.geometry.coordinates[0];
      const fromBack = fromLineString.geometry.coordinates[fromLineString.geometry.coordinates.length - 1];
      const frontDistance = distance(fromFront, nearest);
      const backDistance = distance(fromBack, nearest);

      if (frontDistance &gt; backDistance) {
github Turfjs / turf / packages / turf-flip / index.js View on Github external
function flip(geojson, options) {
    // Backwards compatible with v4.0
    var mutate = (typeof options === 'object') ? options.mutate : options;

    if (!geojson) throw new Error('geojson is required');
    // ensure that we don't modify features in-place and changes to the
    // output do not change the previous feature, including changes to nested
    // properties.
    if (mutate === false || mutate === undefined) geojson = JSON.parse(JSON.stringify(geojson));

    coordEach(geojson, function (coord) {
        var x = coord[0];
        var y = coord[1];
        coord[0] = y;
        coord[1] = x;
    });
    return geojson;
}
github Turfjs / turf / packages / turf-transform-translate / index.js View on Github external
zTranslation = (zTranslation !== undefined) ? zTranslation : 0;
    if (distance === 0 &amp;&amp; zTranslation === 0) return geojson;

    if (direction === undefined || direction === null || isNaN(direction)) throw new Error('direction is required');

    // Invert with negative distances
    if (distance &lt; 0) {
        distance = -distance;
        direction = -direction;
    }

    // Clone geojson to avoid side effects
    if (mutate === false || mutate === undefined) geojson = JSON.parse(JSON.stringify(geojson));

    // Translate each coordinate
    coordEach(geojson, function (pointCoords) {
        var newCoords = getCoords(rhumbDestination(pointCoords, distance, direction, units));
        pointCoords[0] = newCoords[0];
        pointCoords[1] = newCoords[1];
        if (zTranslation &amp;&amp; pointCoords.length === 3) pointCoords[2] += zTranslation;
    });
    return geojson;
}
github Turfjs / turf / packages / turf-explode / index.js View on Github external
export default function (geojson) {
    var points = [];
    if (geojson.type === 'FeatureCollection') {
        featureEach(geojson, function (feature) {
            coordEach(feature, function (coord) {
                points.push(point(coord, feature.properties));
            });
        });
    } else {
        coordEach(geojson, function (coord) {
            points.push(point(coord, geojson.properties));
        });
    }
    return featureCollection(points);
}
github Turfjs / turf / packages / turf-center-mean / index.ts View on Github external
geomEach(geojson, function (geom, featureIndex, properties) {
        let weight = properties[options.weight];
        weight = (weight === undefined || weight === null) ? 1 : weight;
        if (!isNumber(weight)) throw new Error('weight value must be a number for feature index ' + featureIndex);
        weight = Number(weight);
        if (weight > 0) {
            coordEach(geom, function (coord) {
                sumXs += coord[0] * weight;
                sumYs += coord[1] * weight;
                sumNs += weight;
            });
        }
    });
    return point([sumXs / sumNs, sumYs / sumNs], options.properties, options);
github Turfjs / turf / packages / turf-polygon-smooth / index.js View on Github external
function processMultiPolygon(poly, tempOutput) {
    var prevGeomIndex = 0;
    var subtractCoordIndex = 0;
    var prevMultiIndex = 0;

    coordEach(poly, function (currentCoord, coordIndex, featureIndex, multiFeatureIndex, geometryIndex) {
        if (multiFeatureIndex > prevMultiIndex) {
            prevMultiIndex = multiFeatureIndex;
            subtractCoordIndex = coordIndex;
            tempOutput.push([[]]);
        }
        if (geometryIndex > prevGeomIndex) {
            prevGeomIndex = geometryIndex;
            subtractCoordIndex = coordIndex;
            tempOutput[multiFeatureIndex].push([]);
        }
        var realCoordIndex = coordIndex - subtractCoordIndex;
        var p1 = poly.coordinates[multiFeatureIndex][geometryIndex][realCoordIndex + 1];
        var p0x = currentCoord[0];
        var p0y = currentCoord[1];
        var p1x = p1[0];
        var p1y = p1[1];
github Turfjs / turf / packages / turf-isolines / index.js View on Github external
createdIsoLines.forEach(function (isoline) {
        coordEach(isoline, resize);
    });
    return createdIsoLines;