For a simple point in polygon test you can check turf
which has a booleanPointInPolygon
. Turf works in node but you should check for differences between v5 and v6+ around how to use npm accordingly. Points should be long/ lat (not lat/ long) and the polygon can be easily pulled out of the feature geometry of your feature collection.
For a more complex use case where you have many points and many polygons within which to locate them you should consider using rbush
.
Note that the rbush library constructs an r-tree out of the bounding boxes of the polygons and not the polygons themselves, so use of an r-tree is just a way to vastly reduce the number of polygons you need to test with booleanPointInPolygon
.
Example code for rbush
:
const RBush = require("rbush");
const turfBbox = require("@turf/bbox").default;
const geo = {} // your feature collection...
const maxEntriesPerNode = 50; // check the doco
const tree = new RBush(maxEntriesPerNode);
const bbox2Object = (keys, bbox) => ["minX", "minY", "maxX", "maxY"].reduce((o, k, i) => ({...o, [k]: bbox[i]}), {})
// create rtree from feature collection
geo.features.forEach(feature => {
const leaf = bbox2Object(bboxKeys, turfBbox(feature)); // use bbox of feature
leaf["id"] = feature.properties.SOME_ID; // add some custom properties
tree.insert(leaf);
});
// test a random point from your data
const [x, y] = [123, 456]; // should be long, lat
const test = tree.search({minX: x, minY: y, maxX: x, maxY: y});
// test should have an array of leaves per the tree.insert above
You can then perform the booleanPointInPolygon
test on this reduced set of polygons.