How to use the @turf/invariant.featureOf 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-polygonize / lib / Graph.js View on Github external
flattenEach(geoJson, feature => {
            featureOf(feature, 'LineString', 'Graph::fromGeoJson');
            // When a LineString if formed by many segments, split them
            coordReduce(feature, (prev, cur) => {
                if (prev) {
                    const start = graph.getNode(prev),
                        end = graph.getNode(cur);

                    graph.addEdge(start, end);
                }
                return cur;
            });
        });
github Turfjs / turf / packages / turf-polygonize / lib / polygonize.js View on Github external
flattenEach(geoJson, function (feature) {
        featureOf(feature, 'LineString', 'Graph::fromGeoJson');
        // When a LineString if formed by many segments, split them
        coordReduce(feature, function (prev, cur) {
            if (prev) {
                var start = graph.getNode(prev),
                    end = graph.getNode(cur);

                graph.addEdge(start, end);
            }
            return cur;
        });
    });
github Turfjs / turf / packages / turf-point-to-line-distance / index.ts View on Github external
function pointToLineDistance(pt: Coord, line: Feature | LineString, options: {
    units?: Units,
    method?: "geodesic" | "planar",
} = {}): number {
    // Optional parameters
    if (!options.method) { options.method = "geodesic"; }
    if (!options.units) { options.units = "kilometers"; }

    // validation
    if (!pt) { throw new Error("pt is required"); }
    if (Array.isArray(pt)) { pt = point(pt);
    } else if (pt.type === "Point") { pt = feature(pt);
    } else { featureOf(pt, "Point", "point"); }

    if (!line) { throw new Error("line is required"); }
    if (Array.isArray(line)) { line = lineString(line);
    } else if (line.type === "LineString") { line = feature(line);
    } else { featureOf(line, "LineString", "line"); }

    let distance = Infinity;
    const p = pt.geometry.coordinates;
    segmentEach(line, (segment) => {
        const a = segment!.geometry.coordinates[0];
        const b = segment!.geometry.coordinates[1];
        const d = distanceToSegment(p, a, b, options);
        if (d < distance) { distance = d; }
    });
    return convertLength(distance, "degrees", options.units);
}
github Turfjs / turf / packages / turf-point-to-line-distance / index.ts View on Github external
method?: "geodesic" | "planar",
} = {}): number {
    // Optional parameters
    if (!options.method) { options.method = "geodesic"; }
    if (!options.units) { options.units = "kilometers"; }

    // validation
    if (!pt) { throw new Error("pt is required"); }
    if (Array.isArray(pt)) { pt = point(pt);
    } else if (pt.type === "Point") { pt = feature(pt);
    } else { featureOf(pt, "Point", "point"); }

    if (!line) { throw new Error("line is required"); }
    if (Array.isArray(line)) { line = lineString(line);
    } else if (line.type === "LineString") { line = feature(line);
    } else { featureOf(line, "LineString", "line"); }

    let distance = Infinity;
    const p = pt.geometry.coordinates;
    segmentEach(line, (segment) => {
        const a = segment!.geometry.coordinates[0];
        const b = segment!.geometry.coordinates[1];
        const d = distanceToSegment(p, a, b, options);
        if (d < distance) { distance = d; }
    });
    return convertLength(distance, "degrees", options.units);
}

@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