@turf/collect
Advanced tools
Comparing version 3.7.3 to 3.7.5
80
bench.js
var aggregate = require('./'); | ||
var Benchmark = require('benchmark'); | ||
var fs = require('fs'); | ||
var polygon = require('@turf/helpers').polygon; | ||
var point = require('@turf/helpers').point; | ||
var featurecollection = require('@turf/helpers').featureCollection; | ||
var polygon = require('@turf/helpers'); | ||
var point = require('@turf/helpers'); | ||
var featureCollection = require('@turf/helpers'); | ||
var poly1 = polygon([[[0,0],[10,0],[10,10],[0,10]]]) | ||
var poly2 = polygon([[[10,0],[20,10],[20,20], [20,0]]]) | ||
var polyFC = featurecollection([poly1, poly2]) | ||
var pt1 = point(5,5, {population: 200}) | ||
var pt2 = point(1,3, {population: 600}) | ||
var pt3 = point(14,2, {population: 100}) | ||
var pt4 = point(13,1, {population: 200}) | ||
var pt5 = point(19,7, {population: 300}) | ||
var aggregations = [ | ||
{ | ||
aggregation: 'sum', | ||
inField: 'population', | ||
outField: 'pop_sum' | ||
}, | ||
{ | ||
aggregation: 'average', | ||
inField: 'population', | ||
outField: 'pop_avg' | ||
}, | ||
{ | ||
aggregation: 'median', | ||
inField: 'population', | ||
outField: 'pop_median' | ||
}, | ||
{ | ||
aggregation: 'min', | ||
inField: 'population', | ||
outField: 'pop_min' | ||
}, | ||
{ | ||
aggregation: 'max', | ||
inField: 'population', | ||
outField: 'pop_max' | ||
}, | ||
{ | ||
aggregation: 'deviation', | ||
inField: 'population', | ||
outField: 'pop_deviation' | ||
}, | ||
{ | ||
aggregation: 'variance', | ||
inField: 'population', | ||
outField: 'pop_variance' | ||
}, | ||
{ | ||
aggregation: 'count', | ||
inField: '', | ||
outField: 'point_count' | ||
} | ||
] | ||
var poly1 = polygon([[[0, 0], [10, 0], [0, 10], [0, 10], [0, 0]]]); | ||
var poly2 = polygon([[[10, 0], [20, 10], [20, 20], [20, 0], [10, 0]]]); | ||
var polyFC = featureCollection([poly1, poly2]); | ||
var pt1 = point([5, 5], {population: 200}); | ||
var pt2 = point([1, 3], {population: 600}); | ||
var pt3 = point([14, 2], {population: 100}); | ||
var pt4 = point([13, 1], {population: 200}); | ||
var pt5 = point([19, 7], {population: 300}); | ||
var ptFC = featurecollection([pt1, pt2, pt3, pt4, pt5]) | ||
var ptFC = featureCollection([pt1, pt2, pt3, pt4, pt5]); | ||
var suite = new Benchmark.Suite('turf-aggregate'); | ||
suite | ||
.add('turf-aggregate',function() { | ||
aggregate(polyFC, ptFC, aggregations) | ||
.add('turf-aggregate', function () { | ||
aggregate(polyFC, ptFC, 'population', 'outPopulation'); | ||
}) | ||
.on('cycle', function(event) { | ||
console.log(String(event.target)); | ||
.on('cycle', function (event) { | ||
console.log(String(event.target)); | ||
}) | ||
.on('complete', function() { | ||
.on('complete', function () { | ||
}) | ||
.run(); | ||
.run(); |
30
index.js
@@ -0,2 +1,4 @@ | ||
var turfbbox = require('@turf/bbox'); | ||
var inside = require('@turf/inside'); | ||
var rbush = require('rbush'); | ||
@@ -30,9 +32,17 @@ /** | ||
*/ | ||
module.exports = function collect(polygons, points, inProperty, outProperty) { | ||
module.exports = function (polygons, points, inProperty, outProperty) { | ||
var rtree = rbush(6); | ||
var treeItems = points.features.map(function (item) { | ||
return { | ||
minX: item.geometry.coordinates[0], | ||
minY: item.geometry.coordinates[1], | ||
maxX: item.geometry.coordinates[0], | ||
maxY: item.geometry.coordinates[1], | ||
property: item.properties[inProperty] | ||
}; | ||
}); | ||
rtree.load(treeItems); | ||
polygons.features.forEach(function (poly) { | ||
var values = points.features.filter(function (pt) { | ||
return inside(pt, poly); | ||
}).map(function (pt) { | ||
return pt.properties[inProperty]; | ||
}); | ||
@@ -42,2 +52,10 @@ if (!poly.properties) { | ||
} | ||
var bbox = turfbbox(poly); | ||
var potentialPoints = rtree.search({minX: bbox[0], minY: bbox[1], maxX: bbox[2], maxY: bbox[3]}); | ||
var values = []; | ||
potentialPoints.forEach(function (pt) { | ||
if (inside({'type': 'Point', 'coordinates': [pt.minX, pt.minY]}, poly)) { | ||
values.push(pt.property); | ||
} | ||
}); | ||
@@ -44,0 +62,0 @@ poly.properties[outProperty] = values; |
{ | ||
"name": "@turf/collect", | ||
"version": "3.7.3", | ||
"version": "3.7.5", | ||
"description": "turf aggregate module", | ||
@@ -13,25 +13,30 @@ "main": "index.js", | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/Turfjs/turf/issues" | ||
}, | ||
"keywords": [ | ||
"aggregate", | ||
"turf", | ||
"aggregate", | ||
"geojson", | ||
"points", | ||
"geojson", | ||
"polygons", | ||
"stats" | ||
], | ||
"author": "morganherlocker", | ||
"homepage": "https://github.com/Turfjs/turf", | ||
"author": "Morgan Herlocker", | ||
"contributors": [ | ||
"Rowan Winsemius" | ||
], | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/Turfjs/turf/issues" | ||
"dependencies": { | ||
"rbush": "^2.0.1", | ||
"@turf/bbox": "^3.7.5", | ||
"@turf/inside": "^3.7.5" | ||
}, | ||
"homepage": "https://github.com/Turfjs/turf", | ||
"devDependencies": { | ||
"benchmark": "^1.0.0", | ||
"tape": "^3.5.0", | ||
"@turf/helpers": "^3.7.3" | ||
"tape": "^3.6.1", | ||
"@turf/helpers": "^3.7.5" | ||
}, | ||
"dependencies": { | ||
"@turf/inside": "^3.7.3" | ||
}, | ||
"types": "index.d.ts" | ||
} |
37
test.js
@@ -1,2 +0,2 @@ | ||
var aggregate = require('./'); | ||
var turfCollect = require('./'); | ||
var test = require('tape'); | ||
@@ -7,16 +7,23 @@ var polygon = require('@turf/helpers').polygon; | ||
test('aggregate', function(t){ | ||
var poly1 = polygon([[[0,0],[10,0],[10,10],[0,10],[0,0]]]); | ||
var poly2 = polygon([[[10,0],[20,10],[20,20],[20,0],[10,0]]]); | ||
var polyFC = featurecollection([poly1, poly2]); | ||
var pt1 = point([5,5], {population: 200}); | ||
var pt2 = point([1,3], {population: 600}); | ||
var pt3 = point([14,2], {population: 100}); | ||
var pt4 = point([13,1], {population: 200}); | ||
var pt5 = point([19,7], {population: 300}); | ||
var ptFC = featurecollection([pt1, pt2, pt3, pt4, pt5]); | ||
var aggregated = aggregate(polyFC, ptFC, 'population', 'values'); | ||
t.deepEqual(aggregated.features[0].properties.values, [200, 600]); | ||
t.deepEqual(aggregated.features[1].properties.values, [100, 200, 300]); | ||
t.end(); | ||
test('turf collect module', function (t) { | ||
var poly1 = polygon([[[0, 0], [10, 0], [10, 10], [0, 10], [0, 0]]]); | ||
var poly2 = polygon([[[10, 0], [20, 10], [20, 20], [20, 0], [10, 0]]]); | ||
var poly3 = polygon([[[100, 0], [110, -10], [110, -20], [100, 0]]]); | ||
var polyFC = featurecollection([poly1, poly2, poly3]); | ||
var pt1 = point([5, 5], {population: 200}); | ||
var pt2 = point([1, 3], {population: 600}); | ||
var pt3 = point([14, 2], {population: 100}); | ||
var pt4 = point([13, 1], {population: 200}); | ||
var pt5 = point([19, 7], {population: 300}); | ||
var ptFC = featurecollection([pt1, pt2, pt3, pt4, pt5]); | ||
var aggregated = turfCollect(polyFC, ptFC, 'population', 'values'); | ||
// Check the same number of input and output polys are the same | ||
t.equal(polyFC.features.length, aggregated.features.length); | ||
// Check the right values have been assigned | ||
t.deepEqual(aggregated.features[0].properties.values, [200, 600]); | ||
t.deepEqual(aggregated.features[1].properties.values, [100, 200, 300]); | ||
// Check the property has been created even if no values have been assigned | ||
t.deepEqual(aggregated.features[2].properties.values, []); | ||
t.end(); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
9752
0
3
117
+ Added@turf/bbox@^3.7.5
+ Addedrbush@^2.0.1
+ Added@turf/bbox@3.14.0(transitive)
+ Added@turf/meta@3.14.0(transitive)
+ Addedquickselect@1.1.1(transitive)
+ Addedrbush@2.0.2(transitive)
Updated@turf/inside@^3.7.5