diff --git a/lib/methods/areal-weighting.js b/lib/methods/areal-weighting.js index a99d388..7614b94 100644 --- a/lib/methods/areal-weighting.js +++ b/lib/methods/areal-weighting.js @@ -3,6 +3,7 @@ const Turf = require('turf'); const Logger = require('../logger'); const isUndefined = require('lodash/isUndefined'); +const rbush = require('rbush'); function main(source, target, options) { target.features.forEach((d, i) => { @@ -18,6 +19,7 @@ function arealWeighting(feature, source, options, attributeName) { const intersectData = getIntersectingFeatures(source.features, feature); const sourceFeatures = intersectData[0]; const intersects = intersectData[1]; + intersects.forEach((d,i) => { const Ps = parseFloat(sourceFeatures[i].properties[attributeName]); @@ -32,18 +34,48 @@ function arealWeighting(feature, source, options, attributeName) { function getIntersectingFeatures(sourceFeatures, targetFeature) { let intersects = []; - let featureSimpl = targetFeature; + let sourceList = []; // source features that got intersected? + + /* Create and fill the bushy tree */ + var tree = rbush(); + let items = []; + + for (var i=0; i { - let intersection = Turf.intersect(featureSimpl, f); + /* Quickly get the likely candidates that might intersect */ + var bbox = Turf.extent(targetFeature); + var candidates = tree.search({ + minX: bbox[0], + minY: bbox[1], + maxX: bbox[2], + maxY: bbox[3] + }); + + /* Check the candidates for actual intersection and do the rest */ + for (var i=0; i