turf-aggregate
Advanced tools
Comparing version 1.0.3 to 3.0.0-canary.2f5f7167
var aggregate = require('./'); | ||
var Benchmark = require('benchmark'); | ||
var fs = require('fs'); | ||
var polygon = require('turf-polygon'); | ||
var point = require('turf-point'); | ||
var featurecollection = require('turf-featurecollection'); | ||
var polygon = require('turf-helpers').polygon; | ||
var point = require('turf-helpers').point; | ||
var featurecollection = require('turf-helpers').featureCollection; | ||
@@ -8,0 +8,0 @@ var poly1 = polygon([[[0,0],[10,0],[10,10],[0,10]]]) |
355
index.js
@@ -1,196 +0,171 @@ | ||
var average = require('turf-average'); | ||
var sum = require('turf-sum'); | ||
var median = require('turf-median'); | ||
var min = require('turf-min'); | ||
var max = require('turf-max'); | ||
var deviation = require('turf-deviation'); | ||
var variance = require('turf-variance'); | ||
var count = require('turf-count'); | ||
var operations = {}; | ||
operations.average = average; | ||
operations.sum = sum; | ||
operations.median = median; | ||
operations.min = min; | ||
operations.max = max; | ||
operations.deviation = deviation; | ||
operations.variance = variance; | ||
operations.count = count; | ||
var inside = require('turf-inside'); | ||
/** | ||
* Calculates a series of aggregations for a set of {@link Point|points} within a set of {@link Polygon|polygons}. Sum, average, count, min, max, and deviation are supported. | ||
* | ||
* @module turf/aggregate | ||
* @category aggregation | ||
* @param {FeatureCollection<Polygon>} polygons polygons with values on which to aggregate | ||
* @param {FeatureCollection<Point>} points points to be aggregated | ||
* @param {Array} aggregations an array of aggregation objects | ||
* @return {FeatureCollection<Polygon>} polygons with properties listed based on `outField` values in `aggregations` | ||
* @example | ||
* var polygons = { | ||
* "type": "FeatureCollection", | ||
* "features": [ | ||
* { | ||
* "type": "Feature", | ||
* "properties": {}, | ||
* "geometry": { | ||
* "type": "Polygon", | ||
* "coordinates": [[ | ||
* [1.669921, 48.632908], | ||
* [1.669921, 49.382372], | ||
* [3.636474, 49.382372], | ||
* [3.636474, 48.632908], | ||
* [1.669921, 48.632908] | ||
* ]] | ||
* } | ||
* }, { | ||
* "type": "Feature", | ||
* "properties": {}, | ||
* "geometry": { | ||
* "type": "Polygon", | ||
* "coordinates": [[ | ||
* [2.230224, 47.85003], | ||
* [2.230224, 48.611121], | ||
* [4.361572, 48.611121], | ||
* [4.361572, 47.85003], | ||
* [2.230224, 47.85003] | ||
* ]] | ||
* } | ||
* } | ||
* ] | ||
* }; | ||
* var points = { | ||
* "type": "FeatureCollection", | ||
* "features": [ | ||
* { | ||
* "type": "Feature", | ||
* "properties": { | ||
* "population": 200 | ||
* }, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [2.054443,49.138596] | ||
* } | ||
* }, | ||
* { | ||
* "type": "Feature", | ||
* "properties": { | ||
* "population": 600 | ||
* }, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [3.065185,48.850258] | ||
* } | ||
* }, | ||
* { | ||
* "type": "Feature", | ||
* "properties": { | ||
* "population": 100 | ||
* }, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [2.329101,48.79239] | ||
* } | ||
* }, | ||
* { | ||
* "type": "Feature", | ||
* "properties": { | ||
* "population": 200 | ||
* }, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [2.614746,48.334343] | ||
* } | ||
* }, | ||
* { | ||
* "type": "Feature", | ||
* "properties": { | ||
* "population": 300 | ||
* }, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [3.416748,48.056053] | ||
* } | ||
* } | ||
* ] | ||
* }; | ||
* 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 aggregated = turf.aggregate( | ||
* polygons, points, aggregations); | ||
* | ||
* var result = turf.featurecollection( | ||
* points.features.concat(aggregated.features)); | ||
* | ||
* //=result | ||
*/ | ||
* Joins attributes FeatureCollection of polygons with a FeatureCollection of | ||
* points. Given an `inProperty` on points and an `outProperty` for polygons, | ||
* this finds every point that lies within each polygon, collects the `inProperty` | ||
* values from those points, and adds them as an array to `outProperty` on the | ||
* polygon. | ||
* | ||
* @name collect | ||
* @category aggregation | ||
* @param {FeatureCollection<Polygon>} polygons polygons with values on which to aggregate | ||
* @param {FeatureCollection<Point>} points points to be aggregated | ||
* @param {Array} aggregations an array of aggregation objects | ||
* @return {FeatureCollection<Polygon>} polygons with properties listed based on `outField` values in `aggregations` | ||
* @example | ||
* var polygons = { | ||
* "type": "FeatureCollection", | ||
* "features": [ | ||
* { | ||
* "type": "Feature", | ||
* "properties": {}, | ||
* "geometry": { | ||
* "type": "Polygon", | ||
* "coordinates": [[ | ||
* [1.669921, 48.632908], | ||
* [1.669921, 49.382372], | ||
* [3.636474, 49.382372], | ||
* [3.636474, 48.632908], | ||
* [1.669921, 48.632908] | ||
* ]] | ||
* } | ||
* }, { | ||
* "type": "Feature", | ||
* "properties": {}, | ||
* "geometry": { | ||
* "type": "Polygon", | ||
* "coordinates": [[ | ||
* [2.230224, 47.85003], | ||
* [2.230224, 48.611121], | ||
* [4.361572, 48.611121], | ||
* [4.361572, 47.85003], | ||
* [2.230224, 47.85003] | ||
* ]] | ||
* } | ||
* } | ||
* ] | ||
* }; | ||
* var points = { | ||
* "type": "FeatureCollection", | ||
* "features": [ | ||
* { | ||
* "type": "Feature", | ||
* "properties": { | ||
* "population": 200 | ||
* }, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [2.054443,49.138596] | ||
* } | ||
* }, | ||
* { | ||
* "type": "Feature", | ||
* "properties": { | ||
* "population": 600 | ||
* }, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [3.065185,48.850258] | ||
* } | ||
* }, | ||
* { | ||
* "type": "Feature", | ||
* "properties": { | ||
* "population": 100 | ||
* }, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [2.329101,48.79239] | ||
* } | ||
* }, | ||
* { | ||
* "type": "Feature", | ||
* "properties": { | ||
* "population": 200 | ||
* }, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [2.614746,48.334343] | ||
* } | ||
* }, | ||
* { | ||
* "type": "Feature", | ||
* "properties": { | ||
* "population": 300 | ||
* }, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [3.416748,48.056053] | ||
* } | ||
* } | ||
* ] | ||
* }; | ||
* 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 aggregated = turf.aggregate( | ||
* polygons, points, statsProperty); | ||
* | ||
* var result = turf.featurecollection( | ||
* points.features.concat(aggregated.features)); | ||
* | ||
* //=result | ||
*/ | ||
module.exports = function collect(polyFC, ptFC, inProperty, outProperty) { | ||
polyFC.features.forEach(function (poly) { | ||
var values = ptFC.features.filter(function (pt) { | ||
return inside(pt, poly); | ||
}).map(function (pt) { | ||
return pt.properties[inProperty]; | ||
}); | ||
module.exports = function(polygons, points, aggregations) { | ||
for (var i = 0, len = aggregations.length; i < len; i++) { | ||
var agg = aggregations[i], | ||
operation = agg.aggregation; | ||
if (!poly.properties) { | ||
poly.properties = {}; | ||
} | ||
if (isAggregationOperation(operation)) { | ||
if (operation === 'count') { | ||
polygons = operations[operation](polygons, points, agg.outField); | ||
} else { | ||
polygons = operations[operation](polygons, points, agg.inField, agg.outField); | ||
} | ||
} else { | ||
throw new Error('"'+ operation +'" is not a recognized aggregation operation.'); | ||
} | ||
} | ||
poly.properties[outProperty] = values; | ||
}); | ||
return polygons; | ||
return polyFC; | ||
}; | ||
function isAggregationOperation(operation) { | ||
return operation === 'average' || | ||
operation === 'sum' || | ||
operation === 'median' || | ||
operation === 'min' || | ||
operation === 'max' || | ||
operation === 'deviation' || | ||
operation === 'variance' || | ||
operation === 'count'; | ||
} |
{ | ||
"name": "turf-aggregate", | ||
"version": "1.0.3", | ||
"version": "3.0.0-canary.2f5f7167", | ||
"description": "turf aggregate module", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "tape test.js", | ||
"doc": "dox -r < index.js | doxme --readme > README.md" | ||
"test": "tape test.js" | ||
}, | ||
@@ -31,18 +30,7 @@ "repository": { | ||
"tape": "^3.5.0", | ||
"turf-featurecollection": "^1.0.0", | ||
"turf-point": "^2.0.0", | ||
"turf-polygon": "^1.0.2", | ||
"dox": "^0.6.1", | ||
"doxme": "^1.4.3" | ||
"turf-helpers": "^3.0.0-canary.2f5f7167" | ||
}, | ||
"dependencies": { | ||
"turf-average": "^1.1.0", | ||
"turf-count": "^1.0.1", | ||
"turf-deviation": "^1.0.0", | ||
"turf-max": "^1.0.0", | ||
"turf-median": "^1.0.1", | ||
"turf-min": "^1.0.0", | ||
"turf-sum": "^1.0.0", | ||
"turf-variance": "^1.0.0" | ||
"turf-inside": "^3.0.0-canary.2f5f7167" | ||
} | ||
} |
67
test.js
var aggregate = require('./'); | ||
var test = require('tape'); | ||
var polygon = require('turf-polygon'); | ||
var point = require('turf-point'); | ||
var featurecollection = require('turf-featurecollection'); | ||
var polygon = require('turf-helpers').polygon; | ||
var point = require('turf-helpers').point; | ||
var featurecollection = require('turf-helpers').featureCollection; | ||
@@ -17,61 +17,6 @@ test('aggregate', function(t){ | ||
var ptFC = featurecollection([pt1, pt2, pt3, pt4, pt5]); | ||
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 aggregated = aggregate(polyFC, ptFC, aggregations); | ||
t.equal(aggregated.features[0].properties.pop_sum, 800); | ||
t.equal(aggregated.features[1].properties.pop_sum, 600); | ||
t.equal(aggregated.features[0].properties.pop_avg, 400); | ||
t.equal(aggregated.features[1].properties.pop_avg, 200); | ||
t.equal(aggregated.features[0].properties.pop_median, 400); | ||
t.equal(aggregated.features[1].properties.pop_median, 200); | ||
t.equal(aggregated.features[0].properties.pop_min, 200); | ||
t.equal(aggregated.features[1].properties.pop_min, 100); | ||
t.equal(aggregated.features[0].properties.pop_max, 600); | ||
t.equal(aggregated.features[1].properties.pop_max, 300); | ||
t.ok(aggregated.features[0].properties.pop_deviation); | ||
t.ok(aggregated.features[1].properties.pop_deviation); | ||
t.ok(aggregated.features[0].properties.pop_variance); | ||
t.ok(aggregated.features[1].properties.pop_variance); | ||
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(); | ||
}); |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
1
3
12562
7
254
1
+ Addedturf-inside@3.0.12(transitive)
+ Addedturf-invariant@3.0.12(transitive)
- Removedturf-average@^1.1.0
- Removedturf-count@^1.0.1
- Removedturf-deviation@^1.0.0
- Removedturf-max@^1.0.0
- Removedturf-median@^1.0.1
- Removedturf-min@^1.0.0
- Removedturf-sum@^1.0.0
- Removedturf-variance@^1.0.0
- Removedminimist@1.2.8(transitive)
- Removedsimple-statistics@0.9.2(transitive)
- Removedturf-average@1.1.2(transitive)
- Removedturf-count@1.0.2(transitive)
- Removedturf-deviation@1.0.1(transitive)
- Removedturf-inside@1.1.4(transitive)
- Removedturf-max@1.0.1(transitive)
- Removedturf-median@1.0.2(transitive)
- Removedturf-min@1.0.1(transitive)
- Removedturf-sum@1.0.1(transitive)
- Removedturf-variance@1.0.1(transitive)