Skip to content

Instantly share code, notes, and snippets.

@dmfenton
Created November 27, 2019 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmfenton/d756c93f96c2aa889ab2925543364a16 to your computer and use it in GitHub Desktop.
Save dmfenton/d756c93f96c2aa889ab2925543364a16 to your computer and use it in GitHub Desktop.
spatial join w/ index
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