How to use the geojson-rbush function in geojson-rbush

To help you get started, we’ve selected a few geojson-rbush 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 splitLineWithPoints(line, splitter) {
    var results = [];
    var tree = rbush();

    flattenEach(splitter, function (point) {
        // Add index/id to features (needed for filter)
        results.forEach(function (feature, index) {
            feature.id = index;
        });
        // First Point - doesn't need to handle any previous line results
        if (!results.length) {
            results = splitLineWithPoint(line, point).features;

            // Add Square BBox to each feature for GeoJSON-RBush
            results.forEach(function (feature) {
                if (!feature.bbox) feature.bbox = square(bbox(feature));
            });
            tree.load(featureCollection(results));
        // Split with remaining points - lines might needed to be split multiple times
github Turfjs / turf / packages / turf-line-split / index.js View on Github external
function splitLineWithPoint(line, splitter) {
    var results = [];

    // handle endpoints
    var startPoint = getCoords(line)[0];
    var endPoint = getCoords(line)[line.geometry.coordinates.length - 1];
    if (pointsEquals(startPoint, getCoords(splitter)) ||
        pointsEquals(endPoint, getCoords(splitter))) return featureCollection([line]);

    // Create spatial index
    var tree = rbush();
    var segments = lineSegment(line);
    tree.load(segments);

    // Find all segments that are within bbox of splitter
    var search = tree.search(splitter);

    // Return itself if point is not within spatial index
    if (!search.features.length) return featureCollection([line]);

    // RBush might return multiple lines - only process the closest line to splitter
    var closestSegment = findClosestFeature(splitter, search);

    // Initial value is the first point of the first segments (beginning of line)
    var initialValue = [startPoint];
    var lastCoords = featureReduce(segments, function (previous, current, index) {
        var currentCoords = getCoords(current)[1];
github Turfjs / turf / packages / turf-dissolve / index.js View on Github external
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) {
            polygon = features[i];

            var matchFeaturePosition = potentialMatchingFeature.properties.origIndexPosition;

            if (originalIndexOfItemsRemoved.length > 0 && matchFeaturePosition !== 0) {
                if (matchFeaturePosition > originalIndexOfItemsRemoved[originalIndexOfItemsRemoved.length - 1]) {
                    matchFeaturePosition = matchFeaturePosition - (originalIndexOfItemsRemoved.length);
                } else {
github Turfjs / turf / packages / turf-line-intersect / index.ts View on Github external
if (line2.type === "LineString") { line2 = feature(line2); }
    if (line1.type === "Feature" &&
        line2.type === "Feature" &&
        line1.geometry !== null &&
        line2.geometry !== null &&
        line1.geometry.type === "LineString" &&
        line2.geometry.type === "LineString" &&
        line1.geometry.coordinates.length === 2 &&
        line2.geometry.coordinates.length === 2) {
        const intersect = intersects(line1, line2);
        if (intersect) { results.push(intersect); }
        return featureCollection(results);
    }

    // Handles complex GeoJSON Geometries
    const tree = rbush();
    tree.load(lineSegment(line2));
    featureEach(lineSegment(line1), (segment) => {
        featureEach(tree.search(segment), (match) => {
            const intersect = intersects(segment, match);
            if (intersect) {
                // prevent duplicate points https://github.com/Turfjs/turf/issues/688
                const key = getCoords(intersect).join(",");
                if (!unique[key]) {
                    unique[key] = true;
                    results.push(intersect);
                }
            }
        });
    });
    return featureCollection(results);
}
github Turfjs / turf / packages / turf-line-overlap / index.ts View on Github external
function lineOverlap(
    line1: Feature | G1,
    line2: Feature | G2,
    options: {tolerance?: number}={}
): FeatureCollection {
    // Optional parameters
    options = options || {};
    if (!isObject(options)) throw new Error('options is invalid');
    var tolerance = options.tolerance || 0;

    // Containers
    var features = [];

    // Create Spatial Index
    var tree = rbush();

    // To-Do -- HACK way to support typescript
    const line: any = lineSegment(line1);
    tree.load(line);
    var overlapSegment;

    // Line Intersection

    // Iterate over line segments
    segmentEach(line2, function (segment) {
        var doesOverlaps = false;

        // Iterate over each segments which falls within the same bounds
        featureEach(tree.search(segment), function (match) {
            if (doesOverlaps === false) {
                var coordsSegment = getCoords(segment).sort();
github liubgithub / maptalks.snapto / index.js View on Github external
constructor(options) {
        super(options);
        this.tree = rbush();
    }
github sharedstreets / sharedstreets-js / src / tile_index.ts View on Github external
constructor() {

        this.jkstraGraph = new jkstra.Graph();
        this.graphVertices = {};  
        this.nextVertexId = 1;

        this.tiles = new Set();
        this.objectIndex = new Map();
        this.featureIndex = new Map();
        this.intersectionIndex = geojsonRbush(9);
        this.geometryIndex = geojsonRbush(9);
    }
github sharedstreets / sharedstreets-js / src / tile_index.ts View on Github external
constructor() {

        this.jkstraGraph = new jkstra.Graph();
        this.graphVertices = {};  
        this.nextVertexId = 1;

        this.tiles = new Set();
        this.objectIndex = new Map();
        this.featureIndex = new Map();
        this.intersectionIndex = geojsonRbush(9);
        this.geometryIndex = geojsonRbush(9);
    }

geojson-rbush

GeoJSON implementation of RBush

MIT
Latest version published 3 years ago

Package Health Score

65 / 100
Full package analysis

Popular geojson-rbush functions