Created
November 27, 2019 14:22
-
-
Save dmfenton/2db764549ea0245cae5d350b840e4067 to your computer and use it in GitHub Desktop.
spatial join w/ index
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const rectangles = require('./buildings2.json').features | |
const points = require('./dmv_props.json').features | |
const bbox = require('@turf/bbox').default | |
const RBush = require('rbush') | |
function prepare(features) { | |
return features.map(f => { | |
const box = bbox(f) | |
return { | |
minX: box[0], | |
minY: box[1], | |
maxX: box[2], | |
maxY: box[3], | |
EGID: f.properties.EGID | |
} | |
}) | |
} | |
function search (tree, point) { | |
const [x, y] = point.geometry.coordinates | |
return tree.search({ | |
minX: x, | |
maxX: x, | |
minY: y, | |
maxY: y | |
}) | |
} | |
function index (features) { | |
const tree = new RBush() | |
const prepared = prepare(features) | |
return tree.load(prepared) | |
} | |
const tree = index(rectangles) | |
points.forEach(p => { | |
const [x, y] = p.geometry.coordinates | |
const thingToFind = { | |
minX: x, | |
maxX: x, | |
minY: y, | |
maxY: y | |
} | |
// console.log(thingToFind) | |
const found = tree.search(thingToFind) | |
if (found[0]) { | |
p.properties.EGID = found[0].EGID | |
} | |
}) | |
filtered = points.filter(p => !!p.properties.EGID) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment