Giter Site home page Giter Site logo

deniscarriere / geojson-rbush Goto Github PK

View Code? Open in Web Editor NEW
70.0 3.0 14.0 2.06 MB

GeoJSON implementation of RBush — a high-performance JavaScript R-tree-based 2D spatial index for points and rectangles

License: MIT License

JavaScript 92.74% TypeScript 7.26%
javascript r-tree spatial-index computational-geometry algorithm geojson

geojson-rbush's Introduction

GeoJSON RBush

Build Status npm version MIT licensed

GeoJSON implementation of RBush — a high-performance JavaScript R-tree-based 2D spatial index for points and rectangles.

Install

npm

$ npm install --save geojson-rbush

API

Table of Contents

rbush

GeoJSON implementation of RBush spatial index.

Parameters

  • maxEntries number defines the maximum number of entries in a tree node. 9 (used by default) is a reasonable choice for most applications. Higher value means faster insertion and slower search, and vice versa. (optional, default 9)

Examples

var geojsonRbush = require('geojson-rbush').default;
var tree = geojsonRbush();

Returns RBush GeoJSON RBush

insert

insert

Parameters

  • feature Feature insert single GeoJSON Feature

Examples

var poly = turf.polygon([[[-78, 41], [-67, 41], [-67, 48], [-78, 48], [-78, 41]]]);
tree.insert(poly)

Returns RBush GeoJSON RBush

load

load

Parameters

  • features (FeatureCollection | Array<Feature>) load entire GeoJSON FeatureCollection

Examples

var polys = turf.polygons([
    [[[-78, 41], [-67, 41], [-67, 48], [-78, 48], [-78, 41]]],
    [[[-93, 32], [-83, 32], [-83, 39], [-93, 39], [-93, 32]]]
]);
tree.load(polys);

Returns RBush GeoJSON RBush

remove

remove

Parameters

  • feature Feature remove single GeoJSON Feature
  • equals Function Pass a custom equals function to compare by value for removal.

Examples

var poly = turf.polygon([[[-78, 41], [-67, 41], [-67, 48], [-78, 48], [-78, 41]]]);

tree.remove(poly);

Returns RBush GeoJSON RBush

clear

clear

Examples

tree.clear()

Returns RBush GeoJSON Rbush

search

search

Parameters

  • geojson (BBox | FeatureCollection | Feature) search with GeoJSON

Examples

var poly = turf.polygon([[[-78, 41], [-67, 41], [-67, 48], [-78, 48], [-78, 41]]]);

tree.search(poly);

Returns FeatureCollection all features that intersects with the given GeoJSON.

collides

collides

Parameters

  • geojson (BBox | FeatureCollection | Feature) collides with GeoJSON

Examples

var poly = turf.polygon([[[-78, 41], [-67, 41], [-67, 48], [-78, 48], [-78, 41]]]);

tree.collides(poly);

Returns boolean true if there are any items intersecting the given GeoJSON, otherwise false.

all

all

Examples

tree.all()

Returns FeatureCollection all the features in RBush

toJSON

toJSON

Examples

var exported = tree.toJSON()

Returns any export data as JSON object

fromJSON

fromJSON

Parameters

  • json any import previously exported data

Examples

var exported = {
  "children": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [110, 50]
      },
      "properties": {},
      "bbox": [110, 50, 110, 50]
    }
  ],
  "height": 1,
  "leaf": true,
  "minX": 110,
  "minY": 50,
  "maxX": 110,
  "maxY": 50
}
tree.fromJSON(exported)

Returns RBush GeoJSON RBush

geojson-rbush's People

Contributors

breautek avatar deniscarriere avatar ffflabs avatar mickeyjohn avatar twelch avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

geojson-rbush's Issues

About managing 180th meridian and poles crossing

More a question than a real issue.

I'm playing with WFS 3.0 and your library and I'm not sure about this use case for the bbox.

In the compliance tests, I need to be able to manage when a polygon is crossing either 180th meridian or poles https://github.com/opengeospatial/ets-wfs30/blob/master/src/main/java/org/opengis/cite/wfs30/collections/GetFeaturesOperation.java#L123-L126
To deal with these cases, I'm wondering if I need to split my polygon crossing the meridian or the pole, get two polygons, run both tree.search(poly1) and tree.search(poly2), dedupe and merge in one GeoJSON or does Rbush and GeoJSON-RBush can manage it another way? Thanks for any input.

Unknown Geometry Type

Hi,

I am searching a tree of points, basically I'd like to find the points within the provided bbox.

// [nw-lon, nw-lat, se-lon, se-lat]
const bbox = getBbox(projection, zoom);

const match = {
  minX: bbox[0],
  minY: bbox[1],
  maxX: bbox[2],
  maxY: bbox[3],
};

myRbush.search(match);

Results in:

Uncaught Error: Unknown Geometry Type

any pointer why it doesn't work?

Cheers,
Andrej

index.d.ts is broken

Getting an error Type 'P' does not satisfy the constraint 'GeoJsonProperties'.
Please add P extends GeoJsonProperties to fix it by default.

Explode MultiPolygons when loading

I'm using martinez to intersect features w/ candidates from rbush in order to do strict intersections:

COUNTRY_INDEX.search(feature)
    .features.filter(x => {
      try {
        const i = martinez.intersection(
          x.geometry.coordinates,
          feature.geometry.coordinates
        );

        return i != null && i.length > 0;
      } catch (err) {
        // buffer to convert to a Polygon
        const i = martinez.intersection(
          x.geometry.coordinates,
          buffer(feature, 0.000001, { units: "degrees" }).geometry.coordinates
        );

        return i != null && i.length > 0;
      }
    })
    .map(x => x.properties.ADM0_A3);

COUNTRY_INDEX is Natural Earth admin 0 boundaries, many of which are MultiPolygons that include territories. As a result, the bboxes produced for the USA, Canada, and Russia cover much of the world. By splitting the MultiPolygons into multiple Polygon features, I get a substantial speed-up because fewer indexed features match:

const { featureCollection, polygon } = require("@turf/helpers");
const geojsonRbush = require("geojson-rbush");

const countries = require("./countries.json");

// explode multipolygons for smaller bounding boxes within the rtree
const polys = countries.features.filter(f => f.geometry.type === "Polygon");
const mps = countries.features.filter(f => f.geometry.type === "MultiPolygon");

const features = mps.reduce(
  (acc, mp) =>
    acc.concat(
      mp.geometry.coordinates.map(rings => polygon(rings, mp.properties))
    ),
  polys
);

const COUNTRY_INDEX = geojsonRbush();

COUNTRY_INDEX.load(featureCollection(features));

Doing this automatically within geojson-rbush seems like it would provide a transparent speed-up for those using MultiPolygons.

depends on rbush *

This module which @turf/line-intersect depends on, has a dependency on rbush with a version specification of *
rbush was just published at version 3.0.0
This has now broken geojson-rbush with the following exception:

TypeError: Cannot set property '_maxEntries' of undefined
    at RBush (/data/github/geopackage-js/node_modules/rbush/rbush.js:65:22)
    at Object.geojsonRbush [as default] (/data/github/geopackage-js/node_modules/geojson-rbush/index.js:22:16)
    at lineIntersect (/data/github/geopackage-js/node_modules/@turf/line-intersect/index.js:52:39)

geojson-rbush should depend on version 2 of rbush

Iincompatibility with the new turf version 7.0.0

Due to the latest update to @turf/bbox(7.0.0), this library is downloading as dependence, the new version, and causing bugs when compiling, I suggest changing turf dependencies from

"@turf/bbox": "*"

to

"@turf/bbox": "6.5.0"

geojson-rbush is including @types/geojson in builds

It looks like @types/geojson is marked as a dependency rather than a devDependency, and so the typescript types are ending up in the final bundle. It should be possible to change this dependency to a devDependency to reduce the final bundle size.

"dependencies": {
"@turf/bbox": "*",
"@turf/helpers": "6.x",
"@turf/meta": "6.x",
"@types/geojson": "7946.0.8",
"rbush": "^3.0.1"
}

How to use this in Angular 7 app?

Hi,
I have been trying to use Browserify to compile this module to use in angular 7 app. However, I still have not been success yet.

import * as GeoRbush from '../../../assets/js/geojson-rbush.min';
constructor() {
var tree = GeoRbush.default.geojsonRbush();
}
Console error:
"core.js:15724 ERROR Error: Uncaught (in promise): TypeError: _assets_js_geojson_rbush_min__WEBPACK_IMPORTED_MODULE_24___default.a.geojsonRbush is not a function
TypeError: _assets_js_geojson_rbush_min__WEBPACK_IMPORTED_MODULE_24___default.a.geojsonRbush is not a function"
...
Please anyone help how to make it work?
Thanks a lot.

Update turf dependencies to v4.6.0

Hi.

I can see this project has (among others) dependencies

  • "turf/bbox": "^3.10.5"
  • "turf/helpers": "^3.10.5"
  • "turf/meta": "^3.10.5"

Have you considered updating them to their v.4.6.0 versions? After all, I can see that those versions are mostly your creation 👍

Depending on version 3.x leads to duplicate packages when installing, for example, @turf/line-intersect

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.